Home > OS >  RHandsontable - renderUI Mapping
RHandsontable - renderUI Mapping

Time:05-26

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...

enter image description here

CodePudding user response:

Use local() in your for loop.

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