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

xmap_df functions and methods return inconsistent classes #76

Closed cynthiahqy closed 1 year ago

cynthiahqy commented 1 year ago

Need to implement separate xmap_tbl and xmap_df to avoid inconsistency in return values. Currently new_xmap_df() returns objects with 2 possible class attributes vectors:

x <- tibble::tribble(
      ~ISONumeric, ~ISO2, ~link,         ~country, ~ISO3,
            "004",  "AF",     1,    "Afghanistan", "AFG",
            "008",  "AL",     1,        "Albania", "ALB",
            "012",  "DZ",     1,        "Algeria", "DZA",
            "016",  "AS",     1, "American Samoa", "ASM",
            "020",  "AD",     1,        "Andorra", "AND"
      ) |>
  as_xmap_df(from = ISO2, to = ISONumeric, weights = link)

x |> new_xmap_df(col_from = "ISO3", col_to = "ISONumeric", col_weights = "link") -> x_from
class(x_from)
# [1] "xmap_df"    "xmap"       "tbl_df"     "tbl"        "data.frame"

switch_xmap_df() strips the tibble class attributes, but new_xmap_df() does not.

x |> new_xmap_df(col_from = "ISO2", col_to="ISONumeric", col_weights = "link") |>
  switch_xmap_df(col_from = "ISO3") -> x_from_switch
class(x_from_switch)
# [1] "xmap_df"    "xmap"       "data.frame"
cynthiahqy commented 1 year ago

Drop tibble support for now

Consider in future:

Code dump for tibble methods for later reference:

#' @describeIn new_xmap Construct xmap_tbl from tbl_df
new_xmap_tbl <- function(x = tibble::tibble(), col_from, col_to, col_weights, from_set = NULL) {
  #' checks argument types
  stopifnot(tibble::is_tibble(x))

  #' coerce from xmap_df to xmap_tbl
  x <- new_xmap_df(x, col_from, col_to, col_weights, from_set)

  #' @return `x` with additional subclasses `xmap_tbl`, `xmap`
  class(x) <- c("xmap_tbl", "xmap", "tbl_df", "tbl")
}
#' Print an `xmap_tbl`
#' 
#' @export
print.xmap_tbl <- function(x){
  xmap_df <- x
  x_direction <- .get_link_direction.xmap_df(xmap_df)
  x_type <- .get_link_types.xmap_df(xmap_df)
  x_links <- tibble::as_tibble(xmap_df)

  ## print headers and links
  cat(paste0("xmap_tbl:\n",  x_type, "\n", x_direction, "\n"))
  print(x_links)
}
cynthiahqy commented 1 year ago

What are the benefits to using the tibble class other than print methods?

Can a xmap_df be passed to dplyr functions, and does that convert it to a tibble?

cynthiahqy commented 1 year ago

Let the generic have the default: as_xmap(x, subclass = NULL), where subclass can be _df, _tbl, _matrix, _igraph. So that: