首页 > 解决方案 > 使用 Shiny/R 渲染带有日期的数据表时出错

问题描述

我有个问题。当我渲染从 Google 电子表格(通过 DT::renderDataTable)构建的数据表(通过 dataTableOutput)时,日期渲染不正确。例如:在电子表格中保存 1997/11/05 - Shiny 渲染 1997/11/04。我能做些什么呢?谢谢。

这是我的代码:

    library(shiny)
    
# Page

    ui <- fluidPage(
    
        sidebarLayout(
            sidebarPanel(
        ),
    
            # Show a plot of the generated distribution
            mainPanel(
                
                
                # Clients
                
                tabItem(
                    
                    tabName = "clients",
                    
                    width = 2000,
                    
                    br(),
                    
                    box(
                        h2("Clients"),
                        
                        width = 2000,
                        
                        style="height: 520px; display: block;
                                    margin-right: 10px; bottom: 0; overflow-y: hidden;",
                        
                        br(),
                        
                        div(dataTableOutput("clients"),
                            style="overflow-x: scroll; 
                                            height: 500px; display: block;"))
                
            )
        )
    ))
    
# Server
    
    server <- function(input, output, session) {
    
        cdata <- reactive({
            googlesheets4::read_sheet('link')
        })
        
        output$clients <- DT::renderDataTable(DT::datatable(
            
            cdata(),
            
            options = list(
                
                initComplete = JS(
                    "function(settings, json) {",
                    "$(this.api().table().header()).css({'background-color': 'gold', 
                    'color': 'black'});",
                    "}"),
                
                pageLength = 5, header = TRUE, lengthChange = FALSE),
            rownames= FALSE,) %>% formatDate(c(2,4), method = "toLocaleDateString")
            
        )
    
    }
    
# Run App

    shinyApp(ui = ui, server = server)

标签: rshinydatatables

解决方案


推荐阅读