R Shiny:字符到日期格式

R Shiny: Character to date format

我正在尝试开发一个能够接收 .csv 文件和 运行 预测的 Shiny App。 首先,我需要导入 2 个 .csv 文件并将日期列从字符格式转换为日期格式。 他们将数据框转换为一个 tsibble 对象以 运行 预测。

在 R 脚本中,这很简单,我将导入文件,他们只需使用:

data$Date <- ymd(data$Date)
dados = as_tsibble(data,index = Date)

但这不起作用。我尝试了很多不同的方法,包括代码中的方法,但我不断得到 error evaluation nested too deeply: infinite recursion/options (expressions=)?,它应该是一个包含 str() 信息的数据框。

预期结果是 table,其中包含如下信息:

> 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")
                )
              )

      )
    )
  )
)

server.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)
})

基本上,我使用日期列创建了一个辅助向量 (datas),并将其转换为日期格式。他们,我将数据 table 复制到另一个辅助变量 (df) 并使用 datas 向量更新了日期列。 最后,我将 df 转换为一个 tsibble 对象。 希望对您有所帮助!