2012-11-01

PrettyR R

When it comes to R blogging I'm a complete newbie. So I'm still struggling with the technical details.

Part of the process is prettifying the code snippets. One of the standard ways of doing this involves copy-and-paste-ing the R code into the Pretty R syntax highlighter.

While assembling the bits to do the posting programmatically I wrote a function that replaces the copy-and-paste part.

Now here's the function prettified by itself:

prettyR <- function(file) {
  require(RCurl)
  require(XML)
  Rcode <- readLines(file)
  Rcode <- paste(Rcode, collapse="\n")
  # assemble the parameters for the http POST to the Pretty R web site
  URL <- "http://www.inside-r.org/pretty-r/tool"
  parameters <- list(
    op = "edit-submit", 
    form_id = "pretty_r_tool_form", 
    code_input = Rcode
  )
  # send the http POST request
  rawHTML <- postForm(URL, .params = parameters)
  parsedHTML <- htmlParse(rawHTML)
 
  # find the node
  prettified <- getNodeSet(parsedHTML, "//div[@class='form-item']/textarea")[[1]]
  prettified <- xmlValue(prettified[[1]])
  return(prettified)
}
Created by Pretty R at inside-R.org

1 comment:

  1. This is great. I've just started blogging about R and this will make my posts look so much nicer. Thanks!

    ReplyDelete