R 如何从按日期排序的基于目录的数据库中导出.csv?
第一次闪亮的用户,我想我有这个设置的大部分。我的代码从基于目录的数据库中检索某些数据。输出是一个csv文件,我希望能够直接从shiny下载 下面的代码非常基本:R 如何从按日期排序的基于目录的数据库中导出.csv?,r,database,export,shiny,R,Database,Export,Shiny,第一次闪亮的用户,我想我有这个设置的大部分。我的代码从基于目录的数据库中检索某些数据。输出是一个csv文件,我希望能够直接从shiny下载 下面的代码非常基本: 它接受输入的日期,分离D/M/Y并创建搜索词 还创建了表示不同文件的某些关键字以进行搜索 它搜索特定文件夹并检索CSV。文件 然后,它仅从该文件检索标记为输入之一的数据 此数据应可通过Shining app以CSV或XLS格式下载/并输出 我的意见是: inputId=“NaVDate”=这是日期(用于搜索和检索.csv) inputI
输入的日期,分离D/M/Y并创建搜索词
输入之一的数据
并输出
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 <- 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
它包含多个按日期排序的文件
C:/Users/dell Optiplex/Desktop/EquilibriumWeeklyMacro/2016/03/
在那里我有一个CSV文件叫做
XCP4P487FOFFET_20160315.CSV
请注意FOFFET_
和20160315
结尾
如果我在闪亮应用程序框中输入2016-03-15
,以下闪亮应用程序应该能够从该文件中检索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)
库(闪亮)
#XCP4P487FOFFET_20160315.CSV
#C:/Users/dell Optiplex/Desktop/equiriumweeklymacro/2016/03/
ui这里是一个示例程序,它带有一个操作按钮,只有在单击该按钮时才会写入csv。不打算将其集成到您的程序中,因为您的程序太大,而且我没有您的数据,但您应该能够处理以下问题:
library(shiny)
library(ggplot2)
u <- shinyUI(fluidPage(
titlePanel("Simple Shiny Input"),
sidebarLayout(position = "left",
sidebarPanel(
actionButton("savebutton", "Save Data"),
sliderInput("cnt","Count:",100,1000,500,5)
),
mainPanel(plotOutput('stdplot'))
)
))
s <- shinyServer(function(input, output)
{
histdata <- reactive({
set.seed(1234)
df <- data.frame(x=rnorm(input$cnt))
} )
output$stdplot = renderPlot({
qplot(data=histdata(),x,fill=I("blue"),bins=30)
})
savedata <- observe({
input$savebutton
if (input$savebutton>0){
write.csv(histdata(),"histo.csv")
}
})
})
shinyApp(ui=u,server=s)
库(闪亮)
图书馆(GG2)
uShiny有一个用于此的小部件,称为downloadButton
,可以找到文档
我已集成到您的应用程序中:
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)
库(闪亮)
uiwrite.csv
有什么问题?或者你所说的“下载”到底是什么意思。write.csv可以工作,但是这个闪亮的应用程序会被不熟悉R的人使用。我需要在我闪亮的应用程序中有一个可点击的按钮来导出6个csv文件。这与数据库
没有真正的关系。也许data.frame
嗨,迈克,谢谢你的回答。虽然有帮助,但保存数据代码与输入的大小有关。我不确定这正是我需要的。一两天后再来,如果你有时间,我会悬赏的。这是一个漫长的计划,但每件事都做了6次。我已将代码缩短为只包含1个变量,而不是6个。没问题,尽管我不确定您的语句“我们的保存数据代码与您的一个输入的大小相关”是什么意思。猜猜看,我会说你的意思是histdata
对input$cnt
的依赖性。然而,这是偶然的,本例的重点只是说明如何使用actionButton
触发数据帧(或多个)的保存。我得到以下错误:正在侦听http://127.0.0.1:6132 警告:[:类型为“closure”的对象不是子可附加堆栈跟踪中的错误(最里面的第一个):44:粘贴43:server[#9]4:3:do.call 2:print.shinny.appobj 1:timestrip中的打印错误[1]:类型为“closure”的对象不可子集
您认为我是否正确地使用了反应式
函数和上面的输入$FundID/input$NaVDate
?很好,我自己从未使用过下载按钮
我会尝试一下。@AlexBădoi是的,看起来您使用的反应式表达式不正确,请ee my edits.mkemp看起来您删除了远期基金
向量,因此进入输出$downloadData的输出实际上不再存在于代码中。另外还有一些额外的括号留在}
。我不确定这是否完全正确。
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)