Bootstrap Modal Multiple Conditions R Shiny

时间:2017-04-14 07:34:12

标签: r shiny shinybs

当按下按钮时,我只需要显示BS模态,并且满足变量的条件。

这是一个简单的应用程序,展示了挑战。我需要在num_rows >= 500时显示BS模态,并且触发提交按钮,而不仅仅是在触发提交按钮时。

我知道这可以使用conditionalPanel使用input.slider作为其中一个条件来完成,但在我的实际项目中它比这更复杂,并且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)

1 个答案:

答案 0 :(得分:1)

查看以下代码。如果num_rows<500包含shinyjs,我禁用了操作按钮。如果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)
相关问题