yuanfan
我知道的是dtplyr
可以把dplyr
的代码转化成data.table
版本的,但是只支持dplyr
和部分tidyr
的函数的转化
比如说第一段代码,先把purrr::map
的写法改写成dplyr
的写法,然后加上dtplyr::lazy_dt
library(sparkline)
library(dplyr, warn.conflicts = F)
library(dtplyr)
table2 <- tibble(
column1 = c(rep(c('坂田银时', '神乐', '志村新八', '定春' ), 10)),
column2 = sample(1:4, 40, replace = T),
column3 = sample(10:20, 40, replace = T)
)
# dplyr version
table2.group <- table2 %>%
group_by(column1) %>%
summarise(
column2_mean = mean(column2),
sparkline1 = as.character(htmltools::as.tags(sparkline(column2, type = "line"))),
sparkline2 = as.character(htmltools::as.tags(sparkline(column2, type = "bar")))
) %>%
ungroup()
# start with lazy_dt
lazy_dt(table2) %>%
group_by(column1) %>%
summarise(
column2_mean = mean(column2),
sparkline1 = as.character(htmltools::as.tags(sparkline(column2, type = "line"))),
sparkline2 = as.character(htmltools::as.tags(sparkline(column2, type = "bar")))
) %>%
ungroup()
#> Source: local data table [4 x 4]
#> Call: `_DT1`[, .(column2_mean = mean(column2), sparkline1 = as.character(htmltools::as.tags(sparkline(column2,
#> type = "line"))), sparkline2 = as.character(htmltools::as.tags(sparkline(column2,
#> type = "bar")))), keyby = .(column1)]
#>
#> column1 column2_mean sparkline1 sparkline2
#> <chr> <dbl> <chr> <chr>
#> 1 坂田银时 2.8 "<span id=\"htmlwidget-5b49… "<span id=\"htmlwidget-430…
#> 2 定春 2.4 "<span id=\"htmlwidget-bbb6… "<span id=\"htmlwidget-b68…
#> 3 志村新八 2.6 "<span id=\"htmlwidget-bd77… "<span id=\"htmlwidget-4d1…
#> 4 神乐 2.4 "<span id=\"htmlwidget-a271… "<span id=\"htmlwidget-413…
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
这里的Call
就是转化后的data.table
代码,把_DT1
替换成data.table(table2)
就可以了
library(data.table, warn.conflicts = F)
data.table(table2)[, .(column2_mean = mean(column2), sparkline1 = as.character(htmltools::as.tags(sparkline(column2, type = "line"))), sparkline2 = as.character(htmltools::as.tags(sparkline(column2, type = "bar")))), keyby = .(column1)]