cynthiahqy / conformr-xmap-project

R Package for harmonising data of different classifications or aggregations into a single dataset
MIT License
7 stars 1 forks source link

dplyr drops `col_` attributes when applied to `xmap_df` #92

Closed cynthiahqy closed 1 year ago

cynthiahqy commented 1 year ago

When testing reset_xmap_df():

This works:

    ## alternative weights
    w2 <- df |> 
      dplyr::mutate(w2 = c(1, 1, 1, 0.4, 0.6)) |>
      as_xmap.data.frame(f1, t1, w2)
    w2_reset <- df |> 
      dplyr::mutate(w2 = c(1, 1, 1, 0.4, 0.6)) |>
      as_xmap.data.frame(f1, t1, w1) |>
      reset_xmap_df(new_weights = "w2")
    testthat::expect_identical(w2, w2_reset)

But not if the mutate comes after as_xmap:

    w2_reset <- df |> 
      as_xmap.data.frame(f1, t1, w1) |>
      dplyr::mutate(w2 = c(1, 1, 1, 0.4, 0.6)) |>
      reset_xmap_df(new_weights = "w2")

This is because dplyr seems to modify the xmap_df object, but annoyingly retains the class attributes:

df |> 
    as_xmap.data.frame(f1, t1, w1) |>
   dplyr::mutate(w2 = c(1, 1, 1, 0.4, 0.6)) |> attributes()
#> $names
#> [1] "f1" "t1" "w1" "w2"
#> 
#> $row.names
#> [1] 1 2 3 4 5
#> 
#> $class
#> [1] "xmap_df"    "xmap"       "data.frame"
cynthiahqy commented 1 year ago

This is not really a bug. In fact in light of #94 it's actually a good feature

cynthiahqy commented 1 year ago

I take that back -- it is annoying when implementing #95 because I can't just check that the xmap argument is of class xmap -- since it's possible that the weights have been modified -- i.e. need to revalidate the crossmap