jrowen / rhandsontable

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

Get afterRowMove callback after using manualMove in R Shiny #376

Open sschw56 opened 3 years ago

sschw56 commented 3 years ago

Hi there,

I am developing a R Shiny App, using a RHandsontable. In the table it should be possible to move the rows via click and drop. So I set manualRowMove = TRUE and it works. My problem ist now, how do I get the current shown table (after the row moving)?

I poorly can JavaScript (and that is here the main problem), but I guess I need the "afterRowMove" hook. How do I get a reactive information, when the user moves a row? Or, it dont has to be reactive, because I added a own context menu item and if this item is triggered, then I need to know which row of the table is where (the index doesn't work!).

Please help me. I read a lot about hooks and Handsontable, but everything is in JS and I cann't transfer it into R.

Chefkoch

Here is a small example:

library(shiny)
library(rhandsontable)

library(shiny)
library(rhandsontable)
library(dplyr)
library(stringr)
library(data.table)

options(scipen = 99999)
options(encoding = "UTF-8")

ui <- fluidRow(
  rHandsontableOutput("foo"),

  fluidRow(
    verbatimTextOutput("sel")
  )
)

server <- function(input, output) {

  table <- cbind(nr = 1:10, iris[1:10, ])

  output$foo = renderRHandsontable({
    rhandsontable(
      table,
      manualRowMove = TRUE,
      rowHeaders = TRUE
    ) %>% 
      hot_context_menu(allowColEdit = FALSE,
                       alignment = FALSE,
                       customOpts = list(
                         reload = list(
                           name = "own_item",
                           callback = htmlwidgets::JS(
                             "function () {
                                val = this.getSelectedLast();
                                Shiny.setInputValue('own_item', {row1: val[0] + 1, row2: val[2] + 1}, {priority: 'event'})
                             }"
                           )
                         )
                       )
      ) 
  })

  output$sel = renderPrint({
    print("Doing something with the index of the marked Rows (which is wrong after movings): ")
    print(paste0("stored index: ", input$own_item$row1, ", possible wrong entry: ", paste0(table[input$own_item$row1, ], collapse = ", ")))
  })

}
# Run the application 
shinyApp(ui = ui, server = server)
sschw56 commented 3 years ago

Nobody?

drwilkins commented 1 year ago

Probably far too late to be useful, but here's an example modified slightly from this post that outputs user-manipulated table data (i.e. when the row moves, the output data moves):


library(shiny)
library(rhandsontable)

ui <- fluidPage(
  titlePanel("rhandsontable test"),
    mainPanel(
    rHandsontableOutput("test"),
    p(),
    h3('Reactivity check'),
    verbatimTextOutput("content")
  )
)

server <- function(input, output) {

  output$test <- renderRHandsontable({
    rhandsontable(
      data = iris[1:5,],
      manualRowMove = TRUE,
      manualColumnMove = TRUE
    )
  })

  output$content <- renderText({
    tmp <- hot_to_r(input$test)
    sprintf(
      'Column names: %s\nRow names:%s',
      paste(names(tmp), collapse = ', '), 
      paste(tmp$Sepal.Length, collapse = ', ')
    )
  })

}

shinyApp(ui = ui, server = server)