List All Functions & Objects of a Package in R (2 Examples)

 

In this R programming tutorial you’ll learn how to create a list containing all functions of a certain package.

The post is structured as follows:

Let’s just jump right in:

 

Example 1: List All Functions in a Package Using lsf.str() Function

Example 1 demonstrates how to get all function names of a particular add-on package in R.

In this specific example, we’ll use the stringr package for illustration. We first need to install and load the stringr package, to be able to apply the functions that are contained in the package.

install.packages("stringr")       # Install & load stringr
library("stringr")

In the next step, we can apply the lsf.str function as shown below:

lsf.str("package:stringr")        # Apply lsf.str function
# %>% : function (lhs, rhs)  
# boundary : function (type = c("character", "line_break", "sentence", "word"), skip_word_none = NA, ...)  
# coll : function (pattern, ignore_case = FALSE, locale = "en", ...)  
# fixed : function (pattern, ignore_case = FALSE)  
# invert_match : function (loc)  
# regex : function (pattern, ignore_case = FALSE, multiline = FALSE, comments = FALSE, dotall = FALSE, ...)  
# str_c : function (..., sep = "", collapse = NULL)  
# str_conv : function (string, encoding)  
# str_count : function (string, pattern = "")  
# str_detect : function (string, pattern, negate = FALSE)  
# str_dup : function (string, times)  
# str_ends : function (string, pattern, negate = FALSE)  
# str_extract : function (string, pattern)  
# str_extract_all : function (string, pattern, simplify = FALSE)  
# str_flatten : function (string, collapse = "")  
# str_glue : function (..., .sep = "", .envir = parent.frame())  
# str_glue_data : function (.x, ..., .sep = "", .envir = parent.frame(), .na = "NA")  
# str_interp : function (string, env = parent.frame())  
# str_length : function (string)  
# str_locate : function (string, pattern)  
# str_locate_all : function (string, pattern)  
# str_match : function (string, pattern)  
# str_match_all : function (string, pattern)  
# str_order : function (x, decreasing = FALSE, na_last = TRUE, locale = "en", numeric = FALSE, ...)  
# str_pad : function (string, width, side = c("left", "right", "both"), pad = " ")  
# str_remove : function (string, pattern)  
# str_remove_all : function (string, pattern)  
# str_replace : function (string, pattern, replacement)  
# str_replace_all : function (string, pattern, replacement)  
# str_replace_na : function (string, replacement = "NA")  
# str_sort : function (x, decreasing = FALSE, na_last = TRUE, locale = "en", numeric = FALSE, ...)  
# str_split : function (string, pattern, n = Inf, simplify = FALSE)  
# str_split_fixed : function (string, pattern, n)  
# str_squish : function (string)  
# str_starts : function (string, pattern, negate = FALSE)  
# str_sub : function (string, start = 1L, end = -1L)  
# str_sub<- : function (string, start = 1L, end = -1L, omit_na = FALSE, value)  
# str_subset : function (string, pattern, negate = FALSE)  
# str_to_lower : function (string, locale = "en")  
# str_to_sentence : function (string, locale = "en")  
# str_to_title : function (string, locale = "en")  
# str_to_upper : function (string, locale = "en")  
# str_trim : function (string, side = c("both", "left", "right"))  
# str_trunc : function (string, width, side = c("right", "left", "center"), ellipsis = "...")  
# str_view : function (string, pattern, match = NA)  
# str_view_all : function (string, pattern, match = NA)  
# str_which : function (string, pattern, negate = FALSE)  
# str_wrap : function (string, width = 80, indent = 0, exdent = 0)  
# word : function (string, start = 1L, end = start, sep = fixed(" "))

As you can see, by executing the previous R syntax, we have created a huge list of function names. This list shows all functions and commands that are provided by the stringr package.

 

Example 2: List All Objects in a Package Using ls() Function

Example 2 illustrates how to return a list with all objects contained in an R add-on package.

For this second example, we’ll again use the stringr package as basement.

We can list all objects contained in the stringr package using the ls function as shown below:

ls("package:stringr")             # Apply ls function
#  [1] "%>%"             "boundary"        "coll"            "fixed"           "fruit"           "invert_match"   
#  [7] "regex"           "sentences"       "str_c"           "str_conv"        "str_count"       "str_detect"     
# [13] "str_dup"         "str_ends"        "str_extract"     "str_extract_all" "str_flatten"     "str_glue"       
# [19] "str_glue_data"   "str_interp"      "str_length"      "str_locate"      "str_locate_all"  "str_match"      
# [25] "str_match_all"   "str_order"       "str_pad"         "str_remove"      "str_remove_all"  "str_replace"    
# [31] "str_replace_all" "str_replace_na"  "str_sort"        "str_split"       "str_split_fixed" "str_squish"     
# [37] "str_starts"      "str_sub"         "str_sub<-"       "str_subset"      "str_to_lower"    "str_to_sentence"
# [43] "str_to_title"    "str_to_upper"    "str_trim"        "str_trunc"       "str_view"        "str_view_all"   
# [49] "str_which"       "str_wrap"        "word"            "words"

The previously shown output of the RStudio console consists of all objects of the stringr package.

 

Video, Further Resources & Summary

Do you need more information on the examples of this tutorial? Then you may want to watch the following video of my YouTube channel. I’m explaining the topics of this tutorial in the video.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you may read the other articles of my homepage:

 

Summary: In this R tutorial you have learned how to view all functions of a package.

In this tutorial, we have used the stringr package as basement to obtain and return a list of functions. However, please note that we could apply the same R syntax to other packages such as dplyr, ggplot2, or data.table.

If you have further questions, tell me about it in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top