[英]I want to create reactive() data within a modalDialog (Shiny)
[英]Shiny: Update a reactive value with textInput() in modalDialog()
我想根据modalDialog()
中的textInput()
更新值。 我发现这个答案确实有效,但它使用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
依赖项才能失效并重新呈现。 因此,在这种情况下使用非反应性变量没有任何意义。
您更应该处理下游问题。
这是使用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.