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

  • Home
  • SEARCH
  • 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 8594795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:16:51+00:00 2026-06-12T00:16:51+00:00

How can I query the value of x for foo in the R code

  • 0

How can I query the value of x for foo in the R code below?

make.foo <- function() {
    x <- 123
    function() x * 3
}

foo <- make.foo()

# now get foo's x
  • 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-12T00:16:52+00:00Added an answer on June 12, 2026 at 12:16 am

    A function will have an environment

    from ?`function`

    A closure has three components, its formals (its argument list), its body (expr in the ‘Usage’ section) and its environment which provides the enclosure of the evaluation frame when the closure is used.

    so you can get from that environment (or list the objects using ls)

    get('x', envir = environment(foo))
    ## [1] 123
    

    or if you want to know all the objects in the environment

    ls(envir = environment(foo))
    ## 'x'
    

    and if you want to assign to that environment (ie change x)

    assign('x', 24, envir = environment(foo))
    
    foo()
    ## 72
    

    You can even remove it from the environment

    rm(x, envir = environment(foo))
    foo()
    ## Error in foo() : object 'x' not found
    

    and then use a globally assigned x

    x <- 3
    foo()
    # [1] 9
    

    and reassign to the function‘s environment

    assign('x', 123, envir = environment(foo))
    foo()
    ## [1] 369
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following URL: domain.com/page.aspx?id=123 How can I sanitize that query string value when
How can I pass the result from a scalar [single row, single value] query
Can a select query use different indexes if a change the value of a
How can I write single UPDATE query to change value of COL1 to ‘X’
I was reading How can I get query string values in JavaScript? on Stackoverflow
Is there any function in C on linux by which we can query MX
Say there is a special PHP function foo($number) that returns double the value of
Can anyone tell me how to display the other values, when a query is
I can query for the picture,location and privacy fields/connections of a users Facebook event
Is there a table i can query all of Oracle Reserved Words? Example, i

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.