I have two R scripts. scriptB is called in scriptA via source("scriptB.R")
both scriptA and scriptB load the same library(x)
where x is knitr
or stringr
or a few other packages
However, because scriptB calls library(x)
for example, it seems scriptA is forced to unload its library(x)
and then load up the scriptB library(x)
. This leads to the following error:
Error in unloadNamespace(package) :
namespace 'x' is imported by 'y' so cannot be unloaded
Error in library(x) : Package 'x' version n.nn cannot be unloaded
I don't actually call unloadNamespace in the source script so I'm not sure why this is happening? How can I prevent the unload. Should I just use require()
in scriptA so it can fail gracefully?
So my questions are:
Is sourcing the R script the best way to include objects from the R script or is there a friendlier way
How do I avoid the script from trying to unload package x?
OK I sorted it. The problem I think was I was using old versions of stringr in scriptA or B so I updated the package and now it runs fine. I suppose R unloads the same library if there is a versioning difference and probably prefers the library that is called last. That's my only explanation.
I remain a little confused however as I didn't specify a version of the library when loading it and I though rstudio only keeps one version of a package.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.