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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:55:37+00:00 2026-05-25T20:55:37+00:00

I have defined a function called once as follows: once <- function(x, value) {

  • 0

I have defined a function called once as follows:

once <- function(x, value) {
    xname <- deparse(substitute(x))
    if(!exists(xname)) {
        assign(xname, value, env=parent.frame())
    }
    invisible()
}

The idea is that value is time-consuming to evaluate, and I only want to assign it to x the first time I run a script.

> z
Error: object 'z' not found
> once(z, 3)
> z
[1] 3

I’d really like the usage to be once(x) <- value rather than once(x, value), but if I write a function once<- it gets upset that the variable doesn’t exist:

> once(z) <- 3
Error in once(z) <- 3 : object 'z' not found

Does anyone have a way around this?

ps: is there a name to describe functions like once<- or in general f<-?

  • 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-25T20:55:38+00:00Added an answer on May 25, 2026 at 8:55 pm

    As per item 3.4.4 in the R Language Reference, something like a names replacement is evaluated like this:

     `*tmp*` <- x
     x <- "names<-"(`*tmp*`, value=c("a","b"))
     rm(`*tmp*`)
    

    This is bad news for your requirement, because the assignment will fail on the first line (as x is not found), and even if it would work, your deparse(substitute) call will never evaluate to what you want it to.

    Sorry to disappoint you

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

Sidebar

Related Questions

I have a user defined function in SQL called getBuisnessDays it takes @startdate and
I have a function, where if a variable is not defined, exit; is called,
I have defined a function called is_logged_in? in a user defined library stored in
I have a function defined in AS3 that's gonna be called from client side
I have defined a function in vim to properly indent folds. Ie so they
I have defined a function in which i want to open a page on
I have defined a function in my Navigation model that executes a query, and
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have a User Defined Function (UDF) written in Java to parse lines in
Most systems will have a user-defined function (UDF) available. Some will not. i want

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.