Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know to which package a particular function belongs to in R prior to package loading?

Tags:

package

r

Example, I know many popular functions, to name one like tbl_df(). I usually do not remember which package it belongs to i.e. data.table or dplyr. So I have to always remember and load a package and I can not do ?tbl_df unless I have loaded the correct package.

Is there a way to know to which package a particular function belongs to, prior to loading or installing of the package in R console itself.

Any help is highly appreciated. Thanks.

like image 610
Sowmya S. Manian Avatar asked Oct 12 '16 08:10

Sowmya S. Manian


People also ask

How do you see what packages are being used in R?

To see what packages are installed, use the installed. packages() command. This will return a matrix with a row for each package that has been installed. Below, we look at the first 5 rows of this matrix.

Which R function is used to load a package?

packages() , which as you can expect, installs a given package. library() which loads packages, i.e. attaches them to the search list on your R workspace.

What package is function %>% in R?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).

What is a function package?

Function packages are basically several external functions and subroutines that are grouped or packaged together.


2 Answers

sos package can help! Try:

install.packages("sos")
library(sos)
findFn("str_replace")

Try this as well

lsp <- function(package, all.names = FALSE, pattern) 
{ package <- deparse(substitute(package)) ls( pos = paste("package", package, sep = ":"),
all.names = all.names, pattern = pattern ) }

after running this function, if you want to search for str_replace function in stringr package- lsp(stringr, pattern="*replace")

like image 142
anoopdk Avatar answered Oct 07 '22 01:10

anoopdk


Inspired by @J_F who suggested ??tbl_df: I was looking for 'arima' and had dozens if not hundreds of hits; I narrowed them down using

help.search('arima', fields=c('name'), ignore.case=FALSE, agrep=FALSE)

(most importantly, agrep=FALSE turns off fuzzy matching)

like image 44
James Avatar answered Oct 07 '22 01:10

James