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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:33:00+00:00 2026-05-26T09:33:00+00:00

Yesterday I learned from Bill Venables how local() can help create static functions and

  • 0

Yesterday I learned from Bill Venables how local() can help create static functions and variables, e.g.,

example <- local({
  hidden.x <- "You can't see me!"
  hidden.fn <- function(){
    cat("\"hidden.fn()\"")
  }
  function(){
    cat("You can see and call example()\n")
    cat("but you can't see hidden.x\n")
    cat("and you can't call ")
    hidden.fn()
    cat("\n")
  }
})

which behaves as follows from the command prompt:

> ls()
[1] "example"
> example()
You can see and call example()
but you can't see hidden.x
and you can't call "hidden.fn()"
> hidden.x                 
Error: object 'hidden.x' not found
> hidden.fn()
Error: could not find function "hidden.fn"

I’ve seen this kind of thing discussed in Static Variables in R where a different approach was employed.

What the pros and cons of these two methods?

  • 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-26T09:33:01+00:00Added an answer on May 26, 2026 at 9:33 am

    Encapsulation

    The advantage of this style of programming is that the hidden objects won’t likely be overwritten by anything else so you can be more confident that they contain what you think. They won’t be used by mistake since they can’t readily be accessed. In the linked-to post in the question there is a global variable, count, which could be accessed and overwritten from anywhere so if we are debugging code and looking at count and see its changed we cannnot really be sure what part of the code has changed it. In contrast, in the example code of the question we have greater assurance that no other part of the code is involved.

    Note that we actually can access the hidden function although its not that easy:

    # run hidden.fn
    environment(example)$hidden.fn()
    

    Object Oriented Programming

    Also note that this is very close to object oriented programming where example and hidden.fn are methods and hidden.x is a property. We could do it like this to make it explicit:

    library(proto)
    p <- proto(x = "x", 
      fn = function(.) cat(' "fn()"\n '),
      example = function(.) .$fn()
    )
    p$example() # prints "fn()"
    

    proto does not hide x and fn but its not that easy to access them by mistake since you must use p$x and p$fn() to access them which is not really that different than being able to write e <- environment(example); e$hidden.fn()

    EDIT:

    The object oriented approach does add the possibility of inheritance, e.g. one could define a child of p which acts like p except that it overrides fn.

    ch <- p$proto(fn = function(.) cat("Hello from ch\n")) # child
    ch$example() # prints: Hello from ch
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just learned yesterday from this site that I can: class Seq(object): def __init__(self,
During an ordeal yesterday, I learned that you can't pass this query to EXEC():
From a response to some question yesterday, I learned that it is nonportable and
I learned something about DLL's yesterday. But I haven't found the way how to
Yesterday I got a call from a Microsoft representative asking if we supply hosted
Yesterday I learned that inline assembly (with the __asm keyword) is not supported under
This is really an extension of my question yesterday where I learned about apply.weekly
Yesterday I switched hosts from 000webhost to ipage however when I went to login
yesterday I deployed my website from c:\development\project\ to c:\inetpub\wwwroot\virtualdir\ (same pc). It worked for
Yesterday I worked up an example of the difference between Ordinary Least Squares (OLS)

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.