0

This is a follow-up to a previous question here: Add values to a reactive table in Shiny

Perhaps this is a rookie problem, but worth a shot. It's my first Shiny app.

I'm making a Shiny app that will allow users to store their inputs and values calculated from their inputs to a data.frame. This will be so they can compare values in the output. I've made a mock up of my code where I want to store 3 columns: inputs A and B, and a function based on inputs A and B.

I think my failure to understand evaluating reactives is my shortcoming here, so I'm turning to the community for help. I can get the code to work if I am just storing inputs and not the reactive function 'calcAB()'. Once I include the function, everything breaks down. I get an error:

Warning: Error in [<-.data.frame: replacement has 2 items, need 3

If I remove the calcAB() function I can save inputs without a problem. Once I include it, I get the above error. The question is how do I store the output of that reactive function in a data.frame?

My code is as follows:

UI Code:

shinyUI(fluidPage(
# Application title
titlePanel("Test Save Data to DF"),

# Sidebar with inputs A and B
sidebarLayout(
  sidebarPanel(
     sliderInput("A",
                 "Input A",
                 min = 1,
                 max = 50,
                 value = 30),
   numericInput("B",
                "Input B",
                min = 1,
                max = 30,
                value = 10),
# Action Button to save the inputs to a data.frame
   actionButton('update', label = 'Update')
),


mainPanel(
   tableOutput('testTable')
)
)
))

The server code:

shinyServer(function(input, output) {

     # My reactive function
  calcAB <- reactive({
   input$A * input$B
})


values <- reactiveValues()
values$df <- data.frame('A' = numeric(0), 'B' = numeric(0),
  'AB' = numeric(0))

newEntry <- observe({
  if(input$update >0) {
newLine <- isolate(c(input$A, input$B, calcAB()))
isolate(values$df[nrow(values$df)+1,] <- c(input$A, input$B, calcAB()))
})

output$testTable <- renderTable({values$df})
}
Community
  • 1
  • 1
Aaron
  • 15
  • 1
  • 7
  • Thanks, but using rbind still fails to include my calcAB() output as a column. Additionally, it gives me column names based on the input values for the first input (ie, X10, X30) and deletes the third column for calcAB. – Aaron Feb 02 '17 at 23:25
  • I think my problem has to do with how Shiny evaluates reactives, but I can't pin down what the particular problem is. If I replace the function calcAB() with a simple `input$A*input$B` my code works. This is fine for my example, but my real app has more complex reactives that I can't simplify into just multiplying two inputs. – Aaron Feb 03 '17 at 00:00

1 Answers1

1

Try with this code:

shinyServer(function(input, output) {
    calcAB <- reactive(input$A * input$B)

    values <- reactiveValues(df = data.frame('A' = numeric(0), 'B' = numeric(0), 'AB' = numeric(0)))

    newEntry <- observe({
      if(input$update >0) {
        values$df <- isolate(rbind(values$df,data.frame('A' =input$A, 'B' = input$B,'AB' = calcAB())))
      }})

    output$testTable <- renderTable({values$df})
    })
HubertL
  • 19,246
  • 3
  • 32
  • 51