jrowen / rhandsontable

A htmlwidgets implementation of Handsontable.js
http://jrowen.github.io/rhandsontable/
Other
383 stars 148 forks source link

hot_to_r fails to copy date data #427

Open trentbaur opened 1 year ago

trentbaur commented 1 year ago

I've recently upgraded my setup to

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)
Turtle9er commented 1 year ago

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.

trentbaur commented 1 year ago

Where would one apply this fix to the example provided? It is not at all clear to me.

Turtle9er commented 1 year ago

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.

power-hunter commented 1 year ago

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?

jjjpellinen commented 1 year ago

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").

helgasoft commented 5 months ago

there are two lines to fix in v.0.3.8:

jjjpellinen commented 4 days ago

This PR will fix it but it seems to take time to merge it.

https://github.com/jrowen/rhandsontable/pull/431