简体   繁体   English

R Shinny - 在点击 actionbutton 之前显示 Spinner

[英]R Shinny - Spinner is displayed before clicking on actionbutton

I would like to display a spinner after clicking on an actionbutton and before the datatable values are shown.我想在单击操作actionbutton之后和显示数据表值之前显示一个datatable器。

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.我尝试了几个选项( renderUIoutput$table_properties <- renderDT({NULL}) ...)但没有成功。

output$table_properties <- renderDT({NULL}) won't work because withSpinner will show exactly when renderDT does not provide an output that can be rendered as data.table. output$table_properties <- renderDT({NULL})将不起作用,因为当renderDT不提供可呈现为 data.table 的 output 时, withSpinner将准确显示。

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.在这种情况下,表的数据不是在renderDT内部生成的,而是使用反应值来允许从服务器内的任何地方访问数据。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM