闪亮动态添加输入字段和数据而无需重新渲染

use*_*230 2 r shiny

我正在尝试动态地将新变量添加到正在运行的闪亮应用程序中,但如果我开始编辑一个变量,则每次我添加其他变量时,值(文本和数字)都会重置。这个例子不需要使用 for 循环就可以工作reactiveValuesToList(),但是当我将它应用到我的代码时,它不起作用。这是我的工作示例:

library(shiny)
dist <- c("Normal", "Gamma")
ui <- shinyUI(fluidPage(

  sidebarPanel(

    actionButton("add_btn", "Add Textbox"),
    actionButton("rm_btn", "Remove Textbox"),
    textOutput("counter")

  ),

  mainPanel(uiOutput("textbox_ui"))

))

server <- shinyServer(function(input, output, session) {

  # Track the number of input boxes to render
  counter <- reactiveValues(n = 0)

  # Track all user inputs
  AllInputs <- reactive({
    x <- reactiveValuesToList(input)
  })

  observeEvent(input$add_btn, {counter$n <- counter$n + 1})
  observeEvent(input$rm_btn, {
    if (counter$n > 0) counter$n <- counter$n - 1
  })

  output$counter <- renderPrint(print(counter$n))

  textboxes <- reactive({

    n <- counter$n

    if (n > 0) {
      isolate({
        lapply(seq_len(n), function(i) {
        fluidRow(
          selectInput(inputId = paste0("news", i),
                      label = paste0("Variable ", i),
                      choices = dist),

          conditionalPanel(
            condition = sprintf("input.%s=='Normal'", paste0("news", i)),
            textInput("txt", "Text input:", paste0("var", i)),  
            column(width = 3, numericInput('normal_mean', 'Mean', value = '0')), column(width = 3, numericInput('normal_sd', 'Standard deviation', value = '1'))),

          conditionalPanel(
            condition =  sprintf("input.%s=='Gamma'", paste0("news", i)),
            textInput("txt", "Text input:", paste0("var", i)),
            column(width = 3, numericInput('gamma_shape', 'Shape', value = '0')), column(width = 3, numericInput('gamma_scale', 'Scale', value = '1')))
        )
        })
      })
    }

  })

  output$textbox_ui <- renderUI({ textboxes() })

})

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试添加AllInputs()[[]]textInput,则不会将文本保留在conditionalPanel通话中:

conditionalPanel(
            condition = sprintf("input.%s=='Normal'", paste0("news", i)),
            textInput("txt", "Text input:", AllInputs()[[paste0("var", i)]]),  
            column(width = 3, numericInput('normal_mean', 'Mean', value = '0')), column(width = 3, numericInput('normal_sd', 'Standard deviation', value = '1')))
Run Code Online (Sandbox Code Playgroud)

我也不确定如何包含AllInputs()[[]]数值以使它们不会改变。

我认为问题出在我的condition线路上conditionalPanel,但无法弄清楚,有什么建议吗?谢谢

bre*_*auv 5

您应该考虑使用模块insertUI/ removeUI。单击按钮不会重置对已调用的输入所做的更改。在这里,您只有输入,因此您只需要调用add_box我创建的函数,但如果您想在模块中添加输出,那么您将需要callModule使用observeEvent. 我参考的文章中对此进行了解释。

这不是您建议的方法,但它有效。

library(shiny)

dist <- c("Normal", "Gamma")

add_box <- function(id){
  ns <- NS(id)
  tags$div(id = paste0("new_box", id),
           selectInput(inputId = ns("news"),
                       label = paste0("Variable ", id),
                       choices = dist),

           conditionalPanel(
             condition = "input.news=='Normal'",
             ns = ns,
             textInput(ns("txt"), "Text input:", paste0("var", id)),  
             column(width = 3, numericInput(ns('normal_mean'), 'Mean', value = '0')), 
             column(width = 3, numericInput(ns('normal_sd'), 'Standard deviation', value = '1'))),

           conditionalPanel(
             condition =  "input.news=='Gamma'",
             ns = ns,
             textInput(ns("txt"), "Text input:", paste0("var", id)),
             column(width = 3, numericInput(ns('gamma_shape'), 'Shape', value = '0')), 
             column(width = 3, numericInput(ns('gamma_scale'), 'Scale', value = '1')))
  )
}


ui <- shinyUI(fluidPage(

  sidebarPanel(

    actionButton("add_btn", "Add Textbox"),
    actionButton("rm_btn", "Remove Textbox"),
    textOutput("counter")

  ),

  mainPanel(column(width = 12, id = "column"))

))

server <- shinyServer(function(input, output, session) {

  # Track the number of input boxes to render
  counter <- reactiveValues(n = 0)

  # Track all user inputs
  AllInputs <- reactive({
    x <- reactiveValuesToList(input)
  })

  observeEvent(input$add_btn, {
    counter$n <- counter$n + 1
    insertUI(selector = "#column",
             where = "beforeEnd",
             ui = add_box(counter$n)
    )
  })

  observeEvent(input$rm_btn, {
    if (counter$n > 0) {
      removeUI(selector = paste0("#new_box", counter$n))
      counter$n <- counter$n - 1
    }
  })

  output$counter <- renderPrint(print(counter$n))


})

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)