Download package r in the selected environment

I am new to R package development and stack overflow, but I could not find this information anywhere.

I am trying to load R-packet mice without polluting my namespace. I tried to import only those functions that I use, but this did not work. Therefore, I agree to download the entire package in one specific environment as follows:

e <- new.env() load_package_into_environment(e, package = "mice") eval(mice(data, m = m, maxit = max.iter, printFlag = F), envir = e) 

However, I could not find the actual function to replace the placeholder "load_package_into_environment". What function, if any, will accomplish this?

EDIT: Here are the files I'm working with and the problem I have to provide in more detail.

File: DESCRIPTION

 Package: bug.example2 Title: Example of Package Loading Bug Version: 0.0.0.9000 Authors@R : person("R", "Woodbridge", email = " example@gmail.com ", role = c("aut", "cre")) Description: Creates a wrapper function for mice::mice function. Depends: R (>= 3.2.3), data.table (>= 1.9.6) License: LazyData: true Imports: mice RoxygenNote: 5.0.1 

File: NAMSPACE (automatically generated by roxygen)

 import(data.table) importFrom(mice,mice) importFrom(mice,mice.impute.logreg) importFrom(mice,mice.impute.pmm) importFrom(mice,mice.impute.polr) importFrom(mice,mice.impute.polyreg) 

File: impute.R (uses the function of the mice from the mouse package)

 #' @import data.table #' @importFrom mice mice #' @importFrom mice mice.impute.pmm #' @importFrom mice mice.impute.logreg #' @importFrom mice mice.impute.polyreg #' @importFrom mice mice.impute.polr #' @export impute <- function(data, m = 5, max.iter = 5){ mice_environment <- new.env() #Impute missing data using mice function, output format is mids object mice.out <- mice(data, m = m, maxit = max.iter, printFlag = F) #save the m imputed data.frames as a list of data.tables return.list <- lapply(1:m, function(x){ as.data.table(complete(mice.out, x)) }) names(return.list) <- paste0("imp.",1:m) return.list } 

File: test-impute.R (uses a test package to test the impute function)

 context("Impute missing values") test_that("Output format is a list of lenght m and each element is a data.table",{ #Set up data set.seed(200) data <- iris data$Species[runif(nrow(data)) < .1] <- NA data$Sepal.Width[runif(nrow(data)) < .2] <- NA setDT(data) #Create imputed data M <- 5 impute.output <- impute(data, m = M) #Test output format expect_is(impute.output, "list") expect_equal(length(impute.output), M) lapply(impute.output,expect_is, "data.table") }) 

Error output from testthat

 1. Error: Output format is a list of lenght m and each element is a data.table - The following functions were not found: mice.impute.pmm, mice.impute.polyreg 1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls, message = function(c) invokeRestart("muffleMessage")) 2: eval(code, new_test_environment) 3: eval(expr, envir, enclos) 4: impute(data, m = M) at test-impute.R:12 5: mice(data, m = m, maxit = max.iter, printFlag = F) at C:\repos\bug.example2/R/impute.R:11 6: check.method(setup, data) 7: stop(paste("The following functions were not found:", paste(fullNames[notFound], collapse = ", "))) 
+5
source share
2 answers

The 'mice' package internally calls imputation methods from the global environment. According to the authors, this allows you to provide your own methods for imputation. Thus, the package should also disclose its default implementations in the global environment. I think this is a good example of pure judgment. Internal methods are now recognized by the package only through the global environment. This is primarily detrimental to the purpose of code packaging. If you want to allow the use of external functions by your package, simply specify the API to transfer them to the package.

The error message that you see is triggered by the function 'mice: check_method':

 notFound <- !vapply(fullNames, exists, logical(1), mode = "function", inherits = TRUE) 

My workaround is to re-export the methods used internally by โ€œmiceโ€ from my own package (which internally uses โ€œmiceโ€, just like yours). Just enter one of your R files and run roxygen2 on it:

 #' @importFrom mice mice #' @export mice.impute.pmm <- mice::mice.impute.pmm #' @export mice.impute.polyreg <- mice::mice.impute.polyreg 

Of course, you need to export other methods if they are used in your data.

My point is that if I need to pollute the global environment, I will pollute it minimally, only with the functions necessary for the work of "mice".

+1
source

Yesterday I got the same error and open a similar question (which btw has already disabled). Today I found a solution, I hope this works for you too, and maybe you (or others) can highlight it a bit.

I am developing the R package and using roxygen2 to document features. I have listed the mice in the import section in the description file, since I have to use the mice :: mice function. When creating and checking the package, everything works smoothly until I actually run a function that calls the mice :: mice. At this moment, I get the same error you are getting.

As far as I understand, this causes a problem: In the part of the documentation using the mouse, you need to add this bit:

 #' @importMethodsFrom mice #' @importFrom mice mice 

Please note that with most packages I have used so far, the following line would be more than enough:

 #' @importFrom mice mice 

Obviously, mice also need to add the @importMethodsFrom directive. I guess this is because it uses S4 classes, but I know little about them, so I just know that it works that way.

I found out about this by reading the "S4" section on this page: http://r-pkgs.had.co.nz/namespace.html#imports

So, to get back to your specific case, I assume that the documents for your function should look something like this.

 #' @import data.table #' @importFrom mice mice #' @importMethodsFrom mice #' @export impute <- function(data, m = 5, max.iter = 5){...} 

It worked for me. Hope this helps you too.

0
source

Source: https://habr.com/ru/post/1245199/