1.onLoad <- function(lib, pkg) { 2 .render_context <<- new_stack() 3 if (getOption("rmarkdown.df_print", TRUE)) { 4 registerMethods(list( 5 # c(package, genname, class) 6 c("knitr", "knit_print", "data.frame") 7 )) 8 } 9} 10 11# Reusable function for registering a set of methods with S3 manually. The 12# methods argument is a list of character vectors, each of which has the form 13# c(package, genname, class). 14registerMethods <- function(methods) { 15 lapply(methods, function(method) { 16 pkg <- method[[1]] 17 generic <- method[[2]] 18 class <- method[[3]] 19 func <- get(paste(generic, class, sep = ".")) 20 if (pkg %in% loadedNamespaces()) { 21 registerS3method(generic, class, func, envir = asNamespace(pkg)) 22 } 23 setHook( 24 packageEvent(pkg, "onLoad"), 25 function(...) { 26 registerS3method(generic, class, func, envir = asNamespace(pkg)) 27 } 28 ) 29 }) 30} 31 32