hot_to_r fails to copy date data
I've recently upgraded my setup to
- R 4.3.0
- rhandsontable 0.3.8
An existing rhandsontable app was failing when trying to copy the date column, returning NA instead of the date. I receive the following warning:
Warning in as.character.POSIXt(as.POSIXlt(x), ...) : as.character(td, ..) no longer obeys a 'format' argument; use format(td, ..) ?
I built an example ShinyApp to reproduce the issue:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
titlePanel("rhandsontable Example"),
sidebarLayout(
sidebarPanel(
actionButton("copyButton", "Copy Data")
),
mainPanel(
rHandsontableOutput("myTable"),
textOutput("copiedData")
)
)
)
server <- function(input, output) {
# Create initial data for the rhandsontable object
initialData <- data.frame(
Name = c("John", "Jane", "Mark"),
Age = c(25, 30, 35),
Date = as.Date(c("2022-01-01", "2022-02-01", "2022-03-01"))
)
# Render the rhandsontable object
output$myTable <- renderRHandsontable({
rhandsontable(initialData)
})
# Copy the data from the rhandsontable object and display it in a text field
observeEvent(input$copyButton, {
copiedData <- hot_to_r(input$myTable)
output$copiedData <- renderText({
paste("Copied Data:", paste(copiedData, collapse = ", "))
})
})
}
shinyApp(ui, server)
Seems like its an easy fix, I had same issue with code after update to 4.3. I just replaced as.character(data[, x], format = "%m/%d/%Y"), with format(data[, x], format = "%m/%d/%Y") Not sure if that will break older code, but using trace I quickly tried it and it worked.
Where would one apply this fix to the example provided? It is not at all clear to me.
Hi, I made the fix in the package by downloading it and creating my own package. However it did not fix the issue with the date, I spoke too soon. I fixed warning, but my dates are still getting messed up whenever I update a table. So seems the error is something else. I just went back to 4.2.1, that was the easiest fix.
Seems like its an easy fix, I had same issue with code after update to 4.3. I just replaced as.character(data[, x], format = "%m/%d/%Y"), with format(data[, x], format = "%m/%d/%Y") Not sure if that will break older code, but using trace I quickly tried it and it worked.
Having same issue after switching from R 4.2.2 to 4.3 as well. Seems a compatibility issue?
The issue seems to be in the rhandsontable function in file rhandsontable.R line 75:
if (!useTypes) {
data = do.call(cbind, lapply(data, function(x) {
if (class(x) == "Date")
as.character(x, format = "%m/%d/%Y")
else as.character(x)
}))
data = as.matrix(data, rownames.force = TRUE)
cols = NULL
}
where one should change as.character(x, format = "%m/%d/%Y") to format(x, "%m/%d/%Y").
there are two lines to fix in v.0.3.8:
- https://github.com/jrowen/rhandsontable/blob/dd296376820ed864e8f938581482878a40f54a0e/R/rhandsontable.R#L75 to
format(x, "%m/%d/%Y") - https://github.com/jrowen/rhandsontable/blob/dd296376820ed864e8f938581482878a40f54a0e/R/rhandsontable.R#L93 to
format(data[, i], "%m/%d/%Y")