Create an R function
We have seen before how we can user OpenCPU to call R existing R functions from packages. In this post we demostrate how we can also publish a custom function without creating a package. For the purpose of this example, we wrote an R function that generates a plot using live data from YAHOO finance:
myfunction <- function(ticker) { require(ggplot2); url <- paste("http://ichart.finance.yahoo.com/table.csv?s=", ticker, "&a=07&b=19&c=2004&d=07&e=13&f=2015&g=d&ignore=.csv", sep=""); mydata <- read.csv(url); mydata$Date <- as.Date(mydata$Date); myplot <- qplot(Date, Close, data=mydata, geom=c("line", "smooth"), main=ticker); print(myplot); }
You can try running this function on your local R by calling for example myfunction("GOOG"); or myfunction("YHOO"); or any other valid ticker. If it works locally we can publish the function to OpenCPU.
Share your function
We can publish the function above by posting it to the /save output. We can do that through any client, but for convenience, we created a simple html form app that can use be to post functions: http://beta.opencpu.org/apps/opencpu.demo/storefunction/. If the saving of the function was successful, the server will return something that looks like this:
{
"object": [ "/tmp/a2c5af4c0f44fc1ffbdd974773905dde" ],
"graphs": [],
"files": {}
}
The function is stored as an object and can be called or retrieved using the given key. The prefix /tmp/ means that the function is stored in the temporary store. For now OpenCPU does not provide a permanent store yet. However stuff in the temp store stays available for quite a while (at least a couple of days/weeks).
Call the function
Now that we saved our function in the store, we would like to call it. The function that we created draws a plot. It does not return a formal object, nor does it create a file. Therefore there are three output types that we can use: png, pdf or svg. Have a look at the documentation if this does not make any sense to you.
To call the function and render the plot we could use one of the following urls. Go ahead and paste the link in the url of your browser.
http://beta.opencpu.org/R/call/store:tmp/a2c5af4c0f44fc1ffbdd974773905dde/pdf?ticker="GOOG"
http://beta.opencpu.org/R/call/store:tmp/a2c5af4c0f44fc1ffbdd974773905dde/svg?ticker="GOOG"
Notice a couple of things. The ticker argument for the function (ticker=”GOOG”) as specified using an HTTP parameter which is part of the URL. You can replace this with any other ticker that is available from Yahoo Finance. Also note that we did not specify any rendering device (pdf, png) in the function itself! The function only creates the plot; the rendering is done when the function is actually called. We can control the rendering using the output’s meta-parameters. Here are some other examples of the same function:
http://beta.opencpu.org/R/call/store:tmp/a2c5af4c0f44fc1ffbdd974773905dde/png?ticker="AAPL"&!width=800&!height=600
http://beta.opencpu.org/R/call/store:tmp/a2c5af4c0f44fc1ffbdd974773905dde/pdf?ticker="AAPL"&!width=8&!height=8
Open Source
We have seen how the /R interface allows us to call R functions, either from packages or from the store. However OpenCPU is meant to be open source by design. Once you post an R function, other people can retrieve the source code from the /store. A function is an object, and can be represented as several formats. Use one of the links above to retrieve the object in respectively ascii (dput), opencpu json encoding, RData (a workspace that you can open in R) or rds (read in R using readRDS).
http://beta.opencpu.org/R/store/tmp/a2c5af4c0f44fc1ffbdd974773905dde http://beta.opencpu.org/R/store/tmp/a2c5af4c0f44fc1ffbdd974773905dde/ascii http://beta.opencpu.org/R/store/tmp/a2c5af4c0f44fc1ffbdd974773905dde/encode http://beta.opencpu.org/R/store/tmp/a2c5af4c0f44fc1ffbdd974773905dde/rda http://beta.opencpu.org/R/store/tmp/a2c5af4c0f44fc1ffbdd974773905dde/rds
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Zero Inflated Models and Generalized Linear Mixed Models with R.
Zuur, Saveliev, Ieno (2012).