scoping

Scoping problem when sfApply is used within function (package snowfall - R)

风格不统一 提交于 2019-11-28 04:16:45
问题 Let me add another scoping problem in R, this time with the snowfall package. If I define a function in my global environment, and I try to use that one later in an sfApply() inside another function, my first function isn't found any more : #Runnable code. Don't forget to stop the cluster with sfStop() require(snowfall) sfInit(parallel=TRUE,cpus=3) func1 <- function(x){ y <- x+1 y } func2 <- function(x){ y <- sfApply(x,2,function(i) func1(i) ) y } y <- matrix(1:10,ncol=2) func2(y) sfStop()

Why does using the same count variable name in nested FOR loops work?

。_饼干妹妹 提交于 2019-11-27 23:39:42
问题 Why does the following not give an error? for (int i=0; i<10; ++i) // outer loop { for (int i=0; i<10;++i) // inner loop { //...do something } //...do something else } The way I understand it, variables in braces ({...}) are in scope only within these braces. But the inner loop is inside the braces of the outer loop. So as soon as I declare int i=0 for the inner loop, shouldn't I get an error about multiple definitions? 回答1: You are actually making a new variable with the same name as another

Is it a good practice to call functions in a package via ::

杀马特。学长 韩版系。学妹 提交于 2019-11-27 17:59:29
I'm writing some R functions that employ some useful functions in other packages like stringr and base64enc . Is it good not to call library(...) or require(... ) to load these packages first but to use :: to directly refer to the function I need, like stringr::str_match(...) ? Is it a good practice in general case? Or what problem might it induce? It all depends on context. :: is primarily necessary if there are namespace collisions , functions from different packages with the same name. When I load the dplyr package, it provides a function filter , which collides with (and masks) the filter

Scoping and functions in R 2.11.1 : What's going wrong?

耗尽温柔 提交于 2019-11-27 14:59:13
问题 This question comes from a range of other questions that all deal with essentially the same problem. For some strange reason, using a function within another function sometimes fails in the sense that variables defined within the local environment of the first function are not found back in the second function. The classical pattern in pseudo-code : ff <- function(x){ y <- some_value some_function(y) } ff(x) Error in eval(expr, envir, enclos) : object 'y' not found First I thought it had

What are the double colons (::) in R?

匆匆过客 提交于 2019-11-27 12:49:31
I am following a tutorial in Rbloggers and found the use of double colons, I looked online, but I couldn't find an explanation for their use. Here is an example of their use. df <- dplyr::data_frame( year = c(2015, NA, NA, NA), trt = c("A", NA, "B", NA) ) I understand it creates a data frame but I don't understand their purpose. As you probably have looked up the help page by now usage of :: helps to access the exact function from that specific package. When you load dplyr you probably got a message as follows.. The following objects are masked from ‘package:base’: intersect, setdiff, setequal

While without global

梦想的初衷 提交于 2019-11-27 09:46:21
This snippet of code is from JuliaBoxTutorials myfriends = ["Ted", "Robyn", "Barney", "Lily", "Marshall"] i = 1; while i <= length(myfriends) friend = myfriends[i] println("Hi $friend, it's great to see you!") i += 1 end giving this error when run with Julia v1.0 UndefVarError: i not defined Stacktrace: [1] top-level scope at ./In[12]:5 [inlined] [2] top-level scope at ./none:0 But when i += 1 is replaced with global i += 1 it works. I guess this was still working in v0.6 and the tutorial will be adapted once the new Intro to Julia is published this Friday. I was just wondering, is it possible

How can a non-imported method in a not-attached package be found by calls to functions not having it in their namespace?

♀尐吖头ヾ 提交于 2019-11-27 04:13:09
An R namespace acts as the immediate environment for all functions in its associated package. In other words, when function bar() from package foo calls another function, the R evaluator first searches for the other function in <environment: namespace:foo> , then in "imports.foo" , <environment: namespace:base> , <environment: R_GlobalEnv> , and so on down the search list returned by typing search() . One nice aspect of namespaces is that they can make packages act like better citizens: unexported functions in <environment: namespace:foo> and functions in imports:foo are available only: (a) to

Java scoping rules and inner classes

陌路散爱 提交于 2019-11-27 02:57:30
问题 All the crazy Java scoping rules are making my head spin and the public static void nonsense isn't helping matters. So far all the programming languages I have used either lexical scoping or some approximation of it without any access modifiers, i.e. inner stuff captures outer stuff and has access to the outer stuff as long as the inner stuff exists. So how do I make sense of the scoping rules for inner classes in Java? Do they get access to variables declared in the outer class or is there

attach() inside function

与世无争的帅哥 提交于 2019-11-27 02:40:33
问题 I'd like to give a params argument to a function and then attach it so that I can use a instead of params$a everytime I refer to the list element a. run.simulation<-function(model,params){ attach(params) # # Use elements of params as parameters in a simulation detach(params) } Is there a problem with this? If I have defined a global variable named c and have also defined an element named c of the list "params" , whose value would be used after the attach command? 回答1: Noah has already pointed

could not find function inside foreach loop

回眸只為那壹抹淺笑 提交于 2019-11-27 01:21:32
问题 I'm trying to use foreach to do multicore computing in R. A <-function(....) { foreach(i=1:10) %dopar% { B() } } then I call function A in the console. The problem is I'm calling a function Posdef inside B that is defined in another script file which I source. I had to put Posdef in the list of export argument of foreach : .export=c("Posdef") . However I get the following error: Error in { : task 3 failed - "could not find function "Posdef"" Why cant R find this defined function? 回答1: The