请求R代码:在R Markdown的flexdashboard中实现Excel数据交互散点图
Solution for Flexdashboard with Selectable Scatter Plots
Hey there! I've put together a complete R Markdown script that builds exactly the flexdashboard you need—importing your Excel data, letting you pick any of the product columns (2-5), and generating a scatter plot of Day vs. the selected product.
Full R Markdown Code
--- title: "General plots" author: "Aravind" date: "April 1st,2018" output: flexdashboard::flex_dashboard runtime: shiny # Required for interactive input components --- ```{r setup, include=FALSE} # Load required packages library(flexdashboard) library(shiny) library(readxl) library(ggplot2) # Import Excel data - replace with your actual file path data <- read_excel("your_excel_file.xlsx")
Scatter Plot: Day vs. Selected Product
Select Product Column
selectInput( inputId = "selected_product", label = "Choose a Product to Plot:", choices = colnames(data)[2:5], # Targets columns 2-5 (product1 to product4) selected = colnames(data)[2] # Default to first product column )
Plot Output
renderPlot({ # Use .data[[...]] to dynamically reference the selected column in ggplot2 ggplot(data, aes(x = Day, y = .data[[input$selected_product]])) + geom_point(size = 3, color = "#2c3e50") + labs( title = paste("Day vs.", input$selected_product), x = "Day", y = input$selected_product ) + theme_minimal() })
### Key Notes: - **Package Dependencies**: Make sure you have all required packages installed first. Run `install.packages(c("flexdashboard", "shiny", "readxl", "ggplot2"))` if you haven't already. - **File Path**: Don't forget to replace `"your_excel_file.xlsx"` with the actual path to your Excel file (absolute or relative). - **Dynamic Column Reference**: The `.data[[input$selected_product]]` syntax is the recommended way to dynamically access columns in ggplot2, avoiding any evaluation errors. - **Runtime Setting**: Adding `runtime: shiny` to the YAML header is critical—it enables the interactive `selectInput` component in your flexdashboard. 内容的提问来源于stack exchange,提问作者Praneeth
相关产品推荐
相关产品推荐

