Quickstart: Add your first R function to Blockspring.

In this tutorial we'll show you how to add an R function to Blockspring. Doing so is helpful if you need to:

Expose your function to the world. Need some R in your Rails app, Android app, Raspberry Pi, or even in Google Spreadsheets? You'll be able to run your function from any application and device by simply copy/pasting a single line of code.

Need some R in your Rails app, Android app, Raspberry Pi, or even in Google Spreadsheets? You'll be able to run your function from any application and device by simply copy/pasting a single line of code.

Share your function with anyone. You'll automatically generate a simple form and webpage documentation. Never spend time explaining your code, or running it for other people.

You'll automatically generate a simple form and webpage documentation. Never spend time explaining your code, or running it for other people.

Combine Blockspring functions together. You can mix/match Blockspring functions like building blocks. And the system gets really fast when it knows ahead of time which functions you're about to run together.

You can mix/match Blockspring functions like building blocks. And the system gets really fast when it knows ahead of time which functions you're about to run together.

Add really, really powerful caching. Don't just cache when you run a function, use Blockspring's social caching. What's the point of recalculating when someone's already done the calculating?

Let's get started!

Expose your function to the world.

This is going to be insanely fast. In 30 seconds, you're going to add a function to Blockspring that forecasts tomorrow's Tesla stock price given data pulled from Yahoo Finance.

suppressMessages(library( 'forecast' )) URL "http://ichart.finance.yahoo.com/table.csv?s=TSLA&a=7&b=1&c=2014" dat "%Y-%m-%d") series

Click to populate the Blockspring editor with the code above, click Publish, and you're done!

What just happened?



When you published your function, Blockspring automatically deployed it to the cloud, gave it an API, generated a webpage, and exposed it to all of the Blockspring client libraries.

Why is this awesome?



Your R function is now in the cloud. That means you can run it from any application or internet-enabled device with a single command. What once took 6 lines of R, now is 1 line in any language. For instance, to run your simple forecaster in Ruby, just type the following:

require ( "blockspring" ) puts Blockspring.runParsed( "09a4860d2f053394559ce956e480cc9a" )

Where do I learn more about using my function?

Your published function should like something like this forecaster. On each Blockspring function page you can click "Integrations", and depending on where you need to use your function, copy/paste the associated one line of code to run it. Check out the run R in Blockspring quickstart to learn more.

That was fast. Can you show me how to publish again?

Here's a quick video that shows you how to publish step-by-step.

Share your function with anyone.

Blockspring lets you share your work with programmers and non-programmers alike because we generate the webpage and docs.

Currently, the webpage for your forecaster function is just a "Submit" button. That's because this function doesn't take any inputs.

Let's tell Blockspring that your function accepts inputs. For instance, try forecasting any stock, not just Tesla.

Click to populate the Blockspring editor with the code below, click Publish, and you'll have a nice form for your function.

suppressMessages(library( 'forecast' )) suppressMessages(library( 'blockspring' )) blockspringDefine(function(request, response){ URL "http://ichart.finance.yahoo.com/table.csv?s=", request$params$ticker, "&a=7&b=1&c=2014" , sep= "" ) dat "%Y-%m-%d") series "forecast", forecasted_values) response$addOutput( "observed_values" , series) response$end() })

Take a look at the final product. Here's a video that shows the process step-by-step:

What just happened?



You updated your function in four ways:

Import the library . You imported the blockspring library with suppressMessages(library('blockspring')) . Blockspring captures outputs by reading what is printed. That's why we're suppressing loading messages.

. You imported the blockspring library with . Blockspring captures outputs by reading what is printed. That's why we're suppressing loading messages. Define the function . You wrapped the function in a blockspringDefine() . This tells Blockspring what function run.

. You wrapped the function in a . This tells Blockspring what function run. Get the inputs . Receiving the ticker input in request$params$ticker . All function inputs will be found in request$params . The "ticker" name is defined when you added a parameter through the Blockspring code editor.

. Receiving the ticker input in . All function inputs will be found in . The "ticker" name is defined when you added a parameter through the Blockspring code editor. Declare the outputs. Adding the forecast output using response$addOutput . Blockspring passes inputs and outputs as JSON. This is adding your output to the JSON response.

Why is this awesome?



You can now share a parameterized function without doing any extra work. By telling Blockspring about your inputs, you automatically get a form so anyone can run your function manually. Learn more about input parameter types.

Wait - how do I run my function in other languages with inputs?



Check out running R functions to get a refresher on running Blockspring functions. Here's the updated Ruby one-liner:

require ( "blockspring" ) puts Blockspring.runParsed( "7c3f1357a1a0a4d909132b194abc43aa" , { ticker: "MSFT" }).params[ "forecast" ]

While we're at it, we can also call our function from Javascript. Check out this simple line graph that uses our function:

Ticker

You're running R right from Javascript!

Now it's your turn.

Congratulations young grasshopper. You've learned to add a R function to your Blockspring library. Write a function, get an application. Now go build!