3

Below is the sample application where I have put spinner loading. But the issue is , even before the action button is pressed, the spinner is been seen. Actually, only when action button is pressed, it should come. I know this can be achieved by adding eventReactive, but is there a way to achieve this only by using observeEvent

library(shiny)
library(dplyr)
library(shinycssloaders)
library(DT)

ui <- fluidPage(

    actionButton("plot","plot"),
    withSpinner(dataTableOutput("Test"),color="black")
)



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

    observeEvent(input$plot, {
    output$Test <- DT::renderDT(DT::datatable(head(iris),
                                              rownames = FALSE, options = list(dom = 't', 
                                                                               ordering=FALSE)))

    })
}
shinyApp(ui = ui, server = server)
Edward
  • 10,360
  • 2
  • 11
  • 26
imran p
  • 332
  • 2
  • 12

1 Answers1

3

One solution is to use uiOutput so that the ui for the spinner and the table are created only when you click on the button:

library(shiny)
library(dplyr)
library(shinycssloaders)
library(DT)

ui <- fluidPage(
  actionButton("plot","plot"),
  uiOutput("spinner")
)

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

  observeEvent(input$plot, {

    output$spinner <- renderUI({
      withSpinner(dataTableOutput("Test"), color="black")
    })

    output$Test <- DT::renderDT({
      Sys.sleep(3)
      DT::datatable(head(iris), 
                    rownames = FALSE, options = list(dom = 't', ordering=FALSE))
    })

  })
}
shinyApp(ui = ui, server = server)
bretauv
  • 7,756
  • 2
  • 20
  • 57