How can I find where the package I loaded comes from - r

I want to programatically save some packages my script has loaded (I mean the actual directory).
I know doing .libPaths() shows the paths where the packages can come from (though I could also load it from somewhere different).
Is there an already build function to get where a package that I loaded comes from ?

My solution would be:
sess.info <- sessionInfo()
names.packages <- names(sess.info$loadedOnly)
find.package(names.packages)
I hope this is what you are searching for.

Related

How to load self-written package documentation with '?my_package_name'?

I made a package in R called "my_package_name". When I run ?my_package_name or ??_my_package_name, no results are found. I want a help file to be loaded in the same way that ?ggplot2 loads a package help file.
I can run ?my_function_name to obtain the help files for my functions. However, this does not work with my package name, even though the description file is complete. I found that help(package = my_package_name) loads a page that contains the description file and help pages, but I would like load a page with ?my_package_name.
I would say that the easiest way is probably to run the following from the usethis package. It will create the file needed to have your package documentation
usethis::use_package_doc()
I suggest reading the documentation of the function to understand what's going on under the hood.

mmetric could not find function R

I have library(rminer) installed yet wondering why mmertric is still not there and unable to use the function.
Anyone has come across this?
#probability of each landmass category
flagdmodel <- naiveBayes(landmass ~ ., data=trainfdata)
#flagdmodel
#predictionmodel
flagprediction <- predict(flagdmodel, testfdata)
mmetric(testfdata$landmass, flagprediction, c("ACC","PRECISION","TPR","F1"))
+ mmetric(testfdata$landmass, flagprediction, c("ACC","PRECISION","TPR","F1"))
mmetric()
Error in mmetric() : could not find function "mmetric"
mmetric()
Error in mmetric() : could not find function "mmetric"
Question: why can't R find functions for the package I installed with RStudio's install tool?
Answer:
When you want to use functions or other objects in packages that aren't in R, you need to do two things:
install.packages("rminer")
library(rminer)
RStudio can do the first step for you with the install tool, but you still need to do the second one. The first step installs the needed directories and files on your computer. The second step loads them into your current R environment.
In RStudio, you can use the packages tab to check both steps. Installed packages will be in the list in that tab. Loaded packages will have a checkmark to the left of the package name.
It may be easier to find, though if you just run the following in your console:
"package:rminer" %in% search()
If the output is TRUE you're good to go. If it's FALSE you need to run library(rminer)

Find which package a data set is included in

Is there anyway that I can find which package a particular dataset belongs to? For example, which package does the dataset "UScereal" belong?
Thanks a lot in advance.
This is what the find() function is for.
> find("iris")
[1] "package:datasets"
> find("UScereal")
[1] "package:MASS"
If an object is in the search path, find() will tell you where it came from. See ?find for more information.
To get more information about a specific dataset, you can also use ?UScereal, which will work if UScereal is in the search path, or ??UScereal if it isn't but its parent package is installed.
To locate a dataset that isn't within an installed package, you can search for it on RDocumentation.org.
You can try ??UScerial. This will search all help files and documentation for strings matching "UScerial" and tell you which package it is from. For example on my machine I get MASS::UScereal.
(This won't work if the package is not installed on your machine, but it will work if it is installed on your machine but not loaded.)
One way is to pull off the datasets database for all your installed packages and then query that for the data you are looking for.
x <- data(package = .packages(all.available = TRUE))$results
x[grep("UScereal",x[,"Item"]),]
Package
"MASS"
LibPath
"C:/Program Files/R/R-3.0.2/library"
Item
"UScereal"
Title
"Nutritional and Marketing Information on US Cereals"
Clearly this requires that you have installed the package. If you haven't, then you would have to search on the web for the correct package.

Check for installed packages in R

Based on the answer to this question: Elegant way to check for missing packages and install them?
I'm using the following code to make sure that all packages are installed when I upgrade R, or set up other users:
list.of.packages <- c("RODBC", "reshape2", "plyr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
I've placed it in my .First function in my .Rprofile, but when I start up R it gives the following error and continues starting up:
Error in match(x, table, nomatch = 0L) :
could not find function "installed.packages"
If I run the code after I get a prompt it works fine. Any ideas why?
Thanks!
It appears from reading ?Startup that:
Next, if a function .First is found on the search path, it is executed
as .First(). Finally, function .First.sys() in the base package is
run. This calls require to attach the default packages specified by
options("defaultPackages").
Now, installed.packages is in the utils package, which is typically one of the default packages. So it's not available at the time .First is called.
Perhaps try replacing installed.packages with utils::installed.packages?
As Josh notes below my eyes skimmed over the piece that addresses this issue directly, namely:
Note that when the site and user profile files are sourced only the
base package is loaded, so objects in other packages need to be
referred to by e.g. utils::dump.frames or after explicitly loading the
package concerned.

How do I load a package without installing it in R?

I have built an R package but I do not want my users to have to install it before using it.
Is there a way to load a package without having to install it?
For example, if I have a package mypackage.tar.gz, is there something like
library("mypackage.tar.gz")
?
I'll join in "the chorus" of suggesting you should really install the package.
That having been said, you can take a look at Hadley's devtools package, which will let you load packages into the workspace without dumping in your global workspace.
The package will have to be untar'd/unzipped and follow the standard R package structure.
In order for this to work, though, your users would have to have the devtools package installed, so ... I'm not sure that this is any type of win for you.
If you only need the code to be loaded without it being installed, take the raw R script and source it:
source(myScript.R)
If you have different functions, you can create an R script that just loads all the necessary source files. What I sometimes do when developing, is name all my functions F_some_function.R and my classes Class_some_function.R. This allows me to source a main file containing following code :
funcdir <- "C:/Some/Path"
files <- dir(funcdir)
srcfiles <- c(grep("^Class_",dir(funcdir),value=T),
grep("^F_",dir(funcdir),value=T)
)
for( i in paste(funcdir,srcfiles,sep="/")) source(i)
If you present them with the tarred file, they can untar themselves using untar() before sourcing the main file.
But honestly, please use a package. You load everything in the global environment (or in a specified environment if you use local=T), but you lose all functionality of a package. Installing a package is no hassle, and removing neither.
If it's a matter of writing rights on the C drive (which is the only possible reason not to use a package that I met in my carreer), one can easily set another library location. R 2.12 actually does this by itself on Windows. See ?.libPaths()

Resources