我正在尝试开发一款能够接收.csv文件并运行预测的Shiny应用程序。首先,我需要导入2.csv文件,并将Date列从字符转换为日期格式。他们,将数据帧转换为tsible对象来运行预测。
在R脚本中,这很简单,我会导入文件,他们只需使用:
data$Date <- ymd(data$Date)
dados = as_tsibble(data,index = Date)
但这行不通。我尝试了许多不同的方法,包括代码上的方法,但我一直得到error evaluation nested too deeply: infinite recursion/options (expressions=)?
,它应该是一个包含str()
信息的数据帧。
预期的结果是一个包含以下信息的表格:
> str(data)
'data.frame': 975 obs. of 13 variables:
$ Date : Date, format: "2017-05-01" "2017-05-02" "2017-05-03" "2017-05-04" ...
$ Demand : int 122 124 113 124 126 114 100 121 118 135 ...
我想不出任何解决办法。有什么建议吗?
我的代码:
**UI.R**
library(shiny)
library(shinydashboard)
library(fpp3)
library(fasster)
dashboardPage(
dashboardHeader( title = "DEMO Forecast"),
dashboardSidebar(
sidebarMenu(
menuItem("Data",tabName = "Data"),
menuItem("Analysis", tabName = "Analysis")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "Data",
fluidRow(
box(title = "Enter the ED demand file",
#INPUT
fileInput("file", label = "Choose .csv file",
multiple = FALSE, accept = ".csv",
buttonLabel = "Browse")
),
box(title = "Enter the ED external data file",
#INPUT
fileInput("file_extra", label = "Choose .csv file",
multiple = FALSE, accept = ".csv",
buttonLabel = "Browse")
)
),
fluidRow(
#OUTPUT
box(width = 12, tableOutput("table_data"))
)
),
tabItem(tabName = "Analysis",
fluidRow(
box(title = "Date format",
#INPUT
selectInput ("date_format", h5("Date format"),
choices = list("ymd", "mdy", "dmy", "ydm")),
h5("y = year, m = month, d = day"),
),
#OUTPUT
box(title = "Accuracy Measures", width = 12,
tableOutput("table_erro")
)
)
)
)
)
)
服务器.R
library(shiny)
library(shinydashboard)
library(fpp3)
library(fasster)
shinyServer(
function(input,output){
#Import ED demand data
data = reactive({
file1 = input$file
if(is.null(file1)){return()}
read.csv(file1$datapath,header = TRUE, sep = ",",
stringsAsFactors = FALSE, dec = ".", na.strings = " ")
})
data <- eventReactive(input$date_format,{
switch (input$date_format,{
"ymd" = ymd(data()$Date)
"ydm" = ydm(data()$Date)
})
})
#Import extra data
data_extra = reactive({
file_extra = input$file_extra
if(is.null(file_extra)){return()}
read.csv(file_extra$datapath, header = TRUE, sep = ",",
stringsAsFactors = FALSE, dec = ".", na.strings = " ")
})
output$table_data = renderTable({
if(is.null(data())){return()}
str(data())
})
}
)
在多次尝试失败后,我使用以下代码解决了这个问题:
#Change date format
data_final = reactive({
datas = data()$Date #auxiliary vector
datas = ymd(datas)
df = data() #auxiliary data frame
df$Date = datas
as_tsibble(df, index = Date)
})
基本上,我用Date列创建了一个辅助向量(datas
(,并将其转换为日期格式。然后,我将数据表复制到另一个辅助变量(df
(,并使用datas
向量更新Date列。最后,我将df
转换为tsible对象。我希望这能有所帮助!