代码之家  ›  专栏  ›  技术社区  ›  SNT

基于Shiny中的输入更新R datatable中的列

  •  0
  • SNT  · 技术社区  · 7 年前

    我正在尝试将用户输入附加到Shiny app的输出表中。当用户更改 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 
        })
      }
    )
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Sada93    7 年前

    你不能用 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 
        })
      }
    )