我只需要在按下按钮并且满足变量条件时显示 BS 模态。
这是一个简单的应用程序,演示了挑战是什么。我需要在num_rows >= 500
时显示 BS 模态,并且提交按钮被触发,而不仅仅是在触发提交按钮时。
我知道这可以通过使用 input.slider
作为条件之一的conditionalPanel
来完成,但在我的实际项目中,它比这复杂得多,并且 BS 模态/条件面板需要依赖于按钮(用户输入(和server
中分配的变量。
library(shiny)
library(shinyBS)
data = matrix(rnorm(1000*10, 0, 1), nrow = 1000)
ui <- fluidPage(
fluidRow(
column(width = 4,
sliderInput("slider", "Choose Number of Rows to Display", 0, 1000, value = NULL),
submitButton('Submit'),
bsModal("modalExample", "Yes/No", "submit", size = "small", wellPanel(
p(div(HTML("<strong>Warning: </strong> you have chosen to display a large
number of rows. Are you sure you want to proceed?"))),
actionButton("no_button", "Yes"),
actionButton("yes_button", "No")
))
),
column(width = 8,
tableOutput('data')
)
)
)
server <- shinyServer(function(input, output, server){
observe({
num_rows <- input$slider
if(num_rows >= 500){
#
# ACTIVATE MODAL PANEL
#
observeEvent(input$no_button, {
# Do not show table
})
observeEvent(input$yes_button, {
output$table <- renderTable(data)
})
} else{ # Display table normally if number of rows is less than 500
output$table <- renderTable(data)
}
})
})
shinyApp(ui, server)
看看下面的代码。如果与包 shinyjs num_rows<500
,我禁用了操作按钮。如果num_rows>=500
操作按钮可用于触发弹出窗口。要更新使用滑块选择的行数,您必须每次都按提交按钮。希望这对您有所帮助或为您提供一些想法。目前,我还没有实现您的警告消息(这对我不起作用(。另一个问题:弹出窗口的滑块和显示仅适用于增加行数,而不是之后减少。如果您找到解决方案,请分享=(
library(shiny)
library(shinyBS)
library(shinyjs)
data = matrix(rnorm(1000*10, 0, 1), nrow = 1000)
data1=data[(1:500),]
head(data)
ui <- fluidPage(
fluidRow(
column(width = 4,
sliderInput("slider", "Choose Number of Rows to Display", 0, 1000, value = NULL),
submitButton('Submit'),
actionButton('Show','Show'),
useShinyjs(),
bsModal("modalExample",'Yes/No','Show', size = "large",tableOutput("tab")
# wellPanel(
# p(div(HTML("<strong>Warning: </strong> you have chosen to display a large
# number of rows. Are you sure you want to proceed?")
# )))
)),
column(width = 8,tableOutput('table'))))
server <- function(input, output)({
observe({
num_rows = input$slider
if(num_rows<500 &num_rows!=0) {
shinyjs::disable('Show')
output$table <- renderTable({
data = data1[(1:num_rows),]
print(head(data1))
data})
}else{
shinyjs::enable('Show')
output$tab = renderTable({
data = data[(1:num_rows),]
data}) }
})
})
shinyApp(ui, server)