Shiny 的新用户,我想我已经完成了大部分设置。我的代码从基于目录的数据库中检索某些数据。输出是一个 csv 文件,我希望能够直接从 shiny 下载该文件。
下面的代码是非常基本的:
- 它采用
输入
日期并分隔 D/M/Y 并创建搜索词 - 还创建了代表不同文件的某些关键字以供搜索
- 它搜索特定文件夹并检索 CSV。文件
- 然后从该文件中仅检索标记为
输入
之一的数据 - 此数据应该可以 CSV 或 XLS 格式通过 Shiny 应用程序下载/
输出
我的输入是:
inputId = "NaVDate"
= 这是日期(用于搜索和检索 .csv)
inputId = "FundID"
= 这是在第 4 步中用来对数据进行排序的 ID
输出是:
Forwards_Fund
这些已经包含在下面的代码中:
UI.R
library(shiny)
ui <- fluidPage(
# Date input by user
dateInput(inputId = "NaVDate", label = "Select NaV Date",
format = "yyyy-mm-dd", startview = "month", weekstart = 0),
#
textInput(inputId = "FundID", label = "Enter Fund ID (Numeric Only)", value = "", width = NULL, placeholder = NULL),
downloadButton("downloadData", "Download")
)
SERVER.R
server <- function(input, output) {
# extract inputed month / year / day and vectorize ( inputed in YYYY-MM-DD format)
timestrip <- reactive({unlist(strsplit(as.character(input$NaVDate), split = ""))})
year <- paste(timestrip[1], timestrip[2], timestrip[3], timestrip[4], sep = "")
month <- paste(timestrip[6], timestrip[7], sep = "")
day <- paste(timestrip[9], timestrip[10], sep = "")
Date = paste(year, month, day, sep = "")
# create directory search criteria for the required files - eg.("XCP4P487FOFFET_20160315.CSV")
Forwards = paste0("*FOFFET_", Date, ".CSV$")
# create main directory file path
file_path <- paste0("C:/Users/dell Optiplex/Desktop/Equilibrium Weekly Macro/", year,"/", month)
# Search through file_path for criteria and create individual .csv filepath
Forwards_CSV_path = list.files(file_path,
pattern = Forwards, full.names=TRUE,
ignore.case=TRUE)
# Read Data from CSV files
Forwards_data = do.call(rbind, lapply(Forwards_CSV_path, function(x) read.csv(x, sep = ";", stringsAsFactors = FALSE)))
# Extract data from the inputed fund code (input$FundID)
Forwards_Fund <- reactive({
Forwards_data[which(Forwards_data$Fund.Code == input$FundID),]
})
## download above data from shiny app
output$downloadData <- downloadHandler(
filename = function() {
paste('data-', Sys.Date(), '.csv', sep='')
},
content = function(con) {
write.csv(Forwards_Fund(), con)
}
)
}
shinyApp(server = server, ui = ui)
错误
Listening on http://127.0.0.1:6132
Warning: Error in [: object of type 'closure' is not subsettable
Stack trace (innermost first):
44: paste
43: server [#9]
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: print
Error in timestrip[1] : object of type 'closure' is not subsettable
编辑: - 更新
这是一款 Shiny 的应用程序,可从基于目录的数据库中检索 CSV 文件的名称。我将继续使用下载按钮对其进行升级。
我的桌面上有一个名为 EquilibriumWeeklyMacro 的文件
C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/
它包含多个按日期排序的文件
C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/2016/03/
在那里我有一个名为
的CSV文件XCP4P487FOFFET_20160315.CSV
注意 FOFFET_
和 20160315
结尾
如果我在 Shiny 应用程序框中输入 2016-03-15
,则以下 Shiny 应用程序应该能够从该文件中检索 XCP4P487FOFFET_20160315.CSV
。
除了最后一部分,下面的代码有效:
output$ForwardText = renderText({list.files(as.character(FileDirectory()),
pattern = as.character(ForwardSearchString()), full.names=TRUE,
ignore.case=TRUE)
})
我知道问题出在上面。 这是应用程序的代码:
library(shiny)
# XCP4P487FOFFET_20160315.CSV
# C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/2016/03/
ui <- fluidPage(
# Date input by user
dateInput(inputId = "NaVDate", label = "Select NaV Date",
format = "yyyy-mm-dd", startview = "month", weekstart = 0),
textOutput("ForwardText")
)
server <- function(input, output) {
DateInput <- reactive({format(as.Date(input$NaVDate), "%Y%m%d")})
FileDirectory <- reactive({paste("C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/",
substr(as.character(DateInput()), 1, 4), "/",
substr(as.character(DateInput()), 5, 6),"/", sep = "")
})
# Search String
ForwardSearchString <- reactive({paste("FOFFET_", as.character(DateInput()), ".CSV")})
# Search through file_path for criteria and create individual .csv filepath
output$ForwardText = renderText({list.files(as.character(FileDirectory()),
pattern = as.character(ForwardSearchString()), full.names=TRUE,
ignore.case=TRUE)
})
}
shinyApp(server = server, ui = ui)
最佳答案
Shiny 有一个名为 downloadButton
的小部件,可以找到文档 here .
我已经集成到您的应用中:
library(shiny)
ui <- fluidPage(
# Date input by user
dateInput(inputId = "NaVDate", label = "Select NaV Date",
format = "yyyy-mm-dd", startview = "month", weekstart = 0),
#
textInput(inputId = "FundID", label = "Enter Fund ID (Numeric Only)", value = "", width = NULL, placeholder = NULL),
downloadButton("downloadData", "Download")
)
server <- function(input, output) {
# extract inputed month / year / day and vectorize ( inputed in YYYY-MM-DD format)
timestrip <- reactive({unlist(strsplit(as.character(input$NaVDate), split = ""))})
Forwards_Fund <- reactive({
year <- paste(timestrip()[1], timestrip()[2], timestrip()[3], timestrip()[4], sep = "")
month <- paste(timestrip()[6], timestrip()[7], sep = "")
day <- paste(timestrip()[9], timestrip()[10], sep = "")
Date = paste(year, month, day, sep = "")
# create directory search criteria for the required files - eg.("XCP4P487FOFFET_20160315.CSV")
Forwards = paste0("*FOFFET_", Date, ".CSV$")
# create main directory file path
file_path <- paste0("C:/Users/dell Optiplex/Desktop/Equilibrium Weekly Macro/", year,"/", month)
# Search through file_path for criteria and create individual .csv filepath
Forwards_CSV_path = list.files(file_path,
pattern = Forwards, full.names=TRUE,
ignore.case=TRUE)
# Read Data from CSV files
Forwards_data = do.call(rbind, lapply(Forwards_CSV_path, function(x) read.csv(x, sep = ";", stringsAsFactors = FALSE)))
# Extract data from the inputed fund code (input$FundID)
Forwards_data[which(Forwards_data$Fund.Code == input$FundID),]
})
output$downloadData <- downloadHandler(
filename = function() {
paste('data-', Sys.Date(), '.csv', sep='')
},
content = function(con) {
write.csv(Forwards_Fund(), con)
}
)
}
shinyApp(server = server, ui = ui)
关于r - 如何从按日期排序的基于目录的数据库中导出 .csv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36066564/