Exporting function with snowfall - r

I am currently developing an R package and would like to make use of parallelization with the package snowfall. I'd like to call a function (actually an Rcpp wrapper if this matters) from the package itself, so the function is currently not floating around in the environment.
sfInit(parallel=TRUE,cpus=Cpu)
sfExport("RcppFunction")
sfLapply(1:N,function(cc) RccpFunction(args))
sfStop()
Anytime I try to do this I get the following error message:
Error in sfExport("RcppFunction") :
Unknown/unfound variable RcppFunction in export. (local=TRUE)
I already tried some tutorials and online handbook, but did not found yet the solution how to export any function to snowfall within a package. As far as I see it doesn't really have to do that this particular function is an RcppFunction. I hope someone can help! Thanks in advance!
PS: I already found this post, but I cannot export my own package to snowfall - at least this doesn't make sense to me.

Related

Autospearman in R

Did anyone used the AutoSpearman function from "software-analytics/Rnalytica" package and worked for him? I keep having errors while loading the package. You can find here the documentation of AutoSpearman function.

ts_backtesting function from TSstudio is not showing in the list of functions in TSstudio package in R

I have been trying to perform the horse approach for finding the best model to run on the time series data but when I run ts_backtesting function, there is a message in console that
Error in ts_backtesting(x) : could not find function "ts_backtesting"
Can anyone please help me know why I am getting such error and how to avoid that.
I checked the latest version of the package, didn't find this function anymore
https://cran.r-project.org/web/packages/TSstudio/index.html,
instead, a function called 'train_model' can be used to do a similar thing.

Why can R not find function "getStates"

I am trying to create a character map and was recommended to use Phylotools and Ape packages in R.
I have installed the packages but when I try to do function getStates it comes up with an error message:
x<-getStates(nexdata,"tips")
Error in getStates(nexdata, "tips") : could not find function "getStates"
I have installed the right packages (so I think) and I am quite stuck. Any help would be really appreciated. If you need me to explain anything in more detail let me know.
The function library() loads and attaches add-on packages. You are using phytools and ape, so you should have in your code
library(phytools)
library(ape)
You can read more in the documentation, e.g. at rdocumentation.org/packages/base/versions/3.6.1/topics/library.

Importing snowfall into custom R package

I'm developing an R package which needs to use parallelisation as made available by the snowfall package. snowfall doesn't seem to import the same was as other packages like ggplot2, data.table, etc. I've included snowfall, rlecuyer, and snow in the description file, name space file, and as an import argument in the function itself. When I try to access this function, I get the following error:
Error in sfInit() : could not find function "setDefaultClusterOptions"
The sfInit function seems to have a nostart / nostop argument which it says is related to nested usage of sfInit but that doesn't seem to do the trick for me either.
The actual code itself uses an sfInit (which is where I get the error), some sfExports and sfLibrarys, and an sfLapply.
Possible solution:
It seems to work if I move snow from the import section to the depends section in the Desciption file. I don't know why though.
When you include a package in 'Depends' when one attaches your package they also attach the package on which your package Depends to their namespace.
This and other differences between Depends and Imports is explained well in other questions on this site.
If you look at {snowfall}'s DESCRIPTION you'll see that it Depends on {snow}. It is plausible that the authors of snowfall know something we don't and that {snow} has to be attached to the global search path in order to work. In fact that is the top caveat in the top answer to the question I linked above...
... if your package relies on a package A which itself "Depends" on
another package B, your package will likely need to attach A with a
"Depends directive.
This is because the functions in package A were written with the
expectation that package B and its functions would be attached to the
search() path.
So, in your case, it just so happens that all {snowfall} wants is {snow} and you happened to provide it. However, it appears the more correct behavior may be for you to Depend on {snowfall} directly.
setDefaultClusterOptions is a function from the snow package. You need to import that too.

Using glmm_funs.R?

I am fitting a GLMM and I had seen some examples where is used the function: overdisp_fun, deļ¬ned in glmm_funs.R, but I don't know which package contain them or how can I call it from R, can somebody help me?
Thanks,
If you google for glmm_funs.R, you'll find links to the script (eg here: http://glmm.wdfiles.com/local--files/trondheim/glmm_funs.R).
You can save the file on your local machine, then call it in your R session with source("path to file/glmm_funs.R").
You will then be able to use the functions contained in the script, including overdisp_fun().
You can think of it a little bit like loading a package, except the functions are just presented in a script.

Resources