Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hot_to_r fails to copy date data #427

Open
trentbaur opened this issue May 17, 2023 · 7 comments
Open

hot_to_r fails to copy date data #427

trentbaur opened this issue May 17, 2023 · 7 comments

Comments

@trentbaur
Copy link

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)

@trentbaur trentbaur changed the title hot_to_r fails to copy date date hot_to_r fails to copy date data May 17, 2023
@Turtle9er
Copy link

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
Copy link
Author

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

@Turtle9er
Copy link

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
Copy link

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
Copy link

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
Copy link

there are two lines to fix in v.0.3.8:

@jjjpellinen
Copy link

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

#431

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants