Home > database >  Merge tables in formattable in R
Merge tables in formattable in R

Time:11-20

Is there a way to combine the two tables in formatttable in R? I want to retain the sparklines in the final table Data:

test1<-structure(list(Name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("John", "Mary"), class = "factor"), 
    year = c(2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
    2021L, 2021L, 2021L, 2021L, 2021L), week = c(1L, 2L, 3L, 
    4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), Total = c(3L, 2L, 0L, 
    0L, 1L, 0L, 3L, 2L, 0L, 0L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
-12L))

data <- structure(list(Name = structure(2:1, .Label = c("John", "Mary"
), class = "factor"), Total_per_name = c(50L, 100L)), class = "data.frame", row.names = c(NA, 
-2L))

My code
test1 %>% 
      group_by(Name) %>%
      summarise("Weekly_trend" = spk_chr(Total)) %>%
      formattable() %>%
      as.datatable() %>%
      spk_add_deps()

CodePudding user response:

Do a join after the summarise

library(dplyr)
library(formattable)
library(sparkline)
test1 %>% 
  group_by(Name) %>%
  summarise("Weekly_trend" = spk_chr(Total))  %>%
  left_join(data) %>%
  formattable() %>%
  as.datatable() %>%
  spk_add_deps()

-output

enter image description here

  •  Tags:  
  • r
  • Related