-1

I am testing parquet files as a way to speed up a Shiny App.

The prices.csv file has been downloaded from Kaggle and converted to parquet using arrow::write_parquet and this little Shiny App has been built:

library(shiny)
library(arrow, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(lubridate)

data <- read_parquet(file = "raw_data/prices.parquet")
tickers = unique(data$symbol) %>% head(250)

ui <- fluidPage(
  tags$h1("Stock ticker explorer"),
  selectInput(
    inputId = "selectTicker",
    label = "Select ticker:",
    choices = tickers,
    selected = tickers[1]
  ),
  tableOutput(outputId = "pricesTable")
)

server <- function(input, output) {
  output$pricesTable <- renderTable({
    filtered_data <- data %>%
      filter(symbol == input$selectTicker) %>%
      head(15) %>%
      collect()
    
    filtered_data
  })
}

shinyApp(ui, server)

In the Shiny App code, I do not understand that

data <- read_parquet(file = "raw_data/prices.parquet")

returns

# A tibble: 851,264 × 7
   date                symbol  open close   low  high  volume
   <dttm>              <chr>  <dbl> <dbl> <dbl> <dbl>   <dbl>
 1 2016-01-05 00:00:00 WLTW    123.  126.  122.  126. 2163600
 2 2016-01-06 00:00:00 WLTW    125.  120.  120.  126. 2386400
 3 2016-01-07 00:00:00 WLTW    116.  115.  115.  120. 2489500
 4 2016-01-08 00:00:00 WLTW    115.  117.  114.  117. 2006300
 5 2016-01-11 00:00:00 WLTW    117.  115.  114.  117. 1408600
 6 2016-01-12 00:00:00 WLTW    116.  116.  114.  116. 1098000
 7 2016-01-13 00:00:00 WLTW    116.  113.  113.  117.  949600
 8 2016-01-14 00:00:00 WLTW    114.  114.  110.  115.  785300
 9 2016-01-15 00:00:00 WLTW    113.  113.  112.  115. 1093700
10 2016-01-19 00:00:00 WLTW    114.  110.  110.  116. 1523500
# … with 851,254 more rows

when executed in R, but when I run the App, I get the date in UNIX format:

Shiny App Output

  1. Why is the output not respecting the format of the data object in R?
  2. How can I convert this Unix date to a normal date format?

I have uncessfully tried:

server <- function(input, output) {
  output$pricesTable <- renderTable({
    filtered_data <- data %>%
      filter(symbol == input$selectTicker) %>%
      head(15) %>%
      mutate(date = as.POSIXct(date)) %>% 
      collect()
    
    filtered_data
  })
}
SparklingWater
  • 215
  • 2
  • 4
  • If the proposed answer is not resolving the issue, please let us know so we can propose alternatives. Otherwise, please accept an answer by clicking the check mark in the upper left of the answer. – socialscientist Jul 19 '22 at 00:23

1 Answers1

0

You should be able to workaround this issue by coercing the dates to character vectors with as.character() since the class of the object is irrelevant to the user experience. An example of this work-around in practice can be found here.

Apparently renderTable() historically has had issues working with dates, so as.POSIXct(date) doesn't fix it. While an issue was opened on GitHub for shiny years ago, it was closed without change.

socialscientist
  • 3,759
  • 5
  • 23
  • 58