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})
}