如何解决R:可反应的-在列中放置图;行/索引过滤不起作用
我想将一个图添加到一个可反应的列中(为简单起见,这里是一个点图,但是稍后我将需要其他类型)。该图的数据来自同一行的其他列。
将图放到(折叠的)详细信息行中时,我可以生成图。 但是,我正在努力将相同的情节放在其自己的栏中。似乎使用index参数过滤数据无效。
如果我改变
match_cyl <- car_df$cyl[index]
到
match_cyl <- car_df$cyl[1]
该代码有效(针对特定值)。因此,该绘图的代码还可以,但是带有索引/函数的内容不正确。
感谢任何提示!
library(reactable)
library(tidyverse)
library(htmltools)
car_df <- mtcars %>%
group_by(cyl) %>%
summarize(mpg = list(mpg)) %>%
dplyr::mutate(
my_plot=NA
)
car_df %>%
reactable(
columns = list(
mpg = colDef(show = FALSE),#does not work
my_plot = colDef(cell=function(index){
match_cyl <- car_df$cyl[index]
plots_data <- filter(mtcars,cyl == match_cyl)
htmltools::div(style = "padding: 30px",{
p1 <- ggplot(plots_data,aes(x = mpg,y = disp)) +
geom_point() +
theme_minimal() +
scale_x_continuous(limits = c(0,max(mtcars$mpg) * 1.1)) +
scale_y_continuous(limits = c(0,max(mtcars$disp) * 1.1)) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
labs(title = glue::glue("Plot for {match_cyl} cylinders."),x = "MPG",y = "displacement")
plotly::ggplotly(p1)
}
)
}
)
),#works
details = function(index){
match_cyl <- car_df$cyl[index]
plots_data <- filter(mtcars,cyl == match_cyl)
htmltools::div(style = "padding: 30px",{
p1 <- ggplot(plots_data,y = disp)) +
geom_point() +
theme_minimal() +
scale_x_continuous(limits = c(0,max(mtcars$mpg) * 1.1)) +
scale_y_continuous(limits = c(0,max(mtcars$disp) * 1.1)) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
labs(title = glue::glue("Plot for {match_cyl} cylinders."),y = "displacement")
plotly::ggplotly(p1)
}
)
}
)
#> Warning in cyl == match_cyl: longer object length is not a multiple of shorter
#> object length
#> Warning in if (nchar(plot$labels$title %||% "") > 0) {: the condition has length
#> > 1 and only the first element will be used
#> Warning in cyl == match_cyl: longer object length is not a multiple of shorter
#> object length
#> Warning in if (nchar(plot$labels$title %||% "") > 0) {: the condition has length
#> > 1 and only the first element will be used
#> Warning in cyl == match_cyl: longer object length is not a multiple of shorter
#> object length
#> Warning in if (nchar(plot$labels$title %||% "") > 0) {: the condition has length
#> > 1 and only the first element will be used
由reprex package(v0.3.0)于2020-11-04创建
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。