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

Shiny下拉框加载文件夹文件并绘制mw列直方图的技术问询

Shiny App to Load CSV Files & Plot Histogram of "mw" Column

Hey there, let's put together that Shiny app you need! You want to browse CSV files in a specific folder (following that date+ARB.csv naming pattern), pick one from a dropdown, and plot a histogram of the mw column. Here's a complete, working solution with breakdowns of each part:

Full Working Code

library(shiny)

# Define the user interface
ui <- fluidPage(
  titlePanel("CSV File Histogram Viewer"),
  sidebarLayout(
    sidebarPanel(
      # Dropdown menu to select a CSV file from your target folder
      selectInput(
        inputId = "selected_file",
        label = "Choose a CSV File:",
        # Filter files to only those ending with ARB.csv, match your naming format
        choices = list.files(path = "C:/R_myfirstT/data", pattern = "ARB\\.csv$", full.names = FALSE)
      )
    ),
    mainPanel(
      # Output area for the histogram
      plotOutput(outputId = "mw_histogram")
    )
  )
)

# Define server logic to load data and render plots
server <- function(input, output) {
  
  # Reactive expression to dynamically load the selected file
  selected_data <- reactive({
    # Wait until a file is selected before running
    req(input$selected_file)
    # Build the full path to the selected file
    file_path <- file.path("C:/R_myfirstT/data", input$selected_file)
    # Load the CSV (adjust read.csv parameters if your files use different delimiters)
    read.csv(file_path, stringsAsFactors = FALSE)
  })
  
  # Render the histogram of the "mw" column
  output$mw_histogram <- renderPlot({
    data <- selected_data()
    
    # Quick check to make sure the "mw" column exists in the selected file
    if (!"mw" %in% colnames(data)) {
      stop("Oops! This file doesn't have a 'mw' column.")
    }
    
    # Create the histogram with custom labels and styling
    hist(data$mw, 
         main = paste("Histogram of 'mw' Values:", input$selected_file),
         xlab = "mw Value",
         col = "lightsteelblue",
         border = "white",
         breaks = "FD") # Uses Freedman-Diaconis rule for bin width
  })
}

# Launch the Shiny app
shinyApp(ui = ui, server = server)

Key Features Explained

  • Folder & File Filtering: The pattern = "ARB\\.csv$" in list.files() ensures we only show files that end with ARB.csv (matching your naming convention). Just make sure to replace "C:/R_myfirstT/data" with your actual folder path.
  • Reactive Data Loading: The selected_data() reactive expression only runs once a file is selected (thanks to req(input$selected_file)), so it won't try to load data before a choice is made.
  • Error Prevention: We check if the mw column exists before plotting to avoid unexpected crashes if a file is missing that column.
  • Customizable Plot: The histogram includes a dynamic title with the selected filename, plus styled colors and a smart bin width calculation.

Extra Tips for Improvement

  • Auto-Update File List: If you want the dropdown to refresh automatically when new files are added to the folder, move the list.files() call into the server and use updateSelectInput:
    server <- function(input, output, session) {
      # Refresh file list when the app starts (or add a button to trigger manually)
      observe({
        files <- list.files(path = "C:/R_myfirstT/data", pattern = "ARB\\.csv$", full.names = FALSE)
        updateSelectInput(session, "selected_file", choices = files)
      })
      
      # Keep the rest of your server logic (selected_data and renderPlot) here
    }
    
  • Adjust CSV Loading: If your files use semicolons instead of commas, replace read.csv with read.delim(sep = ";").
  • Add More Controls: You could add a slider to adjust histogram bin width, or a checkbox to show density curves, by adding more inputs to the sidebar and updating the hist() call.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:54:51