2017-04-14 45 views
0

我只需要按下按鈕時顯示BS模式並滿足變量條件。Bootstrap模態多重條件R Shiny

這是一個簡單的應用程序,演示了挑戰是什麼。我需要顯示一個BS模式num_rows >= 500,並且提交按鈕被觸發,而不僅僅是當提交按鈕被觸發時。

我知道這可能是與使用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) 
+0

這是一個側面註解:因爲像'modalDialog'和'showModal' 2016年10月和光澤0.14,有光澤的優惠功能。看到我在這裏發佈的示例http://stackoverflow.com/questions/43408022/fileinput-button-with-selectinput-in-shiny。雖然使用像你這樣的shinyBS庫是絕對好的,但我個人認爲應用程序使用閃亮的本地函數更安全。 – Enzo

回答

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)