as.Date在使用Shiny时返回数字 [英] as.Date returns number when working with Shiny

查看:74
本文介绍了as.Date在使用Shiny时返回数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google表单来创建订阅表单.由于google表单中使用的日期"类型问题对于很少接触技术的人(将要订阅的人是处于危险中的青少年)有些困惑,因此我选择将出生的年,月和日分为三部分不同的问题,然后团结起来,然后使用as.Date.我会使用fileInput和renderTable上传由Google表单生成的.csv文件,以显示数据.

I'm working with google forms to create a subscription form. As the "date" type question used in google forms is a bit confusing for people with little contact with technology (the people that are going to subscribe are at-risk teenagers), I chose to separate year, month and day of birth in three different questions and then unite then using as.Date. I would upload the .csv file generated by google forms using fileInput and renderTable to show the data.

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

This code works perfectly in the console

# 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"功能.得到了相同的结果.

But when I apply this code to shiny the date returns a number, not the date. I've tried to use lubridate and to use "as.character" instead of "paste" . I've tried to use the "origin" function in as.Date, as well as the POSIXc option. Got the same result.

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

I really can't use the "date" question in forms, because people are having a hard time filling the questionnaire and are putting the wrong birth date constantly.

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

我该怎么办?

推荐答案

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

I used the function "strftime". By the way, I used "read.csv" instead of "read_csv", because I got an error when running your code.

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

这篇关于as.Date在使用Shiny时返回数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆