【问题标题】:as.Date returns number when working with Shinyas.Date 在使用 Shiny 时返回数字
【发布时间】:2020-02-13 06:52:42
【问题描述】:

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

我该怎么办?

【问题讨论】:

标签: r date shiny as.date


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 2021-09-07
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多