r - Shiny:在 modalDialog() 中使用 textInput() 更新 react 值

标签 r shiny shiny-reactivity

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

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

这有效

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当我不使用l <- reactiveValues()时.

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 依赖项才能失效并重新渲染。因此,在这种情况下使用非 react 变量没有任何意义。

你应该致力于解决下游问题。

这是使用 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)

关于r - Shiny:在 modalDialog() 中使用 textInput() 更新 react 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71768304/

相关文章:

r - map() 中是否有 "next"等效项

r - fread 内存使用量远大于文件

r - 使用库 ('tidyverse' ) 而不是子包有什么好处?

r - Shiny模块之间的通信

r - ignoreInit 不适用于动态内容

r - 使用 purrr::pwalk 从 tibble 创建多个 Shiny 的 observeEvents

r - 如何删除数据框R中的某些日期

xml - 使用 XML 包将 TCX 导入 R

r - 如何使用自定义列名称将数据框转置为 Shiny

r - Shiny 的应用程序中选项卡名称之间没有空格