1
votes

I am trying to append the user input to the output table in Shiny app. And when the user changes any values for Total Cost it should update in the table before I click on run. How can I fix that?

library(dplyr)
library(shiny)

shinyApp(
  ui = basicPage(
    mainPanel(
      numericInput("model_input", label = h5("Total Cost"), value = 10000),
      numericInput("iterations", label = h5("Runs"), value = 900),
      actionButton("run", "Run"),
      actionButton("reset", "reset"),
      tableOutput("view")
    )
  ),
  server = function(input, output) {
    v <- reactiveValues(data = mtcars %>% mutate(budget  = input$model_input))  # this makes sure that on load, your default data will show up

    observeEvent(input$run, {
      v$data <- mtcars %>% mutate(new = mpg * input$model_input +input$iterations)
    })

    observeEvent(input$reset, {
      v$data <- mtcars # your default data
    })  

    output$view <- renderTable({
      v$data 
    })
  }
)
1

1 Answers

0
votes

You cant use input$model_input outside a reactive context. That was probably causing some issues. We simple move it outside into an observeEvent.

library(dplyr)
library(shiny)

shinyApp(
  ui = basicPage(
    mainPanel(
      numericInput("model_input", label = h5("Total Cost"), value = 10000),
      numericInput("iterations", label = h5("Runs"), value = 900),
      actionButton("run", "Run"),
      actionButton("reset", "reset"),
      tableOutput("view")
    )
  ),
  server = function(input, output) {
    v <- reactiveValues(data = mtcars)  # this makes sure that on load, your default data will show up

    observeEvent(input$model_input,{
      v$data <- v$data %>% mutate(budget  = input$model_input)
    })
    observeEvent(input$run, {
      v$data <- mtcars %>% mutate(new = mpg * input$model_input +input$iterations)
    })

    observeEvent(input$reset, {
      v$data <- mtcars # your default data
    })  

    output$view <- renderTable({
      v$data 
    })
  }
)