I'd like to be able to have a print method reference the name of the object, but I'm just not sure if this is possible?
Looking at How to get the name of an object in R and then subsequently
meta programming - want to refer to users name for object in print method - #2 by nirgrahamuk (which is my own question )
This seems to work outside of a print
S3 method. Which is on one hand, fascinating, on the other hand, a real shame.
Looking at r - Getting the object name for S3 print method failing - Stack Overflow it seems like this just isn't possible? I feel like I've seen some magic code in the tidyverse that does this however. But I can't recall it at the moment (and it also just might not exist).
Here's a reprex that hopefully demonstrates the issue:
my_info <- function(x, env = environment()){
obj_name <- deparse(substitute(x, env))
cli::cli_alert_info("Access column names with {obj_name}$<var>")
cli::cli_alert_info("e.g., {obj_name}${names(x)[1]}")
}
my_print <- function(x){
cli::cli_h1("My Object")
cli::cli_text("My dims are {dim(x)}")
my_info(x, environment())
}
my_info(airquality)
#> βΉ Access column names with airquality$<var>
#> βΉ e.g., airquality$Ozone
my_print(airquality)
#>
#> ββ My Object βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> My dims are 153 and 6
#> βΉ Access column names with airquality$<var>
#> βΉ e.g., airquality$Ozone
But when using the print method, this doesnβt work, it just prints βxβ
print.my_class <- function(x, ...){
cli::cli_h1("My Object")
cli::cli_text("My dims are {dim(x)}")
my_info(x, environment())
}
class(airquality) <- c("my_class", class(airquality))
airquality
#>
#> ββ My Object βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> My dims are 153 and 6
#> βΉ Access column names with x$<var>
#> βΉ e.g., x$Ozone
Created on 2024-05-16 with reprex v2.1.0
Session info
sessioninfo::session_info()
#> β Session info βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> setting value
#> version R version 4.4.0 (2024-04-24)
#> os macOS Sonoma 14.3.1
#> system aarch64, darwin20
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz Australia/Brisbane
#> date 2024-05-16
#> pandoc 3.1.13 @ /opt/homebrew/bin/ (via rmarkdown)
#>
#> β Packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> package * version date (UTC) lib source
#> cli 3.6.2 2023-12-11 [1] CRAN (R 4.4.0)
#> digest 0.6.35 2024-03-11 [1] CRAN (R 4.4.0)
#> evaluate 0.23 2023-11-01 [1] CRAN (R 4.4.0)
#> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.4.0)
#> fs 1.6.4 2024-04-25 [1] CRAN (R 4.4.0)
#> glue 1.7.0 2024-01-09 [1] CRAN (R 4.4.0)
#> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#> knitr 1.46 2024-04-06 [1] CRAN (R 4.4.0)
#> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.4.0)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.0)
#> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.4.0)
#> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.4.0)
#> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.4.0)
#> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.4.0)
#> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.4.0)
#> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.4.0)
#> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.4.0)
#> rmarkdown 2.26 2024-03-05 [1] CRAN (R 4.4.0)
#> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.4.0)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0)
#> styler 1.10.3 2024-04-07 [1] CRAN (R 4.4.0)
#> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.4.0)
#> withr 3.0.0 2024-01-16 [1] CRAN (R 4.4.0)
#> xfun 0.43 2024-03-25 [1] CRAN (R 4.4.0)
#> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.4.0)
#>
#> [1] /Users/nick/Library/R/arm64/4.4/library
#> [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#>
#> ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Is there a way to get the my_print
to work with a print
S3 method?