SNT
SNT

Reputation: 1393

Display output table in shiny based on user inputs and reset to default table

I have a shiny app which on load I want to show a default table. Now once the application is loaded the user can change the values in the inputs and once clicked on the run button it the current table should be replaced with the new table based on user inputs. And once done and if the user clicks on the reset button it should again show the default table. How can I achieve that. Below is the basic app

shinyApp(
ui = basicPage(
  mainPanel(
    numericInput("model_input", label = h5("Total Budget"), value = 9000000),
    numericInput("iterations", label = h5("Iterations"), value = 900),
    actionButton("run", "Run"),
    actionButton("reset", "reset"),
    tableOutput("view")
  )
),
server = function(input, output) {
  runcar<- reactive({
    mtcars %>% mutate(new = mpg * input$model_input +input$iterations)
         })
  output$view <- renderTable({
    mtcars
  })
}

)

Upvotes: 0

Views: 386

Answers (1)

tigerloveslobsters
tigerloveslobsters

Reputation: 646

I think the solution below should be able to solve your question.

library(dplyr)
library(shiny)

shinyApp(
  ui = basicPage(
    mainPanel(
      numericInput("model_input", label = h5("Total Budget"), value = 9000000),
      numericInput("iterations", label = h5("Iterations"), 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$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
    })
  }
)

Upvotes: 1

Related Questions