Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9157255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:58:12+00:00 2026-06-17T12:58:12+00:00

Update 2 @G. Grothendieck posted two approaches. The second one is changing the function

  • 0

Update 2
@G. Grothendieck posted two approaches. The second one is changing the function environment inside a function. This solves my problem of too many coding replicates. I am not sure if this is a good method to pass through the CRAN check when making my scripts into a package. I will update again when I have some conclusions.

Update

I am trying to pass a lot of input argument variables to f2 and do not want to index every variable inside the function as env$c, env$d, env$calls, that is why I tried to use with in f5 and f6(a modified f2). However, assign does not work with with inside the {}, moving assign outside with will do the job but in my real case I have a few assigns inside the with expressions which I do not know how to move them out of the with function easily.

Here is an example:

## In the <environment: R_GlobalEnv>
a <- 1
b <- 2
f1 <- function(){
    c <- 3
d <- 4
f2 <- function(P){
    assign("calls", calls+1, inherits=TRUE)
    print(calls)
    return(P+c+d)
 }
calls <- 0
v <- vector()
for(i in 1:10){
    v[i] <- f2(P=0)
    c <- c+1
    d <- d+1
  }
 return(v)
}
f1()

Function f2 is inside f1, when f2 is called, it looks for variables calls,c,d in the environment environment(f1). This is what I wanted.

However, when I want to use f2 also in the other functions, I will define this function in the Global environment instead, call it f4.

f4 <- function(P){
  assign("calls", calls+1, inherits=TRUE)
  print(calls)
  return(P+c+d)
}

This won’t work, because it will look for calls,c,d in the Global environment instead of inside a function where the function is called. For example:

f3 <- function(){
  c <- 3
  d <- 4
  calls <- 0
  v <- vector()
  for(i in 1:10){
    v[i] <- f4(P=0) ## or replace here with f5(P=0)
    c <- c+1
    d <- d+1
  }
  return(v)
}
f3()

The safe way should be define calls,c,d in the input arguments of f4 and then pass these parameters into f4. However, in my case, there are too many variables to be passed into this function f4 and it would be better that I can pass it as an environment and tell f4 do not look in the Global environment(environment(f4)), only look inside the environment when f3 is called.

The way I solve it now is to use the environment as a list and use the with function.

f5 <- function(P,liste){
  with(liste,{
     assign("calls", calls+1, inherits=TRUE)
     print(calls)
     return(P+c+d)
     }
  )
}
f3 <- function(){
  c <- 3
  d <- 4
  calls <- 0
  v <- vector()
  for(i in 1:10){
    v[i] <- f5(P=0,as.list(environment())) ## or replace here with f5(P=0)
    c <- c+1
    d <- d+1
  }
  return(v)
}
f3()

However, now assign("calls", calls+1, inherits=TRUE) does not work as it should be since assign does not modify the original object. The variable calls is connected to an optimization function where the objective function is f5. That is the reason I use assign instead of passing calls as an input arguments. Using attach is also not clear to me. Here is my way to correct the assign issue:

f7 <- function(P,calls,liste){
  ##calls <<- calls+1
  ##browser()
  assign("calls", calls+1, inherits=TRUE,envir = sys.frame(-1))
  print(calls)
  with(liste,{
    print(paste('with the listed envrionment, calls=',calls))
    return(P+c+d)
  }
  )
}
########
##################
f8 <- function(){
  c <- 3
  d <- 4
  calls <- 0
  v <- vector()
  for(i in 1:10){
    ##browser()
    ##v[i] <- f4(P=0) ## or replace here with f5(P=0)
    v[i] <- f7(P=0,calls,liste=as.list(environment()))
    c <- c+1
    d <- d+1
  }
  f7(P=0,calls,liste=as.list(environment()))
  print(paste('final call number',calls))
  return(v)
}
f8()

I am not sure how this should be done in R. Am I on the right direction, especially when passing through the CRAN check? Anyone has some hints on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:58:13+00:00Added an answer on June 17, 2026 at 12:58 pm

    (1) Pass caller’s environment. You can explicitly pass the parent environment and index into it. Try this:

    f2a <- function(P, env = parent.frame()) {
        env$calls <- env$calls + 1
        print(env$calls)
        return(P + env$c + env$d)
    }
    
    a <- 1
    b <- 2
    # same as f1 except f2 removed and call to f2 replaced with call to f2a
    f1a <- function(){
        c <- 3
        d <- 4
        calls <- 0
        v <- vector()
        for(i in 1:10){
            v[i] <- f2a(P=0)
            c <- c+1
            d <- d+1
          }
         return(v)
    }
    f1a()
    

    (2) Reset called function’s environment We can reset the environment of f2b in f1b as shown here:

    f2b <- function(P) {
        calls <<- calls + 1
        print(calls)
        return(P + c + d)
    }
    
    a <- 1
    b <- 2
    # same as f1 except f2 removed, call to f2 replaced with call to f2b
    #  and line marked ## at the beginning is new
    f1b <- function(){
        environment(f2b) <- environment() ##
        c <- 3
        d <- 4
        calls <- 0
        v <- vector()
        for(i in 1:10){
            v[i] <- f2b(P=0)
            c <- c+1
            d <- d+1
          }
         return(v)
    }
    f1b()
    

    (3) Macro using eval.parent(substitute(…)) Yet another approach is to define a macro-like construct which effectively injects the body of f2c inline into f1c1. Here f2c is the same as f2b except for the calls <- calls + 1 line (no <<- needed) and the wrapping of the entire body in eval.parent(substitute({...})). f1c is the same as f1a except the call to f2a is replaced with a call to f2c .

    f2c <- function(P) eval.parent(substitute({
        calls <- calls + 1
        print(calls)
        return(P + c + d)
    }))
    
    a <- 1
    b <- 2
    f1c <- function(){
        c <- 3
        d <- 4
        calls <- 0
        v <- vector()
        for(i in 1:10){
            v[i] <- f2c(P=0)
            c <- c+1
            d <- d+1
          }
         return(v)
    }
    f1c()
    

    (4) defmacro This is almost the same as the the last solution except it uses defmacro in the gtools package to define the macro rather than doing it ourself. (Also see the Rcmdr package for another defmacro version.) Because of the way defmacro works we must also pass calls but since it’s a macro and not a function this just tells it to substitute calls in and is not the same as passing calls to a function.

    library(gtools)
    
    f2d <- defmacro(P, calls, expr = {
        calls <- calls + 1
        print(calls)
        return(P + c + d)
    })
    
    a <- 1
    b <- 2
    f1d <- function(){
        c <- 3
        d <- 4
        calls <- 0
        v <- vector()
        for(i in 1:10){
            v[i] <- f2d(P=0, calls)
            c <- c+1
            d <- d+1
          }
         return(v)
    }
    f1d()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: Thanks for everyone who helped out - the answer to this one lay
Update: This only seems to be a problem at some computers. The normal, intuitive
Update : I edited the code, but the problem persists... Hi everyone, this is
UPDATE: I've spent way too much time on this and have decided to ditch
(Update: Problem caused by zsh, see accepted answer) Long ago, I followed this great
Update: I was able to solve this problem. I just remembered VBA use *
Update: I posted my own answer below And there's a longer version of this
UPDATE: Hopefully this is a better explanation of the problem: I'm trying to pass
Update: I reported this as a bug to Apple and they fixed it! All
UPDATE: I've been playing around with this more, and it seems like tmux's clear-history

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.