2017-04-14 4 views
0

버튼을 누를 때 BS 모달 만 표시하고 변수 조건이 충족되어야합니다.부트 스트랩 모달 복수 조건 R Shiny

이것은 간단한 도전 과제를 보여주는 앱입니다. num_rows >= 500, 제출 버튼이 시작되었을 때뿐만 아니라 제출 버튼이 시작되면 BS 모달을 표시해야합니다.

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

다음 코드를 살펴보십시오. 패키지 shinyjs로 num_rows<500의 경우 작업 버튼을 비활성화했습니다. num_rows>=500 버튼을 누르면 팝업이 트리거됩니다. 슬라이더로 선택된 행 수를 업데이트하려면 매번 제출 단추를 눌러야합니다. 희망이 도움이되거나 당신에게 몇 가지 아이디어를 얻을 수 있습니다. 현재로서는 귀하의 경고 메시지를 구현하지 않았습니다 (저에게는 적합하지 않습니다). 또 다른 문제점 : 팝업을위한 슬라이더와 디스플레이는 이후에 감소하지 않고 행 수가 증가하는 경우에만 작동합니다. 당신이, pls는 공유에 대한 해결책을 =)

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)