jrowen / rhandsontable

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

RHandsontable - renderUI Mapping #408

Closed zburch closed 2 years ago

zburch commented 2 years ago

I'm wanting to show an HoT table per slicing factor in shiny, from a dataset that has dynamic slices. Below is my best attempt to achieve this, but the output is a duplicated table. You can see how everything looks correct from the split data frames in the print out, but it appears RHandsontable has an issue where it's only pointing to the last HoT created in the map.

Any idea how to achieve showing the distinct dataframes?

library(shiny)
library(dplyr)
library(rhandsontable)
library(purrr)
ui <- fluidPage(

  uiOutput('tables')
)

server <- function(input, output) {
  mtcars$slc <- sample(c('aaa','bbb'),nrow(mtcars),replace=TRUE)
  df <- mtcars

  getSlice <- function(df_tmp,slca){
    print(slca)
    df_tmp <- df_tmp %>% filter(slc==slca)
    df_tmp
  }

  output$tables <- renderUI({
    slices <- unique(df$slc)
    input_dfs <- map(slices,~getSlice(df,.x))

    for(i in 1:length(slices)){
      print(input_dfs[[i]]) # proof that there are two distinct data 
                            #  frames going into renderRHandsontable()
      output[[slices[i]]] <- renderRHandsontable(rhandsontable(input_dfs[[i]]))
    }

    out <- map(slices,function(x){
      rHandsontableOutput(x)
    })
    print(out) # div ids are correctly distinct, but the tables that show are not!
    out
  })
}

shinyApp(ui = ui, server = server)

Output - all 'aaa' duplicates...

image

zburch commented 2 years ago

Solved. See this answer here... https://stackoverflow.com/questions/72383374/rhandsontable-renderui-mapping