簡體   English   中英

在 R 中使用 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)
     ) 
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM