9

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!

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
Chloe
  • 91
  • 1
  • 1
  • 2

1 Answers1

25

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)}
  )
})
alexwhan
  • 15,636
  • 5
  • 52
  • 66
  • 2
    HOLY COW!! THIS TOTALLY WORKS!! THANK YOU SO MUCH!!! I WORKED ON THIS ALL DAY!!! MANY THANKS TO YOU!!!!! HAVE A BEAUTIFUL DAY! – Chloe May 31 '13 at 05:04