Home > Net >  R Shinny - Spinner is displayed before clicking on actionbutton
R Shinny - Spinner is displayed before clicking on actionbutton

Time:12-31

I would like to display a spinner after clicking on an actionbutton and before the datatable values are shown.

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


ui <- fluidPage(
  navbarPage("Query Tool",
             navbarMenu("Structures",
                        tabPanel("Structure Properties", fluid = TRUE,
                                 sidebarLayout(
                                   sidebarPanel(
                                     textInput("structure_id_properties", strong("Structure:"), value = ''),
                                     actionButton("run_properties", "Run Analysis", icon = icon("play"))),
                                   mainPanel(
                                     tabsetPanel(type = "tabs",
                                                 tabPanel("Data Table",br(), withSpinner(DTOutput("table_properties")))
                                     ))
                                 )))))


server <- function(input, output) {
  observeEvent(input$run_properties, {
    structure_id_properties <- "test"    
    output$table_properties <- renderDT ({data_output(sql_data)})
    output$query_properties <- renderText({properties_sql}) 
  })
}

I have tried several options (renderUI, output$table_properties <- renderDT({NULL})...) in vain.

CodePudding user response:

output$table_properties <- renderDT({NULL}) won't work because withSpinner will show exactly when renderDT does not provide a n output that can be rendered as data table.

Here would be one way of doing it. In this case the the data for the table aren't generated inside renderDT but it uses a reactive value to allow access to the data from anywhere inside the server.

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


ui <- fluidPage(
  navbarPage("Query Tool",
             navbarMenu("Structures",
                        tabPanel("Structure Properties", fluid = TRUE,
                                 sidebarLayout(
                                   sidebarPanel(
                                     textInput("structure_id_properties", strong("Structure:"), value = ''),
                                     actionButton("run_properties", "Run Analysis", icon = icon("play"))),
                                   mainPanel(
                                     tabsetPanel(type = "tabs",
                                                 tabPanel("Data Table",
                                                          br(), 
                                                          withSpinner(DTOutput("table_properties")))
                                     ))
                                 )))))


server <- function(input, output) {
  data_output <- reactiveValues(sql_data = data.frame(test1 = character(), test2 = character(), test3 = integer()))
  
  observeEvent(input$run_properties, {
    data_output$sql_data <- NULL
    Sys.sleep(2.5) # for demo purposes
    data_output$sql_data <- data.frame(test1 = letters[1:5], test2 = LETTERS[1:5], test3 = 1:5)
  })
  output$table_properties <- renderDT ({req(data_output$sql_data)})
  
  output$query_properties <- renderText({properties_sql}) 
  
}

shinyApp(ui, server)
  • Related