Questions about R package publishing and code visibility - r

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?

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.

Related

Check for all package dependencies given a package's source code in R

I have inherited a R package from a previous colleague. I am trying to clean up the stated package dependencies in the DESCRIPTION file as I have noticed several of them are superfluous.
Since there are over 30 stated dependencies, however, I would rather not have to check each one manually to see if it is actually needed. I also have a sneaking suspicion that there may be a few dependencies missing from the DESCRIPTION as well.
Ideally I would like to be able to analyze the package's source code to automatically determine what dependencies are needed. I tried to find a tool or package that could do this but haven't had any luck so far.

Link to vignette from readme.rda

I am building an R package. I have several vignettes that I would like to include links to in my README.Rmd.
I know that vignettes are to be built optionally when installing the package.
I do not really understand where I should start. I am in the process of building the package in R studio. I would the user to be able to see the vignette just by clicking at link in the readme on GitHub. Is this possible? How?
The following obviously does not work.
[The main vignette](vignettes/Vignette.html)
You can do this, but it might be more trouble than it's worth.
The problems are
Your package directories are different in the source on Github than they are when your package is installed in R. The link you give would be fine if you actually put Vignette.html in the vignettes directory, but when your package is installed, it will be in doc.
RStudio won't put the processed vignette in either of those locations by default if you just knit Vignette.Rmd.
You don't normally commit output files on Github.
So here's what you could do to work around this. Make the link look like
[The main vignette](doc/Vignette.html)
To make sure that file is there on Github, in RStudio create the doc directory and run
rmarkdown::render("vignettes/Vignette.Rmd", output_file="doc/Vignette.html")
You'll need to commit the output file and push it to Github, but you don't want to include it when you build the .tar.gz file, so you'll also need to add the lines
^doc$
^doc/Vignette.html$
to the .Rbuildignore file in the main package directory.
With all these changes I think your vignette will be visible on Github and also after you install the package in R.
A much simpler approach is just to tell the user to run
vignette("Vignette", package = "yourpackagename")
after installing the package, but this won't make it visible on Github.

Installing the dependencies of a binary package automatically

In R, I have developed my own package for work (let's call it 'foo') and I have built the package and produced a binary foo.zip with the dependencies (e.g. ggplot2) listed in the description file.
When I use the R package installer in the packages tab it doesn't automatically download the dependencies from CRAN. Ultimately I don't want the end user to have to do this and I don't intend to load it to CRAN for the time being.
I have a way with: devtools::install_dep but I don't want the user to have to do this!
Use the remotes package:
remotes::install_deps("~/RStudio/foo/foo.tar.gz")
You don't say how you're expecting users to install the package. I believe if you put it on the web on a CRAN-like repository, and tell your users to install from there, R will by default install dependencies. This involves telling them how to add your repository to the repository list, so it might be just as easy to ask them to install devtools and use devtools::install_dep.
Another possibility is to distribute your package in a source version; then this answer: https://stackoverflow.com/a/38902163/2554330 gives ideas how to proceed. One that works for me to install something like "~/RStudio/foo/foo.tar.gz" is
install_url(paste0("file://", normalizePath("~/RStudio/foo/foo.tar.gz")))
If you are on Windows, you'll probably need a slightly different way to construct the URL.
Distributing binary packages is only convenient if all your users use the same version of R as you do; they aren't guaranteed to work if the minor version number changes.

Include an unofficial Julia package in a REQUIRE file?

I'd like to list an "unofficial" Julia package in my package's REQUIRE file. (That is, the package I'd like to list can only be installed with Pkg.clone, not Pkg.add.) How can I configure my package so that this unofficial package gets installed automatically, when someone clones my package?
Unfortunately there is no way to do this at this time, but it is a requested feature. See this Github issue and this mailing list thread.

What is the difference between a library and a package in R?

In R what is the difference between a library and a package?
I have come across posts where people refer to packages within a library. Based on this idea I interpret it that a package lives in a library (i.e I store my packages with a designated library). However I get confused when I want to use package 'x'.
I am under the imperssion I need to call the library function to get package 'x' to be in use ?
And once I have have called upon package 'x' the functions of package 'x' then become available to me ?
In R, a package is a collection of R functions, data and compiled code. The location where the packages are stored is called the library. If there is a particular functionality that you require, you can download the package from the appropriate site and it will be stored in your library. To actually use the package use the command "library(package)" which makes that package available to you. Then just call the appropriate package functions etc.
1. Package
Package extends basic R functionality and standardizes the distribution of code. For example, a package can contain a set of functions relating to a specific topic or tasks.
Packages can be distributed as SOURCE (a directory with all package components), BINARIES (contains files in OS-specific format) or as a BUNDLE (compressed file containing package components, similar to source).
The most basic package, for example created with,
library(devtools)
create("C:/Users/Documents/R-dev/MyPackage")
contains:
R/ directory where all the R code goes to, and DESCRIPTION and NAMESPACE metadata files.
2. Library
Library is a directory where the packages are stored. You can have multiple libraries on your hard drive.
To see which libraries are available (which paths are searched for packages):
.libPaths()
And to see which packages are there:
lapply(.libPaths(), dir)
To use package ‘x’, it first has to be installed in a package library. This can be done for example, with:
install.packages(‘x’) # to install packages from CRAN
or
R CMD INSTALL Xpackagename.tar.gz #to install directly from source
Once installed it has to be loaded into memory with library(x) or require(x).

Resources