[英]R Shiny: How to read different CSV file based of user input?
I have a flexdashboard shiny app I am working on and trying to create a reactive function that imports different datasets (through read.csv()) based on the user input.我有一个 flexdashboard shiny 应用程序,我正在开发并尝试创建一个反应式 function,它根据用户输入导入不同的数据集(通过 read.csv())。
I have 2 empty csv files in a folder called "Test".我在一个名为“Test”的文件夹中有 2 个空的 csv 文件。 One is called Oct 2021 and the other is called Nov 2021.
一个称为 2021 年 10 月,另一个称为 2021 年 11 月。
Rather than loading in all the csv files in - I would like the user to choose the name of the file and have it load in.而不是加载所有 csv 文件 - 我希望用户选择文件的名称并加载它。
Here is my code这是我的代码
---
title: "Test"
output:
flexdashboard::flex_dashboard:
runtime: shiny
---
Page 1
=====================================
Inputs {.sidebar}
-------------------------------------
```{r}
library(flexdashboard)
library(shiny)
library(DT)
library(readr)
```
```{r}
selectInput("input_type","Select Month:", c("Oct 2021", "Nov 2021"))
```
Column
-----------------------------------------------------------------------
### DATA OUTPUTS HERE
```{r}
#Prepare data here
data <- reactive({
tmp <- read.csv(input$input_type$"~Test/")
tmp
})
```
```{r}
renderDataTable(
datatable(
data()
)
)
```
I thought this would work but I get an error "$ operator is invalid for atomic vectors"我认为这可行,但我收到一个错误“$ 运算符对原子向量无效”
input$input_type
is the name of the file, but to specify the complete path we need to paste some strings together. input$input_type
是文件的名称,但要指定完整路径,我们需要将一些字符串粘贴在一起。 read.csv
can be used like this: read.csv
可以这样使用:
read.csv(paste0('~/Test/', input$input_type, '.csv'))
Sample Shiny App:样品 Shiny 应用程序:
library(shiny)
library(DT)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("input_type","Select Month:", c("Oct 2021", "Nov 2021")),
actionButton('load_csv', 'Load csv')),
mainPanel(dataTableOutput('dt'))
)
)
server <- function(input, output, session) {
data <- eventReactive(input$load_csv, {
read.csv(paste0('~/Test/', input$input_type, '.csv'))
})
output$dt <- renderDataTable({
req(data())
datatable(
data()
)}
)
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.