Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of object is an R package?

Tags:

Probably a pretty basic question but a friend and I tried to run str(packge_name) and R threw us an error. Now that I'm looking at it, I'm wondering if an R package is like a .zip file in that it is a collection of objects, say pictures and songs, but not a picture or song itself.

If I tried to open a zip of pictures with an image viewer, it wouldn't know what to do until I unzipped it - just like I can't call str(forecast) but I can call str(ts) once I've loaded the forecast package into my library...

Can anyone set me straight?

like image 245
d8aninja Avatar asked Jan 13 '15 16:01

d8aninja


People also ask

How do you check the type of an object in R?

To get type of a value or variable or object in R programming, call typeof() function and pass the value/variable to it.

What is an object in R programming?

Objects are the instance of the class. Also, everything in R is an object and to know more look at Data types in R. They also can have their attributes like class, attributes,dimnnames, names, etc.

What is the type of an object?

An object type is a user-defined composite datatype that encapsulates a data structure along with the functions and procedures needed to manipulate the data. The variables that form the data structure are called attributes.

How many types of objects are there in R?

There are three types of objects that constitute the R language. They are calls, expressions, and names. Since R has objects of type "expression" we will try to avoid the use of the word expression in other contexts.


2 Answers

R packages are generally distributed as compressed bundles of files. They can either be in "binary" form which are preprocessed at a repository to compile any C or Fortran source and create the proper headers, or they can be in source form where the various required files are available to be used in the installation process, but this requires that the users have the necessary compilers and tools installed at locations where the R build process using OS system resources can get at them.

If you read the documentation for a package at CRAN you see they are distributed in set of compressed formats that vary depending on the OS-targets:

Package source:     Rcpp_0.11.3.tar.gz  # the Linus/UNIX targets Windows binaries:   r-devel: Rcpp_0.11.3.zip, r-release: Rcpp_0.11.3.zip, r-oldrel: Rcpp_0.11.3.zip OS X Snow Leopard binaries:     r-release: Rcpp_0.11.3.tgz, r-oldrel: Rcpp_0.11.3.tgz OS X Mavericks binaries:    r-release: Rcpp_0.11.3.tgz Old sources:    Rcpp archive   # not really a file but a web link 

Once installed an R package will have a specified directory structure. The DESCRIPTION file is a text file with specific entries for components that determine whether the local installation meets the dependencies of the package. There are NAMESPACE, LICENSE, and INDEX files. There are directories named '/help', '/html', '/Meta', '/R', and possibly '/libs', '/demo', '/data', '/unitTests', and others.

This is the tree at the top of the ../library/Rcpp package directory:

$ ls CITATION    NAMESPACE   THANKS      examples    libs DESCRIPTION NEWS.Rd     announce    help        prompt INDEX       R       discovery   html        skeleton Meta        README      doc     include     unitTests 

So in the "life-cycle" of a package, there will be initially a series of required and optional files, which then get processed by the BUILD and CHECK mechanisms into an installed package, which than then get compressed for distribution, and later unpacked into a specified directory tree on the users machine. See these help pages:

?.libPaths  # also describes .Library() ?package.skeleton ?install.packages ?INSTALL 

And of course read Writing R Extensions, a document that ships with every installation of R.

like image 83
IRTFM Avatar answered Sep 17 '22 09:09

IRTFM


Your question is:

What type of object is an R package?

Somehow, I’m still missing an answer to this exact question. So here goes:

As far as R is concerned, an R package is not an object. That is, it’s not an object in R’s type system. R is being a bit difficult, because it allows you to write

library(pkg_name) 

Without requiring you to define pkg_name anywhere prior. In contrast, other objects which you are using in R have to be defined somewhere – either by you, or by some package that’s loaded either explicitly or implicitly.

This is unfortunate, and confuses people. Therefore, when you see library(pkg_name), think

library('pkg_name') 

That is, imagine the package name in quotes. This does in fact work just as expected. The fact that the code also works without quotes is a peculiarity of the library function, known as non-standard evaluation. In this case, it’s mostly an unfortunate design decision (but there are reasons).

So, to repeat the answer: a package isn’t a type of R object1. For R, it’s simply a name which refers to a known location in the file system, similar to what you’ve assumed. BondedDust’s answer goes into detail to explain that structure, so I shan’t repeat it here.


1 For super technical details, see Joshua’s and Richard’s comments below.

like image 38
Konrad Rudolph Avatar answered Sep 19 '22 09:09

Konrad Rudolph