我想在有动态切片的数据集中,按切片因子显示一个HoT表。下面是我实现这一目标的最佳尝试,但输出是一个复制表。从打印出的拆分数据帧中可以看出一切看起来如何正确,但RHandsontable似乎存在一个问题,即它只指向映射中创建的最后一个HoT。
知道如何实现显示不同的数据格式吗?
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)输出-所有'aaa‘副本.

发布于 2022-05-26 02:32:25
在for循环中使用local()。
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]]))
})
}https://stackoverflow.com/questions/72383374
复制相似问题