nanxstats / r-base-shortcuts

⚡ Base R shortcuts: A collection of lesser-known but powerful idioms and coding patterns for writing concise and fast R code
https://nanx.me/blog/post/r-base-shortcuts/
158 stars 16 forks source link

R-Insight: Return a Listing of all Functions in a Target R Package #7

Closed brichard1638 closed 1 year ago

brichard1638 commented 1 year ago

The R code in this snippet allows the user to directly write console output in RStudio to a pre-designated text file. The purpose of this code snippet is to provide an alternative for capturing R data results when such data cannot be stored within a data object. List object data produced by various R functions, for example, are very difficult to convert to data frames, which is necessary if the data needs to be externally accessed and used. The following R code resolves this issue:

This example returns a listing of all R package functions by name and by structure, converting the output to a text file:

library(dplyr) fp = paste0(path.expand("~"), "/dplyr_fcn_list.txt") sink(fp, append = FALSE, split = FALSE) lsf.str(pos = "package:dplyr") sink() closeAllConnections()

NOTE1: Before executing the code, the target package must be locally installed AND loaded.

NOTE2: The lst.str function is an abbreviation for List Functions as a String.

nanxstats commented 1 year ago

Sure, there is a need to redirect the outputs at times.

I agree that the base R solution would involve sink() or capture.output(). There are also with_sink facilities in withr.

It has nothing to do with dplyr or listing the function signatures from namespaces, though.