根据用户输入以闪亮的方式显示输出表并重置为默认表

时间:2019-02-12 20:51:45

标签: r 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) {
  runcar<- reactive({
    mtcars %>% mutate(new = mpg * input$model_input +input$iterations)
         })
  output$view <- renderTable({
    mtcars
  })
}

1 个答案:

答案 0 :(得分:1)

我认为下面的解决方案应该能够解决您的问题。

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