Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a question regarding how to stop rendering a time consuming datatable when it is taking a long time to load. I have a shiny app which has multiple tabItems(library: shinydashboard), containing dataTableOutput which gets rows from restApi calls. Some of the dataTables are fast. But when a tabItem containing a time consuming datatable(say, table1) is clicked, R will stop processing contents of other tabItems, even if clicked, till table1 is loaded completely.

An reproducible example is:

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

get_data <- function(type){
    if(type == "historical"){
        GBP <- array(dim = 1)
        USD <- array(dim = 1)
        days <- seq(from=as.Date('2020-09-01'), to=as.Date("2021-01-01"),by='days' )
        for ( i in seq_along(days)){
            GBP[i] <- content(GET(paste0('https://api.ratesapi.io/api/',days[i])))$rates$GBP
            USD[i] <- content(GET(paste0('https://api.ratesapi.io/api/',days[i])))$rates$USD
        }
        return(datatable(data.frame("EURO" = rep(1, length(GBP)), "GBP" = GBP, "USD" = USD)))
    }
    if(type == "latest"){
        GBP <- content(GET('https://api.ratesapi.io/api/latest'))$rates$GBP
        USD <- content(GET('https://api.ratesapi.io/api/latest'))$rates$USD
        return(datatable(data.frame("EURO" = 1, "GBP" = GBP, "USD" = USD)))
    }
}

ui <- shinyUI(dashboardPage(skin = "black",
                      dashboardHeader(title = "Reproducible Example"),
                      dashboardSidebar(sidebarMenu(id = "sidebarmenu",
                                                   menuItem("Home", tabName = "home", icon = icon("home")),
                                                   menuItem("Historical Rate", tabName = "tab1", icon = icon("table")),
                                                   menuItem("Current Rate", tabName = "tab2", icon = icon("table"))
                      )
                      ),
                      dashboardBody(
                          useShinyjs(),
                          tabItems(
                              tabItem(tabName = "home",
                                      HTML("Home")
                              ),
                              tabItem(tabName = "tab1",
                                      box(DT::dataTableOutput("tableData1")%>% withSpinner(),width = 10)
                              ),
                              tabItem(tabName = "tab2",
                                      box(DT::dataTableOutput("tableData2")%>% withSpinner(),width = 10)
                              )
                          ))
))

server <- shinyServer(function(input, output) {
    
    output$tableData1 <- DT::renderDataTable(get_data("historical"))
    output$tableData2 <- DT::renderDataTable(get_data("latest"))
    
})

# Run the application 
shinyApp(ui = ui, server = server)

In the above app, if you click on tab "Historical" and then jump to tab "Latest", you have to wait for getting data in "Historical" first.

I want to interrupt this process of getting data, if I click other tabs and show that clicked tab's content. Can anyone help me with this?

question from:https://stackoverflow.com/questions/65934648/stop-rendering-time-consuming-datatables-in-r-shinydashboard

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
807 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...