Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner loading before action button is pressed

Tags:

r

shiny

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)
like image 638
imran p Avatar asked Dec 18 '25 07:12

imran p


1 Answers

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)
like image 51
bretauv Avatar answered Dec 20 '25 23:12

bretauv