2017-04-21 78 views
0

我有一个闪亮的应用程序,用户可以检查一个框,如果他想显示数据,如果是的话,他可以选择数据表的数量显示在主面板。有两个操作按钮,一个用于提交和刷新。如果用户在selectinput中选择1或2或3并点击提交,它将在主面板中显示3个数据表。一旦他点击刷新或取消选中该复选框,它将清除主面板屏幕,但是如果他决定再次选中该框,最后显示的表格会显示在主面板上。这也应该清除屏幕。如果他再次选中该框,我如何清除屏幕。这是我的代码。感谢您寻找到这一点,我是新来的R和闪亮刷新主面板屏幕上Checkboxinput问题

library(shiny) 
    library(DT) 
    ui <- fluidPage(
     sidebarLayout(
     sidebarPanel(
     checkboxInput("addData", "Add Data"), 
     conditionalPanel(condition="input.addData === true", 
      selectInput("amountTable", "Amount Tables", 1:10), 
      actionButton("submit1" ,"Submit", icon("refresh"), 
         class = "btn btn-primary"), 

      actionButton("refresh1" ,"Refresh", icon("refresh"), 
         class = "btn btn-primary") 
     ) 

     ), 
     mainPanel(
     # UI output 
     uiOutput("dt") 
     ) 
    ) 
    ) 

    server <- function(input, output, session) { 

    global <- reactiveValues(refresh = FALSE) 

    observe({ 
     if(input$refresh1) isolate(global$refresh <- TRUE) 
    }) 

    observe({ 
     if(input$submit1) isolate(global$refresh <- FALSE) 
    }) 

    observeEvent(input$submit1, { 
     lapply(1:input$amountTable, function(amtTable) { 
     output[[paste0('T', amtTable)]] <- DT::renderDataTable({ 
     iris[1:amtTable, ] 
      }) 
    }) 
    }) 

    observeEvent(input$submit1, { 

    lapply(1:input$amountTable, function(j) { 
     output[[paste0('Text', j)]] <- renderText({ 
     paste0("This is AmountTable", j) 
     # br() ## add space in between the text and table 
     }) 
    }) 
}) 

output$dt <- renderUI({ 
    if(global$refresh) return() 
    tagList(lapply(1:input$amountTable, function(i) { 
     list(textOutput(paste0('Text', i)),br(), 
      dataTableOutput(paste0('T', i))) 
    })) 
}) 


} 

shinyApp(ui, server) 
+0

对我来说,如果我取消勾选框,主屏幕不会取消选中。 – BigDataScientist

回答

1

您可以将此代码添加到您server功能。

observe({ 
    if(input$addData == FALSE) isolate(global$refresh <- TRUE) 
    }) 

这将删除表如果没有选择此checkbox

+0

非常感谢。这是我正在寻找的。 – aotearoa

1

,您应该使用 if(input$refresh1 | !input$addData) isolate(global$refresh <- TRUE)而不是此行if(input$refresh1) isolate(global$refresh <- TRUE)

编辑:krish也较快。但是,我留下的答案会缩短代码。