根据Shiny中的输入更新R数据表中的列

时间:2019-03-12 20:44:51

标签: r datatable shiny

我正在尝试将用户输入追加到Shiny应用程序的输出表中。当用户更改Total Cost的任何值时,它应该在表中更新,然后再单击“运行”。我该如何解决?

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 个答案:

答案 0 :(得分:0)

您不能在反应上下文之外使用input$model_input。那可能引起一些问题。我们将其简单地移到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 
    })
  }
)