R闪亮:字符至日期格式

问题描述 投票:0回答:1

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

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

      )
    )
  )
)

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())
    })

  }
)
r shiny shinydashboard lubridate
1个回答
0
投票

经过多次失败的尝试,我设法使用以下代码解决了这个问题:

#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转换为一个斜线对象。希望对您有所帮助!

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.