簡體   English   中英

根據Shiny中的輸入更新R數據表中的列

[英]Update column in R datatable based on inputs in 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 
    })
  }
)

您不能在反應上下文之外使用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 
    })
  }
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM