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

Joins lose custom attributes #3468

Closed
msberends opened this issue Mar 27, 2018 · 3 comments
Closed

Joins lose custom attributes #3468

msberends opened this issue Mar 27, 2018 · 3 comments

Comments

@msberends
Copy link

msberends commented Mar 27, 2018

Much like #3259, the join functions also lose custom attributes. We use attributes to save the query to the object it produced:

my_get_data_function <- function(query) {
  data <- db_function_to_connect_and_collect_and_so_forth(sql(query))
  attr(data, "qry") <- sql(query)
  data
}
qry <- function(object) {
  q <- attributes(object)$qry
  if (!is.null(q)) {
    q
  } else {
    cat("No query found.\n")
  }
}

So, for example with dplyr data:

library(dplyr)
attributes(band_members)
# $names
# [1] "name" "band"
# 
# $class
# [1] "tbl_df"     "tbl"        "data.frame"
# 
# $row.names
# [1] 1 2 3

# save this attributes to this dataset:
attr(band_members, 'qry') <- sql("SELECT * FROM tbl1")
attributes(band_members)
# $names
# [1] "name" "band"
# 
# $class
# [1] "tbl_df"     "tbl"        "data.frame"
# 
# $row.names
# [1] 1 2 3
# 
# $qry
# <SQL> SELECT * FROM tbl1

# and here it vanishes:
band_members %>% left_join(band_instruments) %>% attributes()
# Joining, by = "name"
# $class
# [1] "tbl_df"     "tbl"        "data.frame"
# 
# $row.names
# [1] 1 2 3
# 
# $names
# [1] "name"  "band"  "plays"

Is this intended? Especially for left_join, we would like to keep the attributes of x.

@batpigandme
Copy link
Contributor

I think this probably can be one issue with #3259, but I'll let a second set of eyes come across it to decide first.

Thanks for filing.

@krlmlr
Copy link
Member

krlmlr commented Apr 1, 2018

Thanks. Yes, currently you can't rely on attributes being preserved. We're working on it, but the attributes of an object will be preserved only if it is a subclass of data.frame or tbl_df.

Currently, the safest way is to implement a custom S3 class and methods for that class.

@krlmlr krlmlr closed this as completed Apr 1, 2018
@lock
Copy link

lock bot commented Sep 28, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants