whether to import "parallel" package in R when using foreach - r

I am using foreach() function in the foreach R package for parallel computing. Besides that function, I think it is also required to use registerDoMC() function in the doMC package.
However, when I write my DESCRIPTION file, the Imports section contains doMC (>= 1.3.0), foreach (>= 1.4.1), but when I run my code, an error indicates: cannot find the iter function. Thus, I also import the iterators package.
It seems that there is still error: the mclapply() function is to be used by foreach(), and this function appears in both the parallel and the multicore package. I include both packages in the Imports section, but when I run search(), the warnings show up:
Warning messages:
1: replacing previous import ‘mclapply’ when loading ‘parallel’
2: replacing previous import ‘mcparallel’ when loading ‘parallel’
3: replacing previous import ‘pvec’ when loading ‘parallel’
This is pretty weird: even though I explicitly imports both packages of iterators and multicore, I still cannot use their functions after loading my own package... Instead, I have to explicitly run:
library(iterators)
library(multicore)
in order to use my own function in my package which makes use of parallel computing. Is there anything wrong in my package writing? Thank you so much!

If you modify your DESCRIPTION file by adding doMC to the "Depends", then the "cannot find the iter function" error should go away, and functions from foreach, iterators and doMC will be available when your package is loaded, which seems to be your preference. The first chapter of Writing R Extensions discusses the differences between "Imports" and "Depends". Generally, it's preferable to use "Imports" to avoid forcing users of your package to load packages that are only needed within a package, but it has uses.
Actually, the "cannot find the iter function" error that you saw is caused by a bug in the doMC package, and using "Depends" rather than "Imports" works around this bug. Your package should only have to import packages that it directly uses, so if you don't explicitly call iter or mclapply, you shouldn't have to import iterators, parallel, or multicore. And since parallel has subsumed multicore, you should never import both parallel and multicore, which should avoid the warning messages that you saw.
I submitted a fix for the doMC bug to the package maintainer, so you should be able to import foreach and doMC into packages without an error in the next version of the package.

Related

Why do I get Error when calling library (rdd)

I have installed the rdd package, when calling library(rdd), I get error an message:
Error: package ‘car’ required by ‘AER’ could not be found"
I have used functions from car (like anova) so I know I have it.
I have MAC OS 10.15.1, R 3.6.1 , I reinstalled R and RStudio.
install.packages("rdd")
library(rdd)
Loading required package: AER
Error: package ‘car’ required by ‘AER’ could not be found
Packages may have dependencies (i.e. other packages) and those dependent package may have dependencies of their very own. Appears that rdd requires AER which in turn requires car. So you need all three (and possibly even more). At the very least you need to now install pkg:car.
(I know this may seem a duplicate but I'm not sure the usual duplicate nominees have seen a multi-level dependency situation such as this. )
Suggest you execute both of this lines:
install.packages("AER", dependencies=TRUE) # should pick up car
# maybe also need install.packages("car", dependencies=TRUE)
install.packages("rdd", dependencies=TRUE)
If you get further errors, pay attention to the first error message and use install package recursively until you establish a full set of dependencies and dependencies of dependencies.

Is there a way to use a package designed for Rstudio in R CLI?

Someone wrote a package (we'll call it 'Thing') that relies heavily on Rstudio to work, including requiring the rstudioapi library. It has an authentication routine that
relies on the rstudioapi::askForPassword("Enter your pw") function.
If I run the setup routine, it throws this sort of error:
> setup_thing()
Thanks for downloading Thing, the custom R package for things.
Loading required package: rstudioapi
Error: RStudio not running
I am trying to avoid re-writing the entire library; Is there a way to install and set up the package in RStudio, but use its configured instance from R CLI?
I think the answer is unfortunately no. But there might be a work around. Have you investigated cronR? https://cran.r-project.org/web/packages/cronR/README.html
You could potentially schedule the jobs within Rstudio itself.

Package Dependencies

I have an R package, that depends on the base64enc library. When I run the source file in the package with Rscript:
Rscript analyzer.R
it runs just fine.
The first line in analyzer.R is:
library(base64enc);
However, when I run a function from the package in the repl:
library(analyzer)
analyze()
It complains that base64enc is not installed.
Error in rawToChar(base64decode(mark[1])) :
could not find function "base64decode"
Calls: analyze ... collect.marks -> lapply -> FUN -> lapply -> FUN -> rawToChar
However, when in the REPL I manually include base64enc:
library(base64enc)
library(analyzer)
analyze()
It works fine. Is there anyway I can tell my analyzer package to use the base64enc library without having to include it in every script every time I use the library?
(Note that a script is not a package.)
When you run your script analyzer.R it explicitly loads base64enc so the package is in your load path.
But your package may just have Imports: base64enc with a corresponding NAMESPACE statement -- that makes the code from base64enc available in you package but does not load it.
Back in the day we used to do Depends: base64enc which would load it too -- but clutters the search path. Imports: is better, but has the very side-effect you observe here. So just load the other package at the REPL.

imported packages in devtools

I develop my R package in RStudio. I use load_all() from devtools (either typing in the console or from the Build menu), which seems to work fine. If I then work on an R function in my package and source it, when I try and run the function I need to load the libraries it depends on, despite these other packages being imported (through the DESCRIPTION file).
So for example, I will get the message:
could not find function "trellis.par.get"
and I need to load the lattice package etc.
Is this expected behaviour? I would have thought devtools would have imported these other package dependencies, or am I misunderstanding something here?
Thanks
David

Package "Imports" not loading in R development package

I am building a package in R in a windows environment using Rstudio, devtools roxygen2 and Rtools.
The package is showing no problems in R CMD CHECK. However when I try to load the package using library("mypkg"), the packages specified under Imports in DESCRIPTION are not being loaded (Loading required package: message is not there). On using pkgDepends("mypkg"), the $Depends is shown as character(0).
I have to load the required packages using library() for mypkg to function.
I am using namespace imports instead of package::function() syntax. All the required packages are there in the NAMESPACE as imports().
Why is this happening? How to solve this?
That's the correct behaviour. Imports just means that code inside your package can see the functions that you import from other packages. The other packages aren't placed on the search path like with Depends.
Further reading:
Better explanation of when to use Imports/Depends

Resources