Using Your Local R Environment to Deploy Blockspring Functions

Add a Parameter to Your Function

In this step, you'll add a new input parameter to your function. Your updated function will take a color as an input and pass that color right out.

library("blockspring")

block <- function(request, response){
        name <- paste("Hi! My name is",request$params$first_name)
        age <- paste("and my age is",request$params$age)
        # add a new input parameter
        color = request$params$color

        response$addOutput("intro", paste(name, age))
        # pass our input parameter right out
        response$addOutput("favorite_color", color)
        response$end()
}

blockspringDefine(block)

Your new code accesses the color input with request$params$color and just passes the color back out using the response$addOutput helper function.

To test, run your function from the command line. Make sure to provide a color key in the JSON input because that connects directly to request$params$color.

$ echo '{"first_name":"Jennifer","age":32, "color": "#a3b4c5"}' | blockspring run Rscript block.R
{"_blockspring_spec":true,"_errors":[],"intro":"Hi! My name is Jennifer and my age is 32", "favorite_color": "#a3b4c5"}

That's it. Pass inputs in through JSON and they are available in your code with request$params. Now push your updated function to Blockspring and open its webpage.

$ blockspring push
Syncronizing script file ./block.R
Syncronizing config file ./blockspring.json
$ blockspring open

If you run the function on the site, you'll see an error.

Your function expects request$params$color, but it can't find it. Blockspring isn't passing a color input parameter because it doesn't know about it yet.

Error

Let's explore how to tell Blockspring about your new input parameter.