Find which package a data set is included in - r

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.

Related

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)

How to store test images as data in a package?

I'm writing a package that helps to quantify images using R. I would like to store example images that people can use to test the package but I'm not sure how to store them. Can anyone help?
In general you can put anything you want into the inst/ directory of a package. Let's say you make a directory inst/images in your source package, with files foo1.png, foo2.jpg, and foo3.tif. When the package is installed, you can find them via system.file(), e.g. list the available files via
list.files(system.file("images",package="your_pkg"))
or access them via e.g.
get_image <- function(fn) {
system.file("images",fn,package="your_pkg")
}
png::readPNG(get_image("foo1.png"))
The only possible pitfall I can think of is that CRAN is not enthusiastic about packages with very large footprints, which could be a problem if (1) you have a lot of large images and (2) you want to submit to CRAN.

How can I find where the package I loaded comes from

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.

Can I load a package's data set without installing the package?

In package ISLR, there is a data set called Default.
I want to use that data set, but the ISLR package is not installed on my machine.
data(Default)
# Warning message:
# In data(Default) : data set ‘Default’ not found
library(ISLR)
# Error in library(ISLR) : there is no package called ‘ISLR’
Since I'll probably never use it again, I don't want to install the package. I thought about reading it from the web, but it's not in the linked web page from the package description.
In general, is there a way to load a data set from a package without installing the package?
You can do this from within R:
download.file("http://cran.r-project.org/src/contrib/ISLR_1.0.tar.gz",
dest="ISLR.tar.gz")
untar("ISLR.tar.gz",files="ISLR/data/Default.rda")
L <- load("ISLR/data/Default.rda")
summary(Default)
If you want to keep a copy of the data file:
file.copy("ISLR/data/Default.rda",".")
Clean up:
unlink(c("ISLR.tar.gz","ISLR"),recursive=TRUE)
I'm not sure you can get around having to download the tarball -- in principle you might be able to run untar() directly on a network connection, but I don't think the underlying machinery can actually extract a file without downloading the whole tarball to somewhere on your machine first.
You said, "Since I'll probably never use it again, I don't want to install the package." If the fact that you'll never use it again is your main concern, then perhaps this solution is not quite what you want, but it is probably the simplest solution:
Install the package with install.packages().
Extract and save the dataset that you want.
Uninstall the package with remove.packages().
So the final result is what you want in three simple steps, though the process does involve installing the package, which you hoped to avoid. But you end up without the package in your system that you don't want, so the end result is the same as what you want.

How to know where a R package has been installed

Good moorning
I would like to know if there is a way to find where a package is installed.
Actually, I am currently documenting a package. In my package, I have a function called "read.myfile" which reads a specific kind of file (roughly like read.table).
I have an instance of this kind of file named "myfile.txt" in my package's folder. On my documentation, I want to run an executable example of this function.
That's why I need the path, where the user has installed the package. So with this path, I can obtain the path of the file "myfile.txt" and use the function "read.myfile" in the .Rd help file, which gives help about the function "read.myfile".
Thus my example will be executable wherever the user has installed the package.
I hope my message was clear.
I don't know if it's possible to do that, but if anyone knows, thanks for helping me.
Use the function system.file.
For example:
system.file(package="ggplot2")
[1] "C:/Users/Andrie/Documents/R/win-library/3.0/ggplot2"
You can use installed.packages and subset to get the only the location of the library in which it is installed:
installed.packages()["tools","LibPath"]
[1] "C:/Program Files/R/R-2.15.2/library"

Resources