You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shinydashboard实现点击按钮后延迟30秒导入数据并展示

Solution for Your Shiny Dashboard Requirements

Got it, let's tackle your two needs step by step—hiding the table until data is ready, and adding a 30-second delay after clicking Submit without freezing the app. Here's the adjusted code with explanations:

Modified UI Code

(Your original UI works just fine; we’ll handle visibility and behavior in the server logic)

library(shiny)
library(shinydashboard)
library(DT)
library(dataiku)

path <- dkuManagedFolderPath("receiver") # Folder with files to choose from

dashboardPage(
  dashboardHeader(title = "Catchy Title"),
  dashboardSidebar(
    selectizeInput("file", "Select File", list.files(path)),
    actionButton("submit", "Submit")
  ),
  dashboardBody(
    fluidRow(
      box(h2("Show Data"), 
          div(style = 'overflow-x: scroll', DT::dataTableOutput('mytable')), 
          width = 12)
    )
  )
)

Modified Server Code

This is where the key changes happen to meet your requirements:

library(dataiku)
library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)

shinyServer(function(input, output) {
  # Reactive value to store our final dataset (starts empty)
  final_dataset <- reactiveVal(NULL)
  
  # Trigger action when Submit button is clicked
  observeEvent(input$submit, {
    # Ensure a file is selected before proceeding
    req(input$file)
    
    # Clear any existing data so old results don't stay visible
    final_dataset(NULL)
    
    # Set up a one-time delayed execution (30 seconds = 30000 milliseconds)
    delayed_task <- observe({
      # Wait 30 seconds before running the dataset logic
      invalidateLater(30000)
      
      # Safely load and select the correct dataset
      tryCatch({
        intermediate_data <- dkuReadDataset("intermediate_dataset")
        # Check if intermediate dataset has rows to decide which final data to load
        df <- if (is.null(nrow(intermediate_data))) {
          dkuReadDataset("final_data1")
        } else {
          dkuReadDataset("final_data2")
        }
        # Update the reactive value with our loaded data
        final_dataset(df)
      }, error = function(e) {
        # Show a user-friendly error if something goes wrong
        showNotification(paste("Error loading data:", e$message), type = "error")
        final_dataset(NULL)
      })
      
      # Stop this observer after it runs once (no repeated tasks)
      cancelObserver(delayed_task)
    })
  })
  
  # Render the table ONLY when valid data is available
  output$mytable = DT::renderDataTable({
    req(final_dataset()) # This keeps the table hidden until data is ready
    final_dataset()
  })
})

Key Changes Explained:

  1. Reactive Data Storage:

    • We use reactiveVal(NULL) to hold our dataset. Initially, it’s empty, so the table won’t render (thanks to req(final_dataset()) in the table renderer).
  2. Non-Blocking Delay:

    • Instead of Sys.sleep() (which freezes the entire app), we use invalidateLater(30000) inside a temporary observer. This tells Shiny to wait 30 seconds, then run the dataset-reading logic without locking up the UI.
    • cancelObserver() ensures this delayed task only runs once per Submit click.
  3. Input & Data Validation:

    • req(input$file) makes sure the Submit action only triggers if a file is selected.
    • req(final_dataset()) guarantees the table stays hidden until valid data is loaded.
  4. Error Handling:

    • Wrapping the dataset logic in tryCatch() prevents the app from crashing if there’s an issue loading files, and shows a clear error notification to the user.

内容的提问来源于stack exchange,提问作者Kasia Kulma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:29:14