restore restores the attributes of class matric_sim.

sim_restore(x, x_attributes)

Arguments

x

object to preserve.

x_attributes

list of attributes of class matric_sim.

Value

Object of class matric_sim if x is a valid object of that class

Details

This is a workaround until tibble inheritance improves

https://github.com/tidyverse/tibble/issues/275 https://adv-r.hadley.nz/s3.html#inheritance.

https://github.com/tidyverse/dplyr/issues/5480#issuecomment-682620522 "dplyr is not really ready for extension in this way"

These are some of the dplyr verbs that will necessitate restoration:

  • summarise

  • group_by

There are likely more!

Examples


sim_df <-
  matric::sim_new(
    data.frame(id1 = 1, id2 = 2, sim = 1),
    data.frame(id = c(1, 2), Metadata_group = c("a", "b")),
    list(method = "pearson")
  )
sim_df_attr <- attributes(sim_df)
"matric_sim" %in% class(sim_df)
#> [1] TRUE
"matric_sim" %in% class(sim_df %>% dplyr::slice(1))
#> [1] TRUE
"matric_sim" %in%
  class(
    sim_df %>%
      dplyr::group_by(id1, id2) %>%
      dplyr::summarize(sim = mean(sim), .groups = "keep")
  )
#> [1] FALSE

"matric_sim" %in%
  class(
    sim_df %>%
      dplyr::group_by(id1, id2) %>%
      dplyr::summarize(sim = mean(sim), .groups = "keep") %>%
      matric::sim_restore(sim_df_attr)
  )
#> [1] TRUE