【问题标题】:Shiny ModalDialog with InsertUI带有 InsertUI 的闪亮模态对话框
【发布时间】:2021-11-23 05:27:34
【问题描述】:

我有一个闪亮的应用程序,带有一个按钮,它调用一个模式对话框,应该在其中插入 UI。我正在使用insertUI,因为我想动态添加 UI 元素。问题是,每当我按下操作按钮时,应用程序就会崩溃并显示以下错误消息:

Warning: Error in as.character: cannot coerce type 'closure' to vector of type 'character'
  [No stack trace available]

如果我在insertUI() 中设置immediate = T 则没有错误,也没有插入UI。有人可以解释这里发生了什么吗?这是一个代表。

library(shiny)

ui <- fluidPage(
  actionButton("add", "addConstraints")
)

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

  observeEvent(input$add, {

    showModal(modalDialog(
      selectizeInput(session$ns("constraintType"), label = "Select constraint type", choices = c("Comparison", "Numeric", "Interval")),
      tags$div(id = session$ns("constraintPlaceholder")),
      insertUI(
        selector = paste0("#", session$ns("constraintPlaceholder")),
        where = "afterEnd",
        ui = HTML("test")
      ),
      title = "Set Constraints",
      footer = tagList(
        modalButton("Cancel"),
        actionButton(session$ns("confirmConstraint"), "Add")
      )
    ))

  })

}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    insertUI 需要在modalDialog 之外调用。 modalDialog 里面是 UI 组件,它们应该是 shinyTags 或 HTML 元素。 insertUI是服务端调用,不能添加到modalDialog

    library(shiny)
    
    ui <- fluidPage(
      actionButton("add", "addConstraints")
    )
    
    server <- function(input, output, session) {
    
      observeEvent(input$add, {
    
        showModal(modalDialog(
          selectizeInput(session$ns("constraintType"), label = "Select constraint type", choices = c("Comparison", "Numeric", "Interval")),
          tags$div(id = session$ns("constraintPlaceholder")),
          title = "Set Constraints",
          footer = tagList(
            modalButton("Cancel"),
            actionButton(session$ns("confirmConstraint"), "Add")
          )
        ))
        insertUI(
              selector = paste0("#", session$ns("constraintPlaceholder")),
              where = "afterEnd",
              ui = HTML("test")
        )
      })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 2021-08-03
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      相关资源
      最近更新 更多