r - 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
})
})
我应该怎么办?
解决方案
我使用了函数“strftime”。顺便说一句,我使用“read.csv”而不是“read_csv”,因为在运行您的代码时出现错误。
dn <- strftime(paste(df$year, df$month, df$day,sep="-"), "%Y-%m-%d")
推荐阅读
- mysql - 我的数据库存储在 AWS RDS 上。不幸的是,我的 aws rds 实例已被删除。它存储在我的计算机中的哪个位置
- json - 如何集成 localStorage 以保存当前浏览器会话的数据(在数组中)?
- pandas - 在 CSV 文件上使用 pandas,如果“日期”列的格式为“MM/DD/YYYY”,如何按“月”过滤数据集
- microsoft-graph-api - 无法将“In-Reply-To”参数传递给 Microsoft Graph sendMail
- sql - 存储过程性能问题 临时表中有 150 万条记录
- laravel - 127.0.0.1:8000 在 laravel 项目中不断加载
- asp.net - 发送电子邮件在本地工作而不是在服务器上
- node.js - “查询值必须是一个数组”...?
- java - 在 Java 中,通过 new 运算符是否可以创建具有相同字符串内容的不同对象?
- javascript - document.querySelectorAll("[value='a']") 的返回值在元素值被修改后不会改变