Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global option to return a tibble if available or a data.frame #58

Closed
JosiahParry opened this issue Sep 20, 2023 · 4 comments
Closed

Global option to return a tibble if available or a data.frame #58

JosiahParry opened this issue Sep 20, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@JosiahParry
Copy link
Collaborator

tibbles have a supperior printing method to the native data.frame. arcgislayers does not import tibble and should not. However, it would be preferable to use it if it is available.

Useful conversation on Mastodon. @jonthegeek's suggestion makes me think a package level global option would be the best way to approach this. Can possibly lean on Tidy Principles blog post

#' Maybe Tibble?
#'
#' Return a tibble if tibble is available.
#' @returns a tibble if the package is available, otherwise a data.frame
maybe_tibble <- function(x) {
  stopifnot(is.data.frame(x))
  # return a tibble if the package is installed
  if (rlang::is_installed("tibble")) {
    return(tibble::as_tibble(x))
  }
  x
}
@elipousson
Copy link
Contributor

Not sure if consistency with {sf} is a priority but sf::read_sf() handles this with the parameter as_tibble = TRUE. You could make that a default that could be ignored if a package-level option requires a different behavior. I do think returning tibbles would be nice!

@JosiahParry
Copy link
Collaborator Author

I agree. I'm strongly considering adding a hard dependency on tibble. I feel that a lot of the work that will be done with this package is going to be exploratory. The printing capabilities of tibble are a huge win. However there was a helpful expert explanation on mastodon that i think can be used to get the desired affect.

We can return a tbl (without hte tbl_df or tibble classes) which will provide the desired printing method if pillar is available or not.

Is there another scenario where you would want another aspect of tibbles beside the printing? I think this could be a quick win. A helper function in utils.R could be defined like so:

as_tbl <- function(x) {
  stopifnot(inherits(x, "data.frame")
  if (inherits(x, "tbl") return(x)
  cur_classes <- class(x)
  structure(x, class = union("tbl", old_class))
}

Then in parse_esri_json() in arcgisutils the results can be cast as a tbl and other places like list_fields() can inherit the class.

@elipousson
Copy link
Contributor

The printing alone would be nice but I think the valid column name checking and ability to avoid picking up row names by accident are also useful features for tibbles.

@JosiahParry
Copy link
Collaborator Author

data.frames for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

No branches or pull requests

2 participants