I need to run 2 different scripts in R without restarting the session. In the first i use the package mgcvand in the second i need gam. I wrote a script which detaches and removes the package mgcv and installs and loads gam.
But still after i want to run the gam() function I get an error telling me that mgcv was not found. Which I interpret as R looking for the mgcv package for installing it...
Here's a MWE:
install.packages(paste(path.pkgs,'mgcv_1.8-7.zip',sep=''),repos=NULL)
require(mgcv)
## FIRST SCRIPT ##
detach(package:mgcv)
remove.packages('mgcv')
unloadNamespace('mgcv')
require(gam)
## SECOND SCRIPT ##
gam(as.formula(t.thr.fm),data=data)
which returns the error:
Error in get(method, envir = home) :
cannot open file 'H:/data/Documents/R/R-3.1.3/library/mgcv/R/mgcv.rdb': No such file or directory
Any ideas instead of restarting the session?
EDIT:
The solution suggested by Floo0 using package::function is unfortunately not an option.
you can tell R to take a function from a specific package via this syntax:
package::function
So in your case (do not detach mgtv) and use
mgcv::gam(...)
gam::gam(...)
If the function is not exported in the namespace of the function you can also try package:::function with 3 :
Related
I am currently doing Logistic Regression on the 'birthwt' data set found within R. This data is found within the package 'MASS'.
However, when I use library(MASS) to retrieve the data, it masks the function of select() from the dplyr package. I use this function almost immediately in my analysis.
After loading the data, I attempt
detach("package:MASS", unload = TRUE)
but I am met with
‘MASS’ namespace cannot be unloaded:
namespace ‘MASS’ is imported by ‘pbkrtest’, ‘car’, ‘lme4’ so cannot be unloaded
I wold really love to sort this out as I have completed all my necessary analysis on the data, but was met with this issue when attempting to knit.
Thank you in advance for any help!
You shouldn't choose unload = TRUE. The default is unload = FALSE, and that's what you need.
Here's the explanation:
In R, packages can be "loaded", which makes them available to other packages that import functions from them. They can also be "attached", which puts them on the search list, so that they are available to the user in the console. If a package is attached, it needs to be loaded, but the reverse is not true.
So if you run detach("package:MASS"), you will remove it from the search list, and in the console, running select() will no longer find the function in MASS. It will still be loaded, so will be available to the other packages that need it.
By the way, using the prefix form MASS::select() or dplyr::select() will work regardless of whether either or both packages are in your search list.
Similar Question
accessing sysdata.rda within package functions
Why This Similar Question Does Not Apply To Me
They were able to actually build it, and apparently it was a Github error for them (not related)
R VERSION
3.4.2 (I tried using 3.4.3 as well but the same problem occurred)
EDIT: I am on Windows 10
Context
I have fully read the following tutorial on R packages and how to include .Rda files in them. I have LazyData in my DESCRIPTION file set as true as well. I have tried both the data/ folder implementation and the R/sysdata.rda implementation using the function devtools::use_data() with the respective options of internal = FALSE and internal = TRUE.
However, when I try to build the package, or use devtools::install (which builds as well I assume), it fails and gives me the following error message:
Error in predict(finalModel, newInput) : object 'finalModel' not found
Where finalModel is stored within my .rda file.
Does anyone know any possible reasons why this might occur?
I also asked a coworker to install the package on his machine, but unfortunately he got the exact same error.
I made another test package as a 'sanity-check' by creating a simple linear model using the lm() function on datasets::swiss, and then made a test package with this newly created model as a .rda file. When I referenced this test model in a function within this test package, it eerily worked, despite the fact that (to the best of my knowledge) I used the exact same steps to create this new R package.
Also, I unfortunately cannot share the code for the package I am creating, but I am willing to share the code for the test package that uses the swiss dataset.
Thank you in advance.
EDIT: My .rda file I am putting in the package was created last year, if that has anything to do with it.
I just solved a similar issue of having object 'objectName' not found that arose during package management. In my case, the issue related to losing the context of variables when working with parallelization.
When using parallel::clusterExport(cl, varlist=c("function-name")), clusterExport looks at .GlobalEnv for variable definitions. This wouldn't come up during debugging, as I always the variables defined in .GlobalEnv. The solution was to state the environment explicitly: parallel::clusterExport(cl, varlist=c("function-name"), envir=environment()). This ensures that the parallel processes have context of the variables within the data/ folder and R/sysdata.rda.
Source
If you have more than one internal file, you must save them together:
usethis::use_data(file_1,
file_2,
file_3,
internal = TRUE,
overwrite = TRUE)
my R package has a function (my_func) that uses function(bar) from another package (foo) which only available on Unix. So I write my code this way, following the suggested packages in Writing R extension:
my_func = function(){
....
if (requireNamespace("foo", quietly = TRUE)) {
foo::bar(..) # also tried bar(...)
} else {
# do something else without `bar`
}
...
}
However, I keep getting such warning message when I run R CMD check:
'loadNamespace' or 'requireNamespace' call not declared from: foo
Is there any way to use platform specific packages without gives such warnings? I tried to include foo in the Description file and the warning disappeared, but then the package cannot be installed on windows because foo is not available for the win.
I think you misread that comment, though it is easy to do because the logic is not clear and incontrovertible.
In that first paragraph that talked about calling packages using require(package) they said it was OK for a test environment or vignette but not from inside a finished function to use library(package) or require(package).
This is considered bad form, because the packages you call inside a function will usurp the order of access your user has set up in her work environment when loading packages.
The method you use above:
package::function()
is the approved of way to use a function within a function without altering the current environment.
But in order to use that function you must still have that package installed on your current machine or in your current working environment (such as a virtualenv or ipython or jupytr..and yes R will work in all those environments).
You cannot run a function that R cannot see from the working environment...so you must have it installed, but not necessarily loaded, to call it.
I am a beginner in R and I am supposed to make a model thanks to regression beta. I learned that I can use betareg() except that even when installing its package the R does not recognize it and displays me error:
Error: could not find function "betareg"
What could be the cause for that?
The error message you are getting typically arises when calling a function.
Before you can call a function, you have to install the package and load the library.
You can try this:
install.packages("betareg")
library(betareg)
and then call the function with relevant parameters
betareg()
I am using a R package Cat
In the help file an example for function data uses dataset head
data(head)
I have my own dataset and I want to try this function data within the package CAT on my dataset A
when I try
library(cat)
A = read.table("C:/A.csv",header=TRUE,sep=",")
data(A)
I get a warning
"Warning message: In data(A) : data set ‘A’ not found*
How do I use specific functions that are part of R packages on my dataset and not the examples in those packages>
As #cenka pointed out, when you are loading your own data, you do not need to use the data() function. That is only for loading data from packages. If you want to use functions from a specific package after you install it, be sure to call library(cat) to actually load the library into your current R session.