Shiny应用功能求助:上传数据展示与提示逻辑实现问题
Let's walk through the fixes needed to get your buttons working as expected. The core issue is that your current logic doesn't properly handle the "Show New data" button's behavior—specifically checking for uploaded data and triggering the right display updates. Here's the revised code with explanations:
Full Working Code
library(tidyverse) library(shiny) library(shinyWidgets) library(DT) options(shiny.maxRequestSize=30*1024^2) # Sample recommendation data recommendation <- tibble(Account = c("Axis", "HSBC", "SBI", "ICICI", "Bandhan Bank", "Axis", "HSBC", "SBI", "ICICI", "Bandhan Bank"), Product = c("Bank", "FBB", "FBB", "FBB","FBB","SIMO", "SIMO", "SIMO", "SIMO", "SIMO"), Region = c("North", "South", "East", "West", "West", "North", "South", "East", "West", "West"), Revenue = c(2000, 30000, 1000, 1000, 200, 200, 300, 100, 100, 200)) ui <- fluidPage( titlePanel("Example"), sidebarLayout( sidebarPanel( fileInput("Upload", "Upload your own Data"), actionButton("Example", "Show Example Data instead"), actionButton("data", "Show New data") ), mainPanel( column(DT::dataTableOutput('showdata'), width = 12), ) ) ) # Server Logic server <- function(input, output) { # Reactive value to store uploaded data (only updated when user uploads a file) uploaded_data <- reactiveVal(NULL) # Reactive value to store the data/tip we want to display in the main panel display_data <- reactiveVal(NULL) # Handle file upload: save the uploaded CSV to uploaded_data observeEvent(input$Upload, { tmp <- read.csv(input$Upload$datapath) uploaded_data(tmp) }) # Show example data when the first button is clicked observeEvent(input$Example, { display_data(recommendation) }) # Handle "Show New data" button click observeEvent(input$data, { current_upload <- uploaded_data() # Check if user has uploaded data yet if (is.null(current_upload)) { # Show a prompt if no data is uploaded display_data(tibble(Message = "please upload data first")) } else { # Show the uploaded data if it exists display_data(current_upload) } }) # Render the display data in the DT table output$showdata = renderDT({ req(display_data()) # Only render if there's something to show display_data() }, rownames = FALSE, class = "display nowrap compact", filter = "top", options = list( scrollX = TRUE, scrollY = TRUE, autoWidth = FALSE)) } # Run the application shinyApp(ui = ui, server = server)
Key Changes Explained
- Split reactive values: We use two separate
reactiveVals:uploaded_data: Stores the user's uploaded file (only updated when a file is uploaded)display_data: Controls what gets shown in the main panel (example data, uploaded data, or the prompt)
- "Show New data" button logic: Now this button checks if
uploaded_datais empty. If it is, it sends a prompt message (as a tibble so it shows up in the DT table). If data exists, it loads the uploaded data intodisplay_data. - Controlled display: The
renderDTnow depends ondisplay_data, so whenever you click either button, the table automatically refreshes with the correct content. - Initial state: The table stays empty until you click one of the buttons, which is a cleaner user experience.
This setup ensures each button behaves exactly as you intended:
- Clicking "Show Example Data instead" loads the sample
recommendationdata. - Clicking "Show New data" either shows your uploaded file or prompts you to upload data first.
内容的提问来源于stack exchange,提问作者SAJ
相关产品推荐
相关产品推荐

