what the difference between call R S3 method and directly call the function from global environment

30 Views Asked by At

I create a S3 method for printing the object name:

printc = function(...) {
  UseMethod("printc")
}

#' @export add_dataset.data.frame

printc.data.frame = function(...){
  
  xs <- rlang::quos(..., .named = TRUE)
  return(rlang::names2(xs))
}

and the there are two way to call it:

b <- printc(iris)

c <- printc.data.frame(iris)

the result b is <df[,5]>, istead of c: iris, could someone know the reason? how can I make the S3 method generate a result like "iris"?

when I try to figure out how S3 dispatch this method, sloop::s3_dispatch(printc(iris)), it does dispatch to the printc.data.frame? I think it may relate to the environment.

0

There are 0 best solutions below