Обновление столбца в R датируется на основе входных данных в Shiny - PullRequest
1 голос
/ 12 марта 2019

Я пытаюсь добавить пользовательский ввод в выходную таблицу в приложении Shiny.И когда пользователь изменяет какие-либо значения для Total Cost, он должен обновиться в таблице, прежде чем я нажму на Run.Как я могу это исправить?

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 голосов
/ 13 марта 2019

Вы не можете использовать 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 
    })
  }
)
...