r - 将 reactable 转换为 ggplot,这可能吗?

标签 r ggplot2 reactable

我目前在某些代码中将可 react 对象存储为对象。我希望能够将所述对象转换为 ggplot,但无论我做什么,我都会得到相同错误的变体。 使用 blastula 的 add_ggplot 函数,我得到:

Error in UseMethod("grid.draw") : 
  no applicable method for 'grid.draw' applied to an object of class "c('reactable', 'htmlwidget')"

使用 ggplotify 的 as.ggplot 函数,我得到:

Error in UseMethod("as.grob") : 
  no applicable method for 'as.grob' applied to an object of class "c('reactable', 'htmlwidget')"

有人对如何达到预期结果有建议吗?

编辑:在回答一个我可能本来应该回答的问题时:reactable 源自一个非常普通的数据框。

df <- structure(list(Date = c("2019-02-09", "2019-02-09", "2019-02-09", 
"2019-02-09", "2019-02-09", "2019-02-09", "2020-02-09", "2020-02-09", 
"2020-02-09", "2020-02-09", "2021-02-09", "2021-02-09", "2021-02-09", 
"2021-02-09"), Type = c("HUF", "HAD", "WOK", "STR", "HUF", "HAD", 
"WOK", "STR", "HUF", "HAD", "WOK", "STR", "HUF", "HAD"), Value = c(12L, 
226394L, 27566L, 217098L, 208463L, 9320L, 156607L, 19790L, 24541L, 
1074419L, 17250L, 12249L, 43651L, 45121L)), class = "data.frame", row.names = c(NA, 
-14L))

EDIT2:这是可 react 的代码,很抱歉之前没有包含它:

react_df <- reactable(df, highlight =  TRUE, compact = TRUE,pagination = FALSE, columns = list(Date = colDef(name = "Last Recorded", align = 'center'), Type = colDef(name = "Category", align = 'center'), Value = colDef(name = "Change(s)", align = 'center', cell = data_bars(df, background = "white", border_width = "2px", bar_height = 3, align_bars = "left", text_position = "outside-end", max_value = 1, number_fmt = scales::percent))))

react_df

最佳答案

reactable 对象是一个 html 小部件,没有办法直接将其转换为 ggplot 对象。有一些方法可以实现它,比如将可 react 对象保存为 png,将 png 转换为光栅 Grob,然后使用 ggplotify:

library(grid)
library(png)
library(ggplotify)

save_reactable_test(react_df, "my_reactable.png")
img <- readPNG("my_reactable.png")
p <- as.ggplot(rasterGrob(img))

现在对象 p 在技术上是一个 ggplot:

class(p)
#> [1] "gg"     "ggplot"

有点看起来像原始表格:

p

enter image description here

但是,这实际上只是一个包装图像,在调整大小、编辑、更改主题元素等方面不会像普通的 ggplot 那样表现。

仅仅编写绘制您要查找的表格的ggplot代码真的不难。即使您丢失了原始数据,您也可以像这样从可 react 对象中恢复它:

library(rvest)
library(jsonlite)

df <- read_html(as.character(react_df$x$tag)) %>%
  html_element("reactable") %>%
  html_attr("data") %>%
  parse_json() %>%
  lapply(unlist) %>%
  as.data.frame()

我们可以直接使用以下 ggplot 代码重新生成您的表格:

library(ggplot2)

ggplot(df, aes(y = rev(seq(nrow(df))))) +
  geom_text(aes(x = 1, label = Date), hjust = 0) +
  geom_text(aes(x = 2, label = Type), hjust = 0) +
  geom_segment(aes(x = 3, xend = 3 + Value / max(Value), yend = stat(y)),
               size = 2, color = "deepskyblue4") +
  geom_text(aes(x = 3, label = paste0(Value, " (",
                      scales::percent(Value / sum(Value), 0.1), ")")),
            hjust = 0, vjust = -0.8) +
  geom_hline(yintercept = seq(nrow(df)) - 0.5, size = 0.05) +
  geom_hline(yintercept = nrow(df) + 0.5) +
  annotate("text", label = colnames(df), x = 1:3, 
             y = nrow(df) + 1, hjust = 0, fontface = 2) +
  theme_void() 

enter image description here

这是一个合适的 ggplot,看起来像原始的 reactable,完全可定制,并且在调整大小等情况下会按预期运行。

关于r - 将 reactable 转换为 ggplot,这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73829821/

相关文章:

r - 将 map() .x 传递给函数

r - 如何在具有可扩展行的响应式(Reactive)中从嵌套的 tibble 打印 ggplot 对象?

r - 结合轴归一化绘图

删除ggplot2 geom_bar中条形之间的空间

R中的SQL查询将两个日期作为参数传递

r - 如何用丝带和线条组合和修改ggplot2图例?

python - 在plotly R中以不同模式对多行悬停文本进行分组

r - ggplot2 中不同颜色的分组条形图

r - 使用 r 中嵌入的字体将 ggplot 图形保存为 PDF

r - 在可 react 的整个单元格上悬停时显示工具提示