在 R shiny 中使用 lubridate 设置日期格式

Set date format with lubridate in R shiny

我试图在闪亮的应用程序中设置 csv 输入的日期格式,但我真的迷路了。我见过很多使用 switch 的方法,但我是这样的:

 ui <- fluidPage(
   theme = shinytheme("sandstone"),             

   # Navigation bar
   navbarPage("Let's do some analysis!",

              # Upload Tab
              tabPanel("Upload",
                       sidebarPanel(
                             fileInput(inputId = "data", label = "Choose CSV Data Set",
                                       accept = c(                                                           
                                         "text/csv",
                                         "text/comma-separated-values,text/plain",
                                         ".csv")
                             ), #fileInput - data

                             checkboxGroupInput("sep1", h5("Separator"), choices = list(";" = ";" , "," = ",")),

                             fileInput(inputId = "stopwords", label = "Choose CSV Stopwords File",
                                       accept = c(
                                         "text/csv",
                                         "text/comma-separated-values,text/plain",
                                         ".csv")
                             ),#fileInput - stopwords

                             checkboxGroupInput("sep2", h5("Separator"), choices = list(";" = ";" , "," = ",")),

                             checkboxGroupInput("date", h5("Date format"), choices = list("ymd" = 1 , "mdy" = 2, "dmy" = 3, "ydm" = 4)),
                             h5("y = year, m = month, d = day"),

                           ), #sidebarPanel - Upload
                           mainPanel(
                             DT::dataTableOutput("data"),
                             h5("Please make sure you selected each separator accurately and take a look at how 'File.Date' is displayed!"),
                             br(),
                             DT::dataTableOutput("stopwords")
                           ) #maiPanel - Upload
              ), #tabPanel - Upload

              # Analizing & Modeling Tab 
              tabPanel("Analizing & Modeling",
                       fluidRow(
                         column(6,
                                visOutput('LDAvis')),
                         column(6,
                                DT::dataTableOutput("top_abstracts"), downloadButton("downloadData", "Download Top Abstracts per topic"))

                       )#fluidRow

              )#tabPanel - Analizing & Modeling 

   ) # navbarPage
 ) #fluidPage

 server <- function(input, output, session) {

   og_data <- eventReactive(input$data,{
     inFile1 <- input$data
     if (is.null(inFile1)) {
       return(NULL)
     } else {
       return(read.csv(inFile1$datapath, sep = input$sep1, header = T, stringsAsFactors = F)) 
     }
   }) # eventReactive - og_data

   og_data <- eventReactive(input$date,{
     switch (input$date,
       1 = ymd(og_data()$File.Date),
       2 = mdy(og_data()$File.Date),
       3 = dmy(og_data()$File.Date),
       4 = ydm(og_data()$File.Date)
     ) 
   }) # eventReactive - innog_data with accurate date format

   stopwords <- eventReactive(input$stopwords,{
     inFile2 <- input$stopwords
     if (is.null(inFile2)) {
       return(NULL)
     } else {
       return(read.csv(inFile2$datapath, sep = input$sep2, header = F, stringsAsFactors = F)) 
     }
   }) # eventReactive - stopwords

   # Upload tab

   output$data <- DT::renderDataTable({
     DT::datatable(og_data(), options = list(pageLength = 1, lengthMenu = c(1,3)))
   })

   output$stopwords <- DT::renderDataTable({
     DT::datatable(stopwords())
   })
 } #server function

有人能告诉我我做错了什么吗?

函数参数名称,包括 switch 的选项,必须是有效的对象名称。标准对象名称不以数字开头。所以 1 = ymd(og_data()$File.Date) 的问题是 1 不是有效的对象名称。

x = 1
switch(x, 1 = "hi")
# Error: unexpected '=' in "switch(x, 1 ="

您可以在数字周围使用反引号使它们成为非标准名称:

x = 1
switch(x, `1` = "hi")
# [1] "hi"

或者(首选),您可以使用同样具有描述性的标准变量名称。这样更清晰,更不容易出错。

...
choices = list("ymd", "mdy", "dmy" , "ydm")
...
switch (input$date,
       "ymd" = ymd(og_data()$File.Date),
       "mdy" = mdy(og_data()$File.Date),
       "dmy" = dmy(og_data()$File.Date),
       "ydm" = ydm(og_data()$File.Date)
     ) 
...