HenrikBengtsson / doFuture

:rocket: R package: doFuture - Use Foreach to Parallelize via Future Framework
https://doFuture.futureverse.org
84 stars 6 forks source link

Cannot find function within package #36

Closed ShixiangWang closed 5 years ago

ShixiangWang commented 5 years ago

Dear author,

When I tried to use doFuture instead of doParallel in my package, it cannot find my functions within the package (not exported). How should I do?

> registerDoFuture()
> plan(multiprocess, workers = 2)
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
> ext <- foreach(file = c("324-fds", "yes")) %dopar% isValidAndUnreserved(file)
Error in { : task 1 failed - "没有"isValidAndUnreserved"这个函数"

I think you can reproduce this issue by putting this function to a newly created package project with usethis::create_package()

# https://stackoverflow.com/questions/8396577/check-if-character-value-is-a-valid-r-object-name
isValidAndUnreserved <- function(string) {
  make.names(string) == string
}

Best, Shixiang

HenrikBengtsson commented 5 years ago

Could you please clarify; What I don't understand is that you say you have a package, say, MyPkg, which has a non-exported function isValidAndUnreserved(). In other words, in order to use that function from outside the package, you need to use MyPkg:::isValidAndUnreserved(). So, wjy do you assume that

> library(MyPkg)
> ext <- foreach(file = c("324-fds", "yes")) %dopar% isValidAndUnreserved(file)

would work? In order for that to work, isValidAndUnreserved() need to be exported, or you have need to use:

> library(MyPkg)
> ext <- foreach(file = c("324-fds", "yes")) %dopar% MyPkg:::isValidAndUnreserved(file)
ShixiangWang commented 5 years ago

@HenrikBengtsson Sorry for my poor English. I want to say I am developing a package, which has functions I don't want to export. And I tried using doFuture to call them in my package but not work, the doFuture cannot find them.

HenrikBengtsson commented 5 years ago

Could you please create a minimum R package that reproduces this problem and push the package to GitHub or somewhere else where I can clone it? Because what you are describing should work.

PS. And of course, make sure your packages are up to date.

ShixiangWang commented 5 years ago

Thanks, you can easily test it with the following instructions.

In RStudio.

> library(usethis)
> create_package("~/test/test")

It will open a new RStudio Window.

> library(usethis)
> use_r("test")

Put the code to test.R.

# https://stackoverflow.com/questions/8396577/check-if-character-value-is-a-valid-r-object-name
isValidAndUnreserved <- function(string) {
  make.names(string) == string
}

test = function() {
  require(doFuture)
  doFuture::registerDoFuture()
  future::plan(multiprocess, workers = 2)
  ext <- foreach(file = c("324-fds", "yes")) %dopar% isValidAndUnreserved(file)
  ext
}

test2 = function() {
  require(foreach)
  require(doParallel)
  doParallel::registerDoParallel(cores = 2)
  ext <- foreach(file = c("324-fds", "yes")) %dopar% isValidAndUnreserved(file)
  ext
}

Save and then load the package.

> devtools::load_all(".")
Loading test

Test the functions:

> test()
> test2()
ShixiangWang commented 5 years ago

I fell awkward that the doFuture works in this minimal package. I will check the practice code again and let you know.

Sorry to bother you.

Best, Shixiang

HenrikBengtsson commented 5 years ago

Note that devtools to all kinds of tricks to emulate R CMD check - so it might not get it right. Also, make sure to test as bare bone as possible and using fresh R sessions if you do it from within R.

ShixiangWang commented 5 years ago

Yes, I cannot get the right answer when using load_all() but it run successfully after I install this package. However, I got too many extra startup messages, is there a way to hide them?

And I have a question about workers in ?future::plan. I watched the video about workers, they are similar to workers in a real factory, but I cannot find any explanation about the difference between workers in future package and cores in other packages like doMC, doParallel. When I set workers = 2, I see 4 new sessions started.

> devtools::load_all(".")
> cn_prepare <- derive(cn, cores = 4)
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
 Error in { : task 1 failed - "没有"getBPnum"这个函数" 
> library(sigminer)
> load(system.file("extdata", "toy_copynumber.RData",
+                  package = "sigminer", mustWork = TRUE
+ ))
> cn_prepare <- derive(cn, cores = 4)
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Fit feature: bp10MB
2 : *
3 : *
4 : *
5 : *
6 : *
7 : *
8 : *
9 : *
10 : *
Select 2 according to BIC
Finally, select 2 after passing threshold 0.1
===================
Fit feature: copynumber
2 : *

Fit feature: changepoint
2 : *
3 : *
4 : *
5 : *

Select 2 according to BIC
Finally, select 2 after passing threshold 0.1
===================
Fit feature: bpchrarm
2 : *
3 : *
4 : *
5 : *
6 : *
7 : *
8 : *
9 : *
10 : *
Select 3 according to BIC
Finally, select 3 after passing threshold 0.1
===================
Fit feature: osCN
2 : *
3 : *
4 : *
5 : *
6 : *
7 : *
8 : *
9 : *
10 : *
Select 2 according to BIC
Finally, select 2 after passing threshold 0.1
===================
Fit feature: segsize
2 : *
3 : *
4 : *
5 : *
6 : *
7 : *
8 : *
9 : *
10 : *
Select 8 according to BIC
Finally, select 8 after passing threshold 0.1
===================
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman
Using library: /Users/wsx/R_library
载入需要的程辑包:pacman