0

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

)

1 Answer 1

1

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
    })
  }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.