繁体   English   中英

格式化selectInput()以向用户显示字符日期

[英]Formatting selectInput() to show character dates to the user

我目前正在构建一个闪亮的应用程序,并尝试获取一组日期以字符串形式呈现给最终用户,同时在服务器端代码中调用时仍保持其日期格式。

这里可能有一个简单的解决方案,但是不确定如何在selectInput下拉列表中获取要格式化的日期。 在我的实际用例中,使用日期滑块并不理想,因为日期不遵循公共间隔。

以下是可重现的示例:

# setup
require(lubridate)
test.dates <- as.Date(c('2014-06-01', '2014-07-01', '2014-08-01',     
                        '2014-09-01', '2014-10-01', '2014-11-01',  
                        '2014-12-01', '2015-01-01','2015-02-01', 
                        '2015-03-01', '2015-04-01'))
test.names <- as.character(paste0(month(test.dates, label = T), ' ', 
                                  year(test.dates)))
test.df <- data.frame(date = test.dates)
row.names(test.df) <- test.names

# shiny
server <- function(input, output) {

   output$table <- renderTable(test.df)

}


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("test", label = "DATE FORMAT TEST:", choices =   
                  test.df, selected = test.df[1,])
    ),
    mainPanel(tableOutput('table'))
  )
)

shinyApp(ui = ui, server = server)

我相信您会发现,在Shiny中,传递character对象比date对象容易得多。 我会简单地使用直接的character日期值,每当你需要他们date在随后的分析对象的显式转换为date对象。 下面提供了一个示例,其中下拉列表和表格均具有字符格式的日期。

require(lubridate)
myDates <- c('2014-06-01', '2014-07-01', '2014-08-01',     
             '2014-09-01', '2014-10-01', '2014-11-01',  
             '2014-12-01', '2015-01-01','2015-02-01', 
             '2015-03-01', '2015-04-01')
test.names <- as.character(paste0(lubridate::month(test.dates, label=TRUE), ' ', 
                                  year(test.dates)))
test.df <- data.frame(date = myDates)
row.names(test.df) <- test.names

# shiny
server <- function(input, output) {

    output$table <- renderTable(test.df)

}


ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            selectInput("test", label = "DATE FORMAT TEST:", choices =   
                            myDates, selected = myDates[1])
        ),
        mainPanel(tableOutput('table'))
    )
)

shinyApp(ui = ui, server = server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM