'Shiny showing numbers instead of dates

I'm trying to show a table with Shiny, but I have a problem showing dates in the right format. Here is an example of what I'm dealing with:

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  sidebarLayout(

    sidebarPanel(
      textInput("myfirstinput", "text1"),
      textInput("mysecondinput", "text2"),
      actionButton("button", "An action button")
    ),
    mainPanel(
      tableOutput("table1")
    )
  )
)


# Define server logic required to draw a histogram
server <- function(input, output) {
  selected <- as.Date('2000-01-01', "%Y-%m-%d")
  selected <- as.list(selected)

  output$table1 <- renderTable(selected)

}

# Run the application 
shinyApp(ui = ui, server = server)


Solution 1:[1]

It seems to work if you change the line:

  selected <- as.Date('2000-01-01', "%Y-%m-%d")

to:

    selected <- format(as.Date('2000-01-01'), "%Y-%m-%d")

Solution 2:[2]

I found that you need to format the date at the last step when the output is computed. Mutating to date earlier on did not solve the issue.

library(shiny)
library(dplyr)

ui <- fluidPage(
    fileInput("upload", "Upload CSV", accept = c(".csv", ".tsv")),
    numericInput("n", "Rows", value = 5, min = 1, step = 1),
    tableOutput("head")
)

server <- function(input, output, session) {
    
    data <- reactive({
        req(input$upload)
        readr::read_csv(input$upload$datapath) 
    })
    
    output$head <- renderTable({
        data() %>%
            group_by(condition, metric) %>%
            filter(agg_date == min(agg_date)) %>%
            arrange(condition) %>%
            mutate(agg_date = format(agg_date, "%Y-%m-%d"))
    })
}

shinyApp(ui, server)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mrhellmann
Solution 2 Joe