r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
502 stars 140 forks source link

Allow custom descriptions for `obj_type_friendly()` #1674

Open EmilHvitfeldt opened 10 months ago

EmilHvitfeldt commented 10 months ago

We were trying to add custom checking functions using import-standalone_types_check.R in https://github.com/tidymodels/dials/. And it would be nice if there was a way to add to the ways obj_type_friendly() works without having to modify a file that says This document is read only.

In the below example, it would be nice if we could make it say parameter set instead of the more verbose <parameters/tbl_df/tbl/data.frame>.

check_param <- function(x, 
                        ..., 
                        allow_na = FALSE, 
                        allow_null = FALSE, 
                        arg = caller_arg(x), 
                        call = caller_env()) {
  if (!missing(x) && inherits(x, "param")) {    
    return(invisible(NULL))  
  }  

  stop_input_type(
    x,
    c("a single parameter object"),
    ...,
    allow_na = allow_na,
    allow_null = allow_null,
    arg = arg,
    call = call
  )
}

library(dials)
#> Loading required package: scales

param <- mtry()
check_param(param)

parameter_set <- parameters(param)
check_param(parameter_set)
#> ERROR:
#> ! `parameter_set` must be a single parameter object, not a <parameters/tbl_df/tbl/data.frame> object.
lionel- commented 10 months ago

Would you like to send a PR to implement this change?