0

When I use a package in R I install it and use it with loading it. Now what if I add a package which uses another package? Is this package automatically downloaded and loaded too? Or is it in general forbidden for a R package to use another package? I don't think that.

Suppose I want to publish a R package. Within my code, can I use functions from other packages and install and load these packages? Or how does this work when I need functions from other packages? Do I have to implement a message that this and that package is needed and that the user has to install and load it prior to it and I need to implement error catching functions in case the package cannot be found on the pc system?

When I want to publish a R package, can I use/call Java code within my package/code?

For a package which was already published - so let's take just as an example the fGarch package - I would like to see the complete code. How can I see this? I know that R is open source and I think it is more or less possible to just enter a function empty and get the code displayed, but sometimes this does not work and especially my question is: Is there a way I can look into the whole code of the package?

For a package which was already published, is it possible to see and look into all files which were submitted? So like a repository as git where all files are submitted - the code itself and further files which are needed like description files or whatever - and I can see these files and look into them?

Furthermore regarding this post here and hiding functions: Is there code in a R package which I cannot see as an end user? This refers also to my previous question, how can I or which way can I see the whole code in a R package?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Stat Tistician
  • 813
  • 5
  • 17
  • 45
  • 2
    (1) *"Java code"*, see [`rJava`](https://cran.r-project.org/web/packages/rJava/index.html). (2) *"see the complete code"*: I often to go to https://github.com/cran to look at source for individual packages' source code, and many packages list their github/gitlab/r-forge repo on their CRAN package page. (3) *"code ... which I cannot see"* is often a problem with S4 and `R6` OOP code, and certainly with compiled code (C, C++, `Rcpp`), but if you have concerns over hiding proprietary code then R may not be your answer. – r2evans May 27 '20 at 16:26
  • You seem to want to get serious about making a package! You may have a look here : http://r-pkgs.had.co.nz/ (this helped me a lot when creating my first package) – py_b May 24 '20 at 10:57

1 Answers1

5

I guess you have a few different questions here. Let's take them in the order you asked them:

What if I add a package which uses another package? Is this package automatically downloaded and loaded too? Or is it in general forbidden for a R package to use another package?

It is certainly not forbidden for an R package to use another R package. In fact, the majority of R packages rely on other packages.

The source code for each R package must include a text-based DESCRIPTION file in the root directory. In this file you will find (among other things) a "Depends" field, and an "Imports" field. Together, these two fields list all the other packages required to use this package. If a user doesn't already have these other packages installed in their local library, R will install them automatically when it installs the requested package.

If your package lists a dependency in "Depends", then the dependency package is attached whenever your package is attached. Thus if you looked at the source code for a package called "foo" and you see that its DESCRIPTION file contains the line

Depends: bar,

you know that when you call library(foo) in your R console, you have effectively done library(bar); library(foo)

This isn't always ideal. The package foo might only need a couple of functions from package bar, and bar might contain some other functions whose names could clash with other commonly used functions. Therefore, in general, if you are writing a package and you only want to use a few functions from another package, it would be better to use "Imports" rather than "Depends" to limit the number of unnecessary symbols being added to your user's search path.


Suppose I want to publish a R package. Within my code, can I use functions from other packages and install and load these packages

Yes, you can use functions from other packages. The simplest way to do this is to include the name of the package in the Depends field of your DESCRIPTION file.

However, when using just a few functions from another package inside your own package, best practice is to use the "Imports" field in the DESCRIPTION file, and use a namespace qualifier for the imported function in your actual R code. For example, if you wanted to use ggplot from the ggplot2 package, then inside your function you would call it ggplot2::ggplot rather than just ggplot.

If you publish your package for others to use, the dependencies will be installed automatically along with your package if the user calls install.packages with the default settings. For example, when I did:

install.packages("fGarch")

I got the associated message:

#> also installing the dependencies ‘timeSeries’, ‘fBasics’, ‘fastICA’

Do I have to implement a message that this and that package is needed and that the user has to install and load it prior to it and I need to implement error catching functions in case the package cannot be found on the pc system?

No, not in general. R will take care of this as long as you have listed the correct packages in your DESCRIPTION file.


When I want to publish a R package, can I use/call Java code within my package/code?

R does not have a native Java API, but you can use your own Java code via the rJava package, which you can list as a dependency for your package. However, there are some users who have difficulty getting Java to run, for example business and academic users who may use R but do not have Java installed and do not have admin rights to install it, so this is something to bear in mind when writing a package.


For a package which was already published - so let's take just as an example the fGarch package - I would like to see the complete code. How can I see this?

Every package available for download from CRAN has its source code available. In the case of fGarch, its CRAN page contains a link to the gzipped tarball of the source code. You can download this and use untar in R to review all the source code. Alternatively, many packages will have an easily-found repository on Github or other source-control sites where you can examine the source code via a browser. For example, you can browse the fGarch source on Github here.


For a package which was already published, is it possible to see and look into all files which were submitted? So like a repository as git where all files are submitted - the code itself and further files which are needed like description files or whatever - and I can see these files and look into them?

Yes, you can look at all the sources files for all the packages uploaded to CRAN on Github at the unofficial Github CRAN mirror here


Is there code in a R package which I cannot see as an end user? This refers also to my previous question, how can I or which way can I see the whole code in a R package?

As above, you can get the source code for any package via CRAN or Github. As you said, you can look at the source code for exported functions just by typing the name of that function into R. For unexported functions, you can do the same with a triple colon. For example, ggplot2:::adjust_breaks allows you to see the function body of the unexported function adjust_breaks from ggplot2. There are some complexities when an object-oriented system like S4, ggproto or R6 is used, or when the source code includes compiled C or C++ code, but I haven't come across a situation yet in which I was not able to find the relevant source code after a minute or two with an R console and a good search engine.

Community
  • 1
  • 1
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • 2
    Your advice to add dependencies to `Depends` indiscriminately runs against what most consider to be best practice (e.g. https://kbroman.org/pkg_primer/pages/depends.html & http://r-pkgs.had.co.nz/description.html#dependencies & https://stackoverflow.com/a/8638902/8386140). Adding them in `Imports` where feasible is better both b/c it results in less packages being loaded when your package is loaded & b/c it's safer in terms of ensuring the proper versions of functions are called as you expect. The issue can actually be complex, but the answer would be improved with at least a nod to this issue – duckmayr May 29 '20 at 22:43
  • 1
    You're right of course @duckmayr. The OP is clearly taking the first few steps in package development, and I'm often guilty of trying to make an answer a bit too simple (as long as it works), because the nuances might make the answer off-puttingly complicated for a novice. On the other hand, I don't want to instill sloppy practices. I'll see if I can find a way to cover this without over-complicating. Thanks for taking the time to provide the feedback - it's appreciated. – Allan Cameron May 29 '20 at 22:54
  • No worries! It is a *very* detailed answer already, and you're certainly right that there's only so much detail that can go into a Stack Overflow answer and that too much detail can be off-putting for a novice. Good luck striking the right balance, I just wanted to bring up that issue. – duckmayr May 29 '20 at 22:57