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

梅拉尼亚(Melania)CB

我试图在闪亮的应用程序中设置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

有人可以告诉我我在做什么错吗?

格雷戈尔·托马斯(Gregor Thomas)

函数参数名称(包括的选项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)
     ) 
...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用lubridate在R中解析多种格式的日期

尝试使用 lubridate() 在 Base R 中以日期/时间格式添加更多轴标记

在R中使用lubridate包以小时和分钟格式显示时间:数据帧问题

使用R中的lubridate从日期确定季节

如何在R中使用lubridate将周和年列转换为日期列

在 R 中,取一个日期字符串并将其转换为日期时间格式(使用 lubridate)

如何在R中使用implyr格式化日期?

在 R 中使用 format() 更改日期格式

在 r 中使用 posixct 更改日期格式

在 R 中使用 dput 后调整日期格式

在R中使用lubridate包对同一日期字符串使用不同的结果

R Shiny-在Shiny模块中使用'session $ sendCustomMessage'设置输入值

将字符格式转换为r lubridate中的日期格式,前20年而不是19年

使用 R 包 lubridate 转换字符日期无法解析

使用Lubridate将R中的字符列转换为日期

使用 lubridate 包在 R 中合并日期及其相关值

在R中将日期格式设置为年月

将日期从字符转换为可以在R中的时间序列分析中使用的日期格式

在R中使用lubridate按年份计数观察值

如何在R管道中使用lubridate解析功能?

在 R Shiny 中使用 renderTable 时如何格式化数字行输出?

在R Shiny daterangeinput中更改日期格式

如何根据CSS的值在R Shiny中使用CSS选择性地设置其样式?

在 R 中使用 data.table::fread 读取非 ISO 格式的日期列

如何在R中使用正则表达式转换日期格式?

如何使用ggplot2在R中使用轴标签和旋转格式设置雷达图

在R中使用Shiny进行绘图

在R Shiny中使用a()时的尾随空格

在函数中使用R Shiny的progressBar