首页 > 解决方案 > as.Date 在使用 Shiny 时返回数字

问题描述

我正在使用谷歌表单来创建订阅表单。由于谷歌表单中使用的“日期”类型问题对于与技术接触很少的人来说有点混乱(要订阅的人是有风险的青少年),我选择将出生年月日分成三个不同的问题,然后联合起来,然后使用 as.Date。我会使用 fileInput 和 renderTable 上传由谷歌表单生成的 .csv 文件以显示数据。

此代码在控制台中完美运行

# read the file from my computer
df <- read_csv("~APAMI/R/base.csv")
    colnames(df)<- c("day", "month", "year")
    dn <- as.Date(with(df, paste("year", "month", "day",sep="-")), "%Y-%m-%d")
    df$dn<-dn
    df

但是当我将此代码应用于闪亮时,日期返回一个数字,而不是日期。我尝试使用 lubridate 并使用 "as.character" 而不是 "paste" 。我尝试在 as.Date 中使用“origin”函数以及 POSIXc 选项。得到了同样的结果。

我真的不能在表格中使用“日期”问题,因为人们很难填写问卷,并且不断地输入错误的出生日期。

用户界面

library(shiny)
library(readr)
library(date)

shinyUI(fluidPage(

  titlePanel("Triagem da Inscrição para APAMI"),

  sidebarLayout(
    sidebarPanel(
      fileInput('datafile', 'Base de Dados')
    ),

    mainPanel(
       tableOutput("tabela")
    )
  )
))

服务器

library(shiny)
library(readr)
library(date)

shinyServer(function(input, output) {

  output$tabela <- renderTable ({

    inFile <- input$datafile 

    if(is.null(inFile))
      return(NULL)

    df <- read_csv(inFile$datapath)
    colnames(df)<- c("year", "month", "day")
    dn <- as.Date(with(df, paste(year, month, day,sep="-")), "%Y-%m-%d")
    df$dn<-dn
    df
  })
})

我应该怎么办?

标签: rdateshinyas.date

解决方案


我使用了函数“strftime”。顺便说一句,我使用“read.csv”而不是“read_csv”,因为在运行您的代码时出现错误。

dn <- strftime(paste(df$year, df$month, df$day,sep="-"), "%Y-%m-%d")

推荐阅读