PrettyR R

November 1, 2012

(This article was first published on A Distant ObserveR, and kindly contributed to R-bloggers)

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) {
Rcode <- readLines(file)
Rcode <- paste(Rcode, collapse="\n")
# assemble the parameters for the http POST to the Pretty R web site
URL <- ""
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]])

To leave a comment for the author, please follow the link and comment on their blog: A Distant ObserveR. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)