as.Date使用Shiny时返回数字

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

我正在使用Google表单来创建订阅表单。由于google表单中使用的“日期”类型问题对于很少接触技术的人(将要订阅的人是处于危险中的青少年)有些混乱,因此我选择将出生的年,月和日分为三部分不同的问题,然后团结起来,然后使用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以及POSIXc选项中使用“ origin”功能。得到了相同的结果。

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

UI

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个回答
0
投票

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

dn <- strftime(paste(df$year, df$month, df$day,sep="-"), "%Y-%m-%d")
© www.soinside.com 2019 - 2024. All rights reserved.