aotearoa
aotearoa

Reputation: 315

Issue on Checkboxinput on the refreshing the main panel screen

I have a shiny app that a user can check a box if he want to show the data and if so he can select the number of data tables to be displayed in the main panel. There are 2 action buttons, one for submit and refresh. If a user selects 1 or 2 or 3 in the selectinput and hit submit.It will display the 3 datatables in the main panel. Once he hit refresh or unchecked the checkbox, it clears the main panel screen, however if he decided to check the box again, the last displayed table shows on the main panel. This should also clear the screen. How do I clear the screen if he checks the box again. Here is my code. Thank you for looking into this I am new to R and Shiny

    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)

Upvotes: 0

Views: 212

Answers (2)

Tonio Liebrand
Tonio Liebrand

Reputation: 17719

You should use if(input$refresh1 | !input$addData) isolate(global$refresh <- TRUE) instead of this line if(input$refresh1) isolate(global$refresh <- TRUE)

Edit: krish was faster. However, I leave the answer as is would shorten the code.

Upvotes: 2

krish
krish

Reputation: 1438

You can add this code to you server function.

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

This will remove the tables when you uncheck the checkbox

Upvotes: 2

Related Questions