【问题标题】:RShiny: How to have sequential Modals in for loopR Shiny:如何在 for 循环中有顺序模型
【发布时间】:2019-06-16 11:03:00
【问题描述】:

我正在尝试在我的 RShiny 应用程序中实现模式弹出窗口,要求用户输入日期。在向用户询问新日期之前,输入的日期将用于循环内的进程中。使用我当前的代码,仅弹出最后一个循环的模式,在本例中为循环 5。如何更改我的代码以使每个循环都有一个弹出模式?

这是我当前代码的示例:

ui = basicPage(
  actionButton("show", "Show modal dialog")
)

server <- function(input, output) {

  observeEvent(input$show, {
    for(i in 1:5){
      showModal(modalDialog(
        textInput(paste("modal",i,sep=" "), paste("Please enter a date for ID", i, sep = " "),
        placeholder = "Please use format MM/DD/YYYY"),
        footer = tagList(modalButton("Enter"))
        ###Process using inputted date for loop
      ))}})}




shinyApp(ui = ui, server = server)

【问题讨论】:

  • 欢迎来到 SO!有一些类似的问题需要多个输出,...您可能想查看local(),...

标签: r loops for-loop shiny modal-dialog


【解决方案1】:

这并不能直接回答您为什么它不适用于您的代码的问题(答案有点复杂,但@BigDataScientist 是正确的,有许多老问题与“为什么 a for循环只输出最后一个结果?”)。但这里有一个使用shinyalert modal 而不是闪亮的modals 的解决方案:

NUM_MODALS <- 5

ui <- fluidPage(
  shinyalert::useShinyalert(),
  actionButton("show", "Show modal dialog"),
  lapply(seq(NUM_MODALS), function(id) {
    div(id, ":", textOutput(paste0("modal", id), inline = TRUE))
  })
)

server <- function(input, output) {

  observeEvent(input$show, {
    for(id in 1:5){
      shinyalert::shinyalert(
        type = "input",
        text = paste("Please enter a date for ID", id),
        inputPlaceholder = "Please use format MM/DD/YYYY",
        inputId = paste0("modal", id)
      )
    }
  })

  lapply(seq(NUM_MODALS), function(id) {
    output[[paste0("modal", id)]] <- renderText({
      input[[paste0("modal", id)]]
    })
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2017-03-04
    • 2020-01-24
    • 1970-01-01
    相关资源
    最近更新 更多