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

Error when pasting new rows #138

Closed
martinv13 opened this issue Dec 28, 2016 · 9 comments
Closed

Error when pasting new rows #138

martinv13 opened this issue Dec 28, 2016 · 9 comments

Comments

@martinv13
Copy link

Hi,
hot_to_r fails when pasting rows from Excel results in creating new rows.
To reproduce, you can try it with your example:

shiny::runGitHub("rhandsontable", "jrowen", subdir = "inst/examples/rhandsontable_portfolio")

Paste an array with more rows than the rhandsontable currently has. An error is triggered in hot_to_r:

Warning: Error in row.names<-.data.frame: invalid 'row.names' length
Stack trace (innermost first):
    88: row.names<-.data.frame
    87: row.names<-
    86: rownames<-
    85: <Anonymous>
    84: do.call
    83: hot_to_r
    82: is.data.table
    81: setDT

I tried to fix it modifying your code, with no luck so far...
Thank you!

@jrowen
Copy link
Owner

jrowen commented Jan 9, 2017

Did you try this with handsontable (see examples here)?

@martinv13
Copy link
Author

With handsontable the table is extended without any problem when you paste new rows.
Whith rhandsontable, hot_to_r fails when new rows are added. It does not seem to work either when you create new lines using the bottom right square of a cell to extend it beyond the limits of the rhandsontable (the error is different though, you enter some kind of infinite loop update between R and the browser).
Thanks

@wurzerj
Copy link

wurzerj commented Feb 13, 2017

I am facing the same issue. From my point of view the bug is in rhandsontable:::toR:

  • At the very bottom of the function you assign the row names (rownames(out) = rowHeaders).
  • If you add a new line to an existing table and parse the content with rhandsontable::hot_to_r, it seems that x$params$rRowHeaders only contains the old row headers and misses any value for the new row.
  • Due to non-matching dimensions rhandsontable:::toR crashes.
  • Interestingly there is some code to prevent this to happen if you add columns, but not for rows.

A temporary workaround is to manually fix this parameter with before executing rhandsontable::hot_to_r(x):

x$params$rRowHeaders <- as.character(seq_len(length(x$data)))

Sorry, just recognized that the problem is more complex. Example:

require(shiny)
require(rhandsontable)

funUI <- function() {
	rhandsontable::rHandsontableOutput("tabTest")
}


funServer <- function(input, output, session) {
	output$tabTest <-  rhandsontable::renderRHandsontable({
		mTest <- matrix(letters[1:5], 1, 5)
		rhandsontable::rhandsontable(mTest)
	})

	observeEvent(input$tabTest, {
		cat("Event\n")
	})
}

shiny::runApp(list(ui=funUI, server=funServer))

If you run that example, please try to add a row to the table. Right after adding a row the event is triggered, but if you then change any value in the new cell it won't be triggered. In addition to the problem above rhandsontable::hot_to_r won't be able to read any values from the new line.

Session Info:

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=German_Austria.1252  LC_CTYPE=German_Austria.1252    LC_MONETARY=German_Austria.1252 LC_NUMERIC=C                    LC_TIME=German_Austria.1252    

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] rhandsontable_0.3.4 shiny_1.0.0      

loaded via a namespace (and not attached):
 [1] htmlwidgets_0.8 httr_1.2.1      magrittr_1.5    R6_2.2.0        htmltools_0.3.5 tools_3.3.1     yaml_2.1.14     curl_2.3        Rcpp_0.12.8     jsonlite_1.2    digest_0.6.11   xtable_1.8-2   
[13] httpuv_1.3.3    mime_0.5  

@hjfischer
Copy link

It probably goes without saying, but the hot_to_r issue also occurs when rows are removed.

@jrowen
Copy link
Owner

jrowen commented Mar 1, 2017

I'm using the latest dev version and not running into any problems with the example noted above. I can add a new row and update cells, each time seeing 'Event' printed to the console. Would you mind trying the dev version and letting me know if you still run into an error?

@hjfischer
Copy link

Here is another example that highlights the issue as in the first post. Click Save after adding or deleting a row, and the Warning: Error in row.names<-.data.frame: invalid 'row.names' length error will appear. I'm using the latest dev version.

library(shiny)
library(rhandsontable)

ui <- function() {
  fluidRow(actionButton(inputId = "download", label = "Save")),
  fluidRow(rHandsontableOutput("tab"))
}

server <- function(input, output, session) {
  output$tab <- renderRHandsontable({
    rhandsontable(mtcars)
  })
  
  save <- observe({
    if(input$download == 0) return()
    saveRDS(hot_to_r(input$tab), file = "test.rds")
  })
}

shinyApp(ui = ui, server = server)

@wurzerj
Copy link

wurzerj commented Mar 13, 2017

Tested the latest dev version:

  • I can confirm, that reactivity is working as it should (in contrast to the CRAN version)
  • Unfortunately hot_to_r crashes, as described in hjfischer's last post

@jrowen
Copy link
Owner

jrowen commented Mar 14, 2017

I just pushed an update that should fix the row naming problem. You may need to update your app to use the following pattern when referring to the table:

  output$tab <- renderRHandsontable({
    if (!is.null(input$tab)) {
      DF = hot_to_r(input$tab)
    } else {
      DF = mtcars
    }

    rhandsontable(DF)
  })

Without these steps, table updates won't be in sync with the data R object mapping. At some point, it would be ideal to move this logic to JavaScript and avoid the R step.

@jrowen
Copy link
Owner

jrowen commented May 21, 2017

Closing, but please let me know if you still have questions.

@jrowen jrowen closed this as completed May 21, 2017
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

4 participants