bernarducs
bernarducs

Reputation: 71

how to know which function is associated with which package in rstats

Rookie here,

Usually, R-scripts are like follows:

library(packageA)
library(packageB)

functionX(code)
functionY(code)

So, how to know which function is associated with which package?

Why do not code packageA.functionY like other languages?

Upvotes: 0

Views: 66

Answers (2)

vincenzocoia
vincenzocoia

Reputation: 21

There are two questions here with two different answers.

Why do not code packageA.functionY like other languages?

As others have commented, this is indeed possible in R with package_name::function_name(). Doing so does not require the package to be loaded/attached (with, say, library(package_name)). With ::, you also have the advantage of using the function you intended to use -- instead of inadvertently calling a function by the same name from another package. And, as you say, it can improve readability. Keep in mind there's a tiny penalty in runtime by using ::.

So, how to know which function is associated with which package?

The approach is to find the environment containing the function, from which you can identify the package. You can do this using the get_env() function from the rlang package. For example, the sd() function comes from the stats package:

rlang::get_env(sd)
#> <environment: namespace:stats>

Created on 2022-01-17 by the reprex package (v2.0.1)

Unfortunately, get_env() does not work for all R objects, such as data frames. The superseded package pryr has a function, where(), that is more flexible. For example, we can see that mtcars comes from the datasets package:

pryr::where("mtcars")
#> <environment: package:datasets>
#> attr(,"name")
#> [1] "package:datasets"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.1/Resources/library/datasets"

Created on 2022-01-17 by the reprex package (v2.0.1)

Upvotes: 1

koolmees
koolmees

Reputation: 2783

Typically speaking we don't specify the package before a function because it's not needed and it's less writing. That does not mean that it's not possible however. You can use the notation package_name::function_name() to load functions from packages without even the need to library() them. This is mostly used when loaded packages have overlapping function names.

To get a list of all functions used in a certain code the package NCmisc has a very handy function:

NCmisc::list.functions.in.file("app.R")

$.GlobalEnv
[1] "dt.data"

$`c("package:lubridate", "package:data.table")`
[1] "isoweek" "month"   "year"   

$`character(0)`
 [1] "."             "dt.Cycle"      "dt.filter"     "dt.KPI"        "dt.monthly"    "dt.MQQ"        "dt.overunder"  "dt.shops"      "dt.sorterrfid" "dt.WO"         "dt.YTD"       

$`package:base`
 [1] "as.Date"  "c"        "library"  "list"     "max"      "order"    "paste"    "paste0"   "readRDS"  "round"    "sum"      "Sys.Date"

$`package:billboarder`
[1] "bb_legend"         "bb_linechart"      "bb_x_axis"         "bb_y_axis"         "bb_y_grid"         "bbaes"             "billboarder"       "billboarderOutput" "renderBillboarder"

$`package:data.table`
[1] "melt.data.table" "setcolorder"    

$`package:shiny`
 [1] "br"                 "checkboxGroupInput" "conditionalPanel"   "dataTableOutput"    "div"                "downloadButton"     "downloadHandler"    "numericInput"       "radioButtons"      
[10] "reactive"           "renderDataTable"    "selectInput"        "shinyApp"           "tabPanel"           "tabsetPanel"       

$`package:shinydashboard`
[1] "dashboardBody"    "dashboardHeader"  "dashboardPage"    "dashboardSidebar"

$`package:utils`
[1] "write.csv"

When using this function just make sure that you have the packages loaded. It can only check the function names in packages that are in your loaded library.

Upvotes: 1

Related Questions