Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R and Shiny: Pass inputs from sliders to reactive function to compute output

I have 6 parameters that the user can change values for. These are our 6 inputs. I want to create an output value that takes those 6 inputs and computes our value of interest given a number of related equations in a function. Here is what I have in my UI...

library(shiny)

# Define UI for slider demo application
shinyUI(pageWithSidebar(

#  Application title
headerPanel("# Header Title Goes Here"),

# Sidebar with sliders that demonstrate various available options
sidebarPanel(
# Simple integer interval
sliderInput("u1", "Name:", 
            min=0, max=10000, value=10000),
#There are 6 slider inputs...

    ),

# Show a table summarizing the values entered
mainPanel(
 tableOutput("values"),

 uiOutput("focal"))
 )
)  

Here is what I have in my server.R...

library(shiny)

shinyServer(function(input, output) {

# Reactive expression to compose a data frame containing all of the values
sliderValues <- reactive({

# Compose data frame
data.frame(
  Name = # Names of my 6 parameters,

  Value = # inputs based on my 6 values by `input$value`,

  stringsAsFactors=FALSE)
})

f <- renderText({function(r1, r2, r3, d1, d2, u1) #these are my 6 values
{ #List of equations that compute f based on the 6 values
}
})


# Show the values using an HTML table
output$values <- renderTable({
sliderValues()
})

# Show the final calculated value 
output$focal <- renderText({
f
})
})

I keep getting... Error: argument 1(type 'closure') cannot be handled by 'cat' and many other errors. I just don't how to transfer the updated user inputs of the 6 parameters to my function and spit that function back out in the output area on the Shiny html page.

Any help would be greatly appreciated!!

Thanks!

like image 503
Chloe Avatar asked May 31 '13 01:05

Chloe


People also ask

How do you use reactive in Shiny R?

A reactive expression is an R expression that uses widget input and returns a value. The reactive expression will update this value whenever the original widget changes. To create a reactive expression use the reactive function, which takes an R expression surrounded by braces (just like the render* functions).

What is reactivity R Shiny?

Reactivity is what makes your Shiny apps responsive. It lets the app instantly update itself whenever the user makes a change. You don't need to know how reactivity occurs to use it (just follow the steps laid out in Lesson 4), but understanding reactivity will make you a better Shiny programmer.

When defining the server logic for a Shiny app you define a function that includes which of the following parameters?

Its three parameters – input , output , and session – should be familiar: every module function must take those three parameters.

What is reactivity in shiny programming?

Reactivity is what makes your Shiny apps responsive. It lets the app instantly update itself whenever the user makes a change. You don’t need to know how reactivity occurs to use it (just follow the steps laid out in Lesson 4), but understanding reactivity will make you a better Shiny programmer.

How does the shinyintro reactive demo work?

You can also access this app with shinyintro::app ("reactive_demo") or view it in a separate tab with the showcase interface . Here is the relevant code for the UI. There are four inputs: cut, color, clarity, and update. There are two outputs: title and plot. Whenever an input changes, it will trigger some types of functions to run.

How do I customize slider controls in shiny?

Customizing Sliders. Shiny slider controls are extremely capable and customizable. Features supported include: The ability to input both single values and ranges Custom formats for value display (e.g for currency) The ability to animate the slider across a range of values Slider controls are created by calling the sliderInput function.

What are the features of the slider controls?

Shiny slider controls are extremely capable and customizable. Features supported include: Custom formats for value display (e.g for currency) The ability to animate the slider across a range of values Slider controls are created by calling the sliderInput function.


1 Answers

I think there are a couple of confusions here. Firstly, where you define f in server.R, I think you just want to define a function the way you normally would. Then, when you do renderText(), you can call the function to get your value.

The way you have it now, you're creating a function inside renderText(), which you're then trying to get renderText to display, without giving it your arguments. That's why you're getting the error message, because renderText passes its first argument to cat, which does not know what to do with the function. It can, however, handle the output of a function.

Anyway, the following works for me. I've only done two sliders, but you can presumably extend it yourself.

ui.R:

#ui.R
library(shiny)

# Define UI for slider demo application
shinyUI(pageWithSidebar(

  #  Application title
  headerPanel("# Header Title Goes Here"),

  # Sidebar with sliders that demonstrate various available options
  sidebarPanel(
    # Simple integer interval
    sliderInput("u1", "Name:", 
                min=0, max=10000, value=10000),
    sliderInput("r1", "r1:", 
                min=0, max=10000, value=10000)


  ),

  # Show a table summarizing the values entered
  mainPanel(
    tableOutput("values"),

    uiOutput("focal"))
)
) 

server.R

#server.R
library(shiny)

shinyServer(function(input, output) {

  # Reactive expression to compose a data frame containing all of the values
  sliderValues <- reactive({

    # Compose data frame
    data.frame(
      Name = c("u1", "r1"),

        Value = c(input$u1,
                  input$r1),

        stringsAsFactors=FALSE)
  })

  f <- function(u1, r1) {
    u1 + r1
  }


  # Show the values using an HTML table
  output$values <- renderTable({
    sliderValues()
  })

  # Show the final calculated value 
  output$focal <- renderText(
    {f(input$u1, input$r1)}
  )
})
like image 61
alexwhan Avatar answered Oct 01 '22 15:10

alexwhan