Remove timing information from the Date columns.

Hi Experts,

Currently, using this function to read the excel file and extract the specific sheet.

The sheet with 200 columns has many Date columns and is 2019-03-02 format (literally 40) and many other which are character and numeric columns.
But when we display the same sheet obtained from this code on Shiny.
Dates along with UTC 00:00:00 is displayed and it is Undesired.
Can we eliminate these unnecessary timing information from all the Dates column ? when displaying in Shiny

We could specify col_types = "text" within read_excel But even the numeric columns will be converted to text and it is undesirable.

read_excel_allsheets <- function(filename, tibble = FALSE) {
  # I prefer straight data.frames
  # but if you like tidyverse tibbles (the default with read_excel)
  # then just pass tibble = TRUE
  sheets <- readxl::excel_sheets(filename)
  myFile <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X, col_names = T, skip = 5
                                                     #, col_types = "text"
                                                     ))
  if(!tibble) x <- lapply(x, as.data.frame)
  names(myFile) <- sheets
  myFile
}

mysheets <- read_excel_allsheets(inFile$datapath)
mySheet <- mysheets$Download

Its hard to know for sure because you are not providing sample data, but probably, your date variables are getting converted to POSIXct class during reading, you can convert it back to Date class afterwards

library(tibble)
library(dplyr) 

df <- data.frame(stringsAsFactors = FALSE,
                 variable = c("a", "b"),
                 date_col1 = as.POSIXct(c("2019-03-02", "2019-03-01")),
                 date_col2 = as.POSIXct(c("2018-03-02", "2018-03-01"))
)
df <- as_tibble(df)
df
#> # A tibble: 2 x 3
#>   variable date_col1           date_col2          
#>   <chr>    <dttm>              <dttm>             
#> 1 a        2019-03-02 00:00:00 2018-03-02 00:00:00
#> 2 b        2019-03-01 00:00:00 2018-03-01 00:00:00

df %>% 
    mutate_if(~inherits(., what = "POSIXct"), as.Date) # Change POSIXct variables to Date class
#> # A tibble: 2 x 3
#>   variable date_col1  date_col2 
#>   <chr>    <date>     <date>    
#> 1 a        2019-03-02 2018-03-02
#> 2 b        2019-03-01 2018-03-01
2 Likes

Perfect @andresrcs.
Your solution works excellently.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.