From inside a function, How would you add a package on the search list?


#1

In one of my packages, fgeo, I'm trying to write a function to list all functions and datasets in itself (fgeo). For that I first need to add fgeo on the search list. I know I'm supposed to avoid calling library() in functions of my packages but in this particular situation I can't think of an alternative.

Do you have a better idea?

add_fgeo_on_search_list <- function() {
  library(fgeo)
  search()
}

add_fgeo_on_search_list()
#> -- Attaching packages -------------------------------------------- fgeo 0.0.0.9000 --
#> v bciex           0.0.0.9000     v fgeo.habitat    0.0.0.9001
#> v fgeo.abundance  0.0.0.9002     v fgeo.map        0.0.0.9203
#> v fgeo.demography 0.0.0.9000     v fgeo.tool       0.0.0.9002
#> 
#>  [1] ".GlobalEnv"              "package:fgeo.tool"      
#>  [3] "package:fgeo.map"        "package:fgeo.habitat"   
#>  [5] "package:fgeo.demography" "package:fgeo.abundance" 
#>  [7] "package:bciex"           "package:fgeo"           
#>  [9] "package:stats"           "package:graphics"       
#> [11] "package:grDevices"       "package:utils"          
#> [13] "package:datasets"        "package:methods"        
#> [15] "Autoloads"               "package:base"

#2

You don't need to attach your package to list the functions exported from it. getNamespaceExports() from the base package will return all exported functions / datasets from a given namespace. e.g.

getNamespaceExports("glue")
#>  [1] "trim"                 "glue_sql"             "single_quote"        
#>  [4] "as_glue"              "glue_col"             "glue"                
#>  [7] "backtick"             "glue_data_sql"        "glue_data"           
#> [10] "double_quote"         "identity_transformer" "collapse"            
#> [13] "glue_data_col"        "evaluate"

#3

Thanks, that is awesome!
Is there an equally elegant function to get -- not the functions -- but the datasets exorted by a package?

For example, I can't find the dataset diamonds in ggplot2.

any(grepl("diamonds", getNamespaceExports("ggplot2")))
#> [1] FALSE

#4

Here is a little thank you note: https://goo.gl/d8bcTh.


#5

It's not a single call, but if you use data(package = "fgeo"), you'll be given info about all the datasets in the fgeo package. If printed, this shows a nicely-formatted page in a new window. But if you assign the output to an object, you can examine it. The results element of that list is a character matrix with all the info you'll need:

data_info <- data(package = "ggplot2")
head(data_info[["results"]])
#      Package   LibPath                                       Item            
# [1,] "ggplot2" "C:/Users/nwerth/Documents/R/win-library/3.4" "diamonds"      
# [2,] "ggplot2" "C:/Users/nwerth/Documents/R/win-library/3.4" "economics"     
# [3,] "ggplot2" "C:/Users/nwerth/Documents/R/win-library/3.4" "economics_long"
# [4,] "ggplot2" "C:/Users/nwerth/Documents/R/win-library/3.4" "faithfuld"     
# [5,] "ggplot2" "C:/Users/nwerth/Documents/R/win-library/3.4" "luv_colours"   
# [6,] "ggplot2" "C:/Users/nwerth/Documents/R/win-library/3.4" "midwest"       
#      Title                                     
# [1,] "Prices of 50,000 round cut diamonds"     
# [2,] "US economic time series"                 
# [3,] "US economic time series"                 
# [4,] "2d density estimate of Old Faithful data"
# [5,] "'colors()' in Luv space"                 
# [6,] "Midwest demographics"

You can make a simple wrapper to just get the object names:

get_datasets <- function(package) {
    dinfo <- data(package = package)
    dinfo[["results"]][, "Item"]
}

get_datasets("ggplot2")
# [1] "diamonds"       "economics"      "economics_long" "faithfuld"     
# [5] "luv_colours"    "midwest"        "mpg"            "msleep"        
# [9] "presidential"   "seals"          "txhousing"