当我使用 insertUI 时,我在 Shiny 应用程序中使用弹出框功能时遇到问题。请参阅下面的代码。
library(shinyBS)
library(shiny)
#ui----
ui = basicPage(
actionButton("show", "Create a New Analysis")
)
#server----
server = function(input, output, session) {
#Show modal when button is clicked.
observeEvent(input$show, {
showModal(dataModal())
})
#dataModal----
#The main modal dialog function. Sets the initial buttons shown on the dialog.
dataModal <- function() {
modalDialog(
h2("Analysis Setup", align = "center"),
h4("Choose a Setting of Care:", align = "center"),
#Level0----
#Inpatient button. The HTML function (i.e. div, style) is used to evenly space
#the buttons in the dialog window.
div(style="display:inline-block;width:32%;text-align: center;",
popify(actionButton("Inpatientz", "Inpatient", icon("user-md")),
"Inpatient",
"Dialogue 1.")),
#Emergency button. The HTML function (i.e. div, style) is used to evenly space
#the buttons in the dialog window.
div(style="display:inline-block;width:32%;text-align: center;",
popify(bsButton("Emergencyz", HTML("Emergency <br> Department"), icon("ambulance"), style = "default", size = "default"),
"Emergency",
"Dialogue 2.")),
#Ambulatory button. The HTML function (i.e. div, style) is used to evenly space
#the buttons in the dialog window.
div(style="display:inline-block;width:32%;text-align: center;",
popify(bsButton("Ambulatoryz", HTML("Ambulatory <br> Surgery"), icon("medkit"), style = "default", size = "default"),
"Ambulatory",
"Dialogue 3.")),
tags$div(id = 'placeholder'),
footer = tagList(
modalButton("Cancel"),
actionButton("ok", "OK")
),
#easyClose is an argument which allows the user to click outside the
#dialog window or press the escape key to close the dialog window.
easyClose = TRUE
)
}
#Level1----
observeEvent(input$Inpatientz, {
#Adds Descriptive Statistics button with popover.
insertUI(selector = '#placeholder',
ui = popify(bsButton("Descriptivez", "Descriptive Statistics", style = "default", size = "default"),
"Descriptive Statistics", "Quote 1"))
})
observeEvent(input$Emergencyz, {
#Adds Trends button with popover.
insertUI(selector = '#placeholder',
ui = popify(bsButton("Trendsz", "Trends", style = "default", size = "default"),
"Trends", "Quote 2"))
})
observeEvent(input$Ambulatoryz, {
#Adds Rank button with popover.
insertUI(selector = '#placeholder',
ui = popify(bsButton("Rankz", "Rank", style = "default", size = "default"),
"Rank", "Quote 3"))
})
#Close Modal
observeEvent(input$ok, {
removeModal()
})
}
shinyApp(ui, server)
使用此代码,我使用 Level0 bsbutton(住院、急诊和门诊(到模态窗口的初始输出成功显示弹出框。但是,响应 Level0 按钮而被动创建的 Level1 按钮(描述性统计、趋势和排名(不显示任何弹出框。我已经浏览了bsbutton文档,但没有成功。我该怎么办?
后台有一些很酷的javascript/web浏览器。我得到了一个使用addPopover
工作的解决方案,同时将immediate = TRUE
添加到insertUI
函数 (https://shiny.rstudio.com/reference/shiny/1.0.0/insertUI.html(,并删除popify
函数(不再需要(。
我的代码如下:
library(shinyBS)
library(shiny)
#ui----
ui = basicPage(
actionButton("show", "Create a New Analysis")
)
#server----
server = function(input, output, session) {
#Show modal when button is clicked.
observeEvent(input$show, {
showModal(dataModal())
})
#dataModal----
#The main modal dialog function. Sets the initial buttons shown on the dialog.
dataModal <- function() {
modalDialog(
h2("Analysis Setup", align = "center"),
h4("Choose a Setting of Care:", align = "center"),
#Level0----
#Inpatient button. The HTML function (i.e. div, style) is used to evenly space
#the buttons in the dialog window.
div(style="display:inline-block;width:32%;text-align: center;",
popify(actionButton("Inpatientz", "Inpatient", icon("user-md")),
"Inpatient",
"Dialogue 1.")),
#Emergency button. The HTML function (i.e. div, style) is used to evenly space
#the buttons in the dialog window.
div(style="display:inline-block;width:32%;text-align: center;",
popify(bsButton("Emergencyz", HTML("Emergency <br> Department"), icon("ambulance"), style = "default", size = "default"),
"Emergency",
"Dialogue 2.")),
#Ambulatory button. The HTML function (i.e. div, style) is used to evenly space
#the buttons in the dialog window.
div(style="display:inline-block;width:32%;text-align: center;",
popify(bsButton("Ambulatoryz", HTML("Ambulatory <br> Surgery"), icon("medkit"), style = "default", size = "default"),
"Ambulatory",
"Dialogue 3.")),
tags$div(id = 'placeholder'),
footer = tagList(
modalButton("Cancel"),
actionButton("ok", "OK")
),
#easyClose is an argument which allows the user to click outside the
#dialog window or press the escape key to close the dialog window.
easyClose = TRUE
)
}
#Level1----
observeEvent(input$Inpatientz, {
#Adds Descriptive Statistics button with popover.
insertUI(selector = '#placeholder',
ui = bsButton("Descriptivez", "Descriptive Statistics", style = "default", size = "default"), immediate = TRUE
)
addPopover(session, "Descriptivez", "Descriptive Statistics", "Quote 1")
})
observeEvent(input$Emergencyz, {
#Adds Trends button with popover.
insertUI(selector = '#placeholder',
ui = bsButton("Trendsz", "Trends", style = "default", size = "default")
, immediate = TRUE)
addPopover(session, "Trendsz", "Trends", "Quote 2")
})
observeEvent(input$Ambulatoryz, {
#Adds Rank button with popover.
insertUI(selector = '#placeholder',
ui = bsButton("Rankz", "Rank", style = "default", size = "default"), immediate = TRUE)
addPopover(session, "Rankz", "Rank", "Quote 3")
})
#Close Modal
observeEvent(input$ok, {
removeModal()
})
}
shinyApp(ui, server)