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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:18:01+00:00 2026-06-18T14:18:01+00:00

Problem: How to define local variables within a scope in a r-code. Example: In

  • 0

Problem:

How to define local variables within a scope in a r-code.

Example:

In C++ the following example defines a scope, and variables declared within the scope are undefined in outside code.

{
     vector V1 = getVector(1);
     vector V1(= getVector(2);
     double P = inner_product(V1, V2);
     print(P);
}
// the variable V1, V2, P are undefined here!

Note: This code is only to illustrate the idea.

This practice has the advantages of:

  • Keeping the global namespace clean;
  • simplifying code;
  • removing ambiguities, specially when a variable is re-used without initialization.

In R, it seems to me that this notion is only existent inside function definitions. So, in order to reproduce the previous example code, I would need to do something like this:

dummy <- function( ) {
     V1 = c(1,2,3);
     V2 = c(1,2,3);
     P = inner_product(V1, V2);
     print(P);
}
dummy( );
# the variable V1, V2, P are undefined here!

or, in an even more obscure way, declare a anonymous function to prevent the function call:

(function() { 
     V1 = c(1,2,3);
     V2 = c(1,2,3);
     P = inner_product(V1, V2);
     print(P);
})()
# the variable V1, V2, P are undefined here!

Question

Is there a more elegant way to create local variable?

  • 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-18T14:18:02+00:00Added an answer on June 18, 2026 at 2:18 pm

    Use local. Using your example:

    local({ 
         V1 = c(1,2,3);
         V2 = c(1,2,3);
         P = inner_product(V1, V2);
         print(P);
    })
    # the variable V1, V2, P are undefined here!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the following code: IntegerSet& IntegerSet::unionOfIntegerSets(IntegerSet a){ IntegerSet result; for
Hey its really hard to define an exact title for my problem. I am
I am trying to define MapPageRoute on the application global.asax but my problem is
I have defined the following ApplicationManifest file the problem is that i have created
In Applescript, if you declare a handler using with labeled parameters, local variables get
Depending on what I know, when I define a method in C#, the local
I have a few local variables in my partials which may or may not
does anyone know how I can solve the following problem. I want to return
I have local variables x1 , x2, and x3 as follows local x1 2
i have a problem when using my code with threading, the problem is apparently

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.