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 7037935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:36:19+00:00 2026-05-28T01:36:19+00:00

[This question has been resolved in the chat room, by Spacedman , but I’m

  • 0

[This question has been resolved in the chat room, by Spacedman, but I’m posting it for others’ benefit in the future.]

I have a function, myFunc, which creates localFunc inside of it. (NB: this is not in a package, but in the global environment.) I’d like to know where localFunc exists in the search path, as I’d like to analyze it via mvbutils::foodweb.

Here is an example:

myFunc <- function(){
    require(data.table)
    require(mvbutils)
    localFunc <- function(x){
        return(as.data.table(x))
    }
    
    vecPrune <- c("localFunc",ls("package:data.table"))
    ix <- match("data.table",search())
    tmpWeb <- foodweb(where = c(1,ix), prune = vecPrune, plotting = FALSE)
    return(tmpWeb)
}

However, a call to myFunc() does not seem to indicate that localFunc calls data.table(). This is incorrect – what gives?

(NB: The where argument specifies the search path.)


Update 1: As Tommy and Spacedman point out, the trick is to specify environment(). The call to foodweb() refers to where = c(1, ix). The index 1 is a mistake. That arose from thinking that .GlobalEnv, which is often (always?) the first item in the search() vector, is the right place to search. That is erroneous. Instead, one should refer to environment(), and the correct call is below. (NB: ix specifies the location of data.table() in the search() output.)

tmpWeb <- foodweb(where = c(environment(),ix), prune = vecPrune, plotting = FALSE)

This appears in the answer to this question, in a function called checkScriptDependencies, which wraps the code from an R script file into a local function, which is then analyzed by foodweb. This is a limited example of how to use environment(), and Tommy has given a good explanation of how to use it and similar functions in this context.

  • 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-05-28T01:36:19+00:00Added an answer on May 28, 2026 at 1:36 am

    To get the current environment, just call environment().

    In general, sys.frame returns any of the environments currently on the call stack, and sys.nframe returns the current depth of the call stack. sys.frames returns a list of all environments on the call stack.

    environment(f) returns the closure environment for a function f (where it will look for functions and global variables).

    parent.env(e) returns the parent environment where it will look if a symbol is not found in e.

    f <- function() {
      function() list(curEnv=environment(), parent=parent.env(environment()), 
              grandParent=parent.env(parent.env(environment())), callStack=sys.frames(), 
              callStackDepth=sys.nframe())
    }
    g <- function(f, n=2) if (n>2) g(f, n-1) else f()
    
    floc <- f() # generate a local function
    g(floc, 3) # call it
    

    This will call the local function floc with a stack depth of 3. It returns a list with the current environment, it’s parent (the local environment in f), and it’s grand parent (where f was defined, so globalenv). It also returns the list of stack frames (environments). These are the environments for the recursive calls in g (except the last one which is the current environment of floc).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked before ( link ) but I have slightly different
Since I posted this question, this issue has resolved itself. There must have been
This question has been asked in a C++ context but I'm curious about Java.
This question has been asked before but the answers aren't always clear or are
This question has been asked before somewhat, but I hope mine differs. My situation
This question has been asked before perhaps multiple times, but I can't get the
This is a question that has been asked before, but unfortunately no solution seems
I realize this question has been asked many times and I am have looked
I know this question has already been asked but I am in urgent need
I know that this question has been resolved in many other threads for example:

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.