闪亮:在 modalDialog() 中使用 textInput() 更新反应值

Shiny: Update a reactive value with textInput() in modalDialog()

我想根据 modalDialog() 中的 textInput() 更新值。我发现 this 答案确实有效,但它使用 reactiveValues() 这会在我的代码中进一步产生问题。

有没有办法在不使用 reactiveValues() 的情况下通过 textInput()modalDialog() 更新我的值 text

这个有效

library(shiny)
library(shinyWidgets)

if (interactive()) {
  shinyApp(
    ui <- fluidPage(
      actionBttn("reset", "RESET", style="simple", size="sm", color = "warning"),
      verbatimTextOutput(outputId = "text")
    ),
    server = function(input, output, session) {
      l <- reactiveValues()
      observeEvent(input$reset, {
        # display a modal dialog with a header, textinput and action buttons
        showModal(modalDialog(
          tags$h2('Please enter your personal information'),
          textInput('name', 'Name'),
          textInput('state', 'State'),
          footer=tagList(
            actionButton('submit', 'Submit'),
            modalButton('cancel')
          )
        ))
      })

      # only store the information if the user clicks submit
      observeEvent(input$submit, {
        removeModal()
        l$name <- input$name
        l$state <- input$state
      })

      # display whatever is listed in l
      output$text <- renderPrint({
        if (is.null(l$name)) return(NULL)
        paste('Name:', l$name, 'and state:', l$state)
      })
    }
  )
}

这不行

当我不使用 l <- reactiveValues() 时,无法更新值 l

library(shiny)
library(shinyWidgets)

if (interactive()) {
  shinyApp(
    ui <- fluidPage(
      actionBttn("reset", "RESET", style="simple", size="sm", color = "warning"),
      verbatimTextOutput(outputId = "text")
    ),
    server = function(input, output, session) {
      
      l <- NULL
      
      
      
      observeEvent(input$reset, {
        # display a modal dialog with a header, textinput and action buttons
        showModal(modalDialog(
          tags$h2('Please enter your personal information'),
          textInput('name', 'Name'),
          footer=tagList(
            actionButton('submit', 'Submit'),
            modalButton('cancel')
          )
        ))
      })
      
      # only store the information if the user clicks submit
      observeEvent(input$submit, {
        removeModal()
        l <- input$name
      })
      
      # display whatever is listed in l
      output$text <- renderPrint({
        if (is.null(l)) return(NULL)
        paste('Name:', l)
      })
    }
  )
}

renderPrint 需要 reactive 依赖无效并且 re-rendered。因此,在这种情况下使用 non-reactive 变量没有任何意义。

你更应该处理下游问题。

这是另一种使用 eventReactive 的方法:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  br(),
  actionBttn("reset", "RESET", style="simple", size="sm", color = "warning"),
  verbatimTextOutput(outputId = "text")
)

server <- function(input, output, session) {
  observeEvent(input$reset, {
    # display a modal dialog with a header, textinput and action buttons
    showModal(modalDialog(
      tags$h2('Please enter your personal information'),
      textInput('name', 'Name'),
      footer=tagList(
        actionButton('submit', 'Submit'),
        modalButton('cancel')
      )
    ))
  })
  
  # only store the information if the user clicks submit
  submittedName <- eventReactive(input$submit, {
    removeModal()
    input$name
  })
  
  output$text <- renderPrint({
    req(submittedName())
    paste('Name:', submittedName())
  })
}

shinyApp(ui, server)

编辑: 使用 reactiveVal 作为占位符:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  br(),
  actionBttn("reset", "RESET", style="simple", size="sm", color = "warning"),
  verbatimTextOutput(outputId = "text")
)

server <- function(input, output, session) {
  observeEvent(input$reset, {
    # display a modal dialog with a header, textinput and action buttons
    showModal(modalDialog(
      tags$h2('Please enter your personal information'),
      textInput('name', 'Name'),
      footer=tagList(
        actionButton('submit', 'Submit'),
        modalButton('cancel')
      )
    ))
  })
  
  submittedName <- reactiveVal("placeholder")
  
  # only store the information if the user clicks submit
  observeEvent(input$submit, {
    removeModal()
    submittedName(input$name)
  })
  
  output$text <- renderPrint({
    paste('Name:', submittedName())
  })
}

shinyApp(ui, server)