as.Date returns number when working with Shiny

徘徊边缘 提交于 2021-01-29 07:50:46

问题


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

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

SERVER

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

What should I do?


回答1:


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


来源:https://stackoverflow.com/questions/58414187/as-date-returns-number-when-working-with-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!