rhandsontable icon indicating copy to clipboard operation
rhandsontable copied to clipboard

Get afterRowMove callback after using manualMove in R Shiny

Open sschw56 opened this issue 3 years ago • 2 comments

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 avatar Mar 25 '21 17:03 sschw56

Nobody?

sschw56 avatar Apr 05 '21 18:04 sschw56

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)

drwilkins avatar Apr 07 '23 21:04 drwilkins