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

Shiny应用问题:文件上传后表格渲染与下载功能失效

Fixing Your Shiny App: Table Rendering & Download Issues

Let's break down the issues in your code and fix them step by step. The core problem is indeed missing reactive logic—your company_data isn't set up to react to changes in user inputs, plus there are a few small syntax/usage errors blocking functionality.

Key Fixes Needed:

  • Wrap filtered data in a reactive expression: This ensures the data updates automatically when the user uploads a new file or modifies the text inputs.
  • Correct data frame column naming: When converting the text lines to a data frame, you need to explicitly name the column so str_detect knows what to filter.
  • Fix download handler logic: The download handler can't directly access company_data (it's not in scope); you need to call the reactive variable instead, plus correct the write.csv parameters.

Full Corrected Code:

library(shiny)
library(tidyverse)

ui <- fluidPage(
  titlePanel("Download data"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose .txt file", multiple = F, accept = c(".txt")),
      textInput('energy_co', 'Name of energy company'),
      textInput('asset', 'name of Asset Manager'),
      downloadButton("downloadData", "Download")
    ),
    mainPanel(
      tableOutput("table")
    )
  )
)

server <- function(input, output, session) {
  options(shiny.maxRequestSize=30*1024^2)
  
  # Reactive expression to handle data filtering
  filtered_data <- reactive({
    req(input$file1, input$energy_co, input$asset) # Wait for all inputs to be present
    
    data <- read_lines(input$file1$datapath)
    text_df <- tibble(terms = data) # Explicitly name the column
    
    text_df %>%
      filter(str_detect(terms, input$asset)) %>%
      filter(str_detect(terms, input$energy_co)) %>%
      distinct(.)
  })
  
  # Render table using the reactive data
  output$table <- renderTable({
    filtered_data()
  })
  
  # Fix download handler to use reactive data
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("filtered_results_", Sys.Date(), ".csv", sep = "") # More meaningful filename
    },
    content = function(file) {
      write.csv(filtered_data(), file, row.names = FALSE)
    }
  )
}

shinyApp(ui, server)

What Changed:

  1. Reactive filtered_data: This wraps all the data reading and filtering logic. The req() function ensures the code only runs once the user has uploaded a file and entered both search terms—prevents errors from missing inputs.
  2. Column Naming: tibble(terms = data) creates a data frame with a column named terms, which matches what you're using in str_detect.
  3. Download Handler:
    • We call filtered_data() inside the download handler to access the latest filtered data.
    • Fixed the write.csv parameter (was using file1 instead of the correct file argument).
    • Updated the filename to be more descriptive and include the current date.

Now when you upload a TXT file, enter both terms, the table will render automatically, and the download button will export the filtered results correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 18:17:58