setting a function argument default value as a variable value at the time of creation of the function












1















Can I create a function with the default value of an argument set to the value of a variable at the time of creation?



Something like,
a=1
fn = function(arg1 = a) {
print (arg1+1)
}


fn would show



function(arg1 = 1) {
print (arg1+1)
}









share|improve this question























  • I would use bquote or substitute and then eval the expression.

    – Roland
    Nov 22 '18 at 9:53













  • Thank you for the clue. Could you elaborate on how it works... I figured out how to do it but I am not sure I fully understand how it works...

    – KH Kim
    Nov 22 '18 at 10:53
















1















Can I create a function with the default value of an argument set to the value of a variable at the time of creation?



Something like,
a=1
fn = function(arg1 = a) {
print (arg1+1)
}


fn would show



function(arg1 = 1) {
print (arg1+1)
}









share|improve this question























  • I would use bquote or substitute and then eval the expression.

    – Roland
    Nov 22 '18 at 9:53













  • Thank you for the clue. Could you elaborate on how it works... I figured out how to do it but I am not sure I fully understand how it works...

    – KH Kim
    Nov 22 '18 at 10:53














1












1








1








Can I create a function with the default value of an argument set to the value of a variable at the time of creation?



Something like,
a=1
fn = function(arg1 = a) {
print (arg1+1)
}


fn would show



function(arg1 = 1) {
print (arg1+1)
}









share|improve this question














Can I create a function with the default value of an argument set to the value of a variable at the time of creation?



Something like,
a=1
fn = function(arg1 = a) {
print (arg1+1)
}


fn would show



function(arg1 = 1) {
print (arg1+1)
}






r function






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 9:30









KH KimKH Kim

3681311




3681311













  • I would use bquote or substitute and then eval the expression.

    – Roland
    Nov 22 '18 at 9:53













  • Thank you for the clue. Could you elaborate on how it works... I figured out how to do it but I am not sure I fully understand how it works...

    – KH Kim
    Nov 22 '18 at 10:53



















  • I would use bquote or substitute and then eval the expression.

    – Roland
    Nov 22 '18 at 9:53













  • Thank you for the clue. Could you elaborate on how it works... I figured out how to do it but I am not sure I fully understand how it works...

    – KH Kim
    Nov 22 '18 at 10:53

















I would use bquote or substitute and then eval the expression.

– Roland
Nov 22 '18 at 9:53







I would use bquote or substitute and then eval the expression.

– Roland
Nov 22 '18 at 9:53















Thank you for the clue. Could you elaborate on how it works... I figured out how to do it but I am not sure I fully understand how it works...

– KH Kim
Nov 22 '18 at 10:53





Thank you for the clue. Could you elaborate on how it works... I figured out how to do it but I am not sure I fully understand how it works...

– KH Kim
Nov 22 '18 at 10:53












2 Answers
2






active

oldest

votes


















1














One way to do this is to use the global options in R:



fn <- function(arg1 = getOption("arg1", 1)) {
print(arg1 + 1)
}

fn() # returns 2
options(arg1 = 5)
fn() # returns 6
fn(2) # returns 3
options(arg1 = NULL)
fn() # returns 2 again


I think the above solution is cleaner compared to using a global variable in .GlobalEnv, but here is also how you can do it with a global variable in .GlobalEnv:



fn2 <- function(arg1 = if( is.null(.GlobalEnv[["a"]]) ) 1 else .GlobalEnv[["a"]]) {
print(arg1+1)
}

fn2() # this returns an empty vector
a <- 5
fn2() # this returns 6





share|improve this answer
























  • Thank you. It could be a way around.

    – KH Kim
    Nov 22 '18 at 10:56



















1














Helped by help for bquote function. Here is what I found working for me.



It does not look straight forward to me but it works.



a=1
fn <- eval(bquote( function(arg1 = .(a)) {
print (arg1+1)
} ))
fn
fn(3)


eval(bquote()) and .(a) are the point.



I found the hows but I don't think I fully understood it.
So anyone can help me understand how it works, I will be glad to take it as an answer.






share|improve this answer
























  • It's basic computing on the language. You construct an expression and then evaluate it. This ensures that the default function parameter is a value and not a symbol.

    – Roland
    Nov 22 '18 at 12:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427702%2fsetting-a-function-argument-default-value-as-a-variable-value-at-the-time-of-cre%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














One way to do this is to use the global options in R:



fn <- function(arg1 = getOption("arg1", 1)) {
print(arg1 + 1)
}

fn() # returns 2
options(arg1 = 5)
fn() # returns 6
fn(2) # returns 3
options(arg1 = NULL)
fn() # returns 2 again


I think the above solution is cleaner compared to using a global variable in .GlobalEnv, but here is also how you can do it with a global variable in .GlobalEnv:



fn2 <- function(arg1 = if( is.null(.GlobalEnv[["a"]]) ) 1 else .GlobalEnv[["a"]]) {
print(arg1+1)
}

fn2() # this returns an empty vector
a <- 5
fn2() # this returns 6





share|improve this answer
























  • Thank you. It could be a way around.

    – KH Kim
    Nov 22 '18 at 10:56
















1














One way to do this is to use the global options in R:



fn <- function(arg1 = getOption("arg1", 1)) {
print(arg1 + 1)
}

fn() # returns 2
options(arg1 = 5)
fn() # returns 6
fn(2) # returns 3
options(arg1 = NULL)
fn() # returns 2 again


I think the above solution is cleaner compared to using a global variable in .GlobalEnv, but here is also how you can do it with a global variable in .GlobalEnv:



fn2 <- function(arg1 = if( is.null(.GlobalEnv[["a"]]) ) 1 else .GlobalEnv[["a"]]) {
print(arg1+1)
}

fn2() # this returns an empty vector
a <- 5
fn2() # this returns 6





share|improve this answer
























  • Thank you. It could be a way around.

    – KH Kim
    Nov 22 '18 at 10:56














1












1








1







One way to do this is to use the global options in R:



fn <- function(arg1 = getOption("arg1", 1)) {
print(arg1 + 1)
}

fn() # returns 2
options(arg1 = 5)
fn() # returns 6
fn(2) # returns 3
options(arg1 = NULL)
fn() # returns 2 again


I think the above solution is cleaner compared to using a global variable in .GlobalEnv, but here is also how you can do it with a global variable in .GlobalEnv:



fn2 <- function(arg1 = if( is.null(.GlobalEnv[["a"]]) ) 1 else .GlobalEnv[["a"]]) {
print(arg1+1)
}

fn2() # this returns an empty vector
a <- 5
fn2() # this returns 6





share|improve this answer













One way to do this is to use the global options in R:



fn <- function(arg1 = getOption("arg1", 1)) {
print(arg1 + 1)
}

fn() # returns 2
options(arg1 = 5)
fn() # returns 6
fn(2) # returns 3
options(arg1 = NULL)
fn() # returns 2 again


I think the above solution is cleaner compared to using a global variable in .GlobalEnv, but here is also how you can do it with a global variable in .GlobalEnv:



fn2 <- function(arg1 = if( is.null(.GlobalEnv[["a"]]) ) 1 else .GlobalEnv[["a"]]) {
print(arg1+1)
}

fn2() # this returns an empty vector
a <- 5
fn2() # this returns 6






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 9:43









Thriving For PerfectionThriving For Perfection

517




517













  • Thank you. It could be a way around.

    – KH Kim
    Nov 22 '18 at 10:56



















  • Thank you. It could be a way around.

    – KH Kim
    Nov 22 '18 at 10:56

















Thank you. It could be a way around.

– KH Kim
Nov 22 '18 at 10:56





Thank you. It could be a way around.

– KH Kim
Nov 22 '18 at 10:56













1














Helped by help for bquote function. Here is what I found working for me.



It does not look straight forward to me but it works.



a=1
fn <- eval(bquote( function(arg1 = .(a)) {
print (arg1+1)
} ))
fn
fn(3)


eval(bquote()) and .(a) are the point.



I found the hows but I don't think I fully understood it.
So anyone can help me understand how it works, I will be glad to take it as an answer.






share|improve this answer
























  • It's basic computing on the language. You construct an expression and then evaluate it. This ensures that the default function parameter is a value and not a symbol.

    – Roland
    Nov 22 '18 at 12:18
















1














Helped by help for bquote function. Here is what I found working for me.



It does not look straight forward to me but it works.



a=1
fn <- eval(bquote( function(arg1 = .(a)) {
print (arg1+1)
} ))
fn
fn(3)


eval(bquote()) and .(a) are the point.



I found the hows but I don't think I fully understood it.
So anyone can help me understand how it works, I will be glad to take it as an answer.






share|improve this answer
























  • It's basic computing on the language. You construct an expression and then evaluate it. This ensures that the default function parameter is a value and not a symbol.

    – Roland
    Nov 22 '18 at 12:18














1












1








1







Helped by help for bquote function. Here is what I found working for me.



It does not look straight forward to me but it works.



a=1
fn <- eval(bquote( function(arg1 = .(a)) {
print (arg1+1)
} ))
fn
fn(3)


eval(bquote()) and .(a) are the point.



I found the hows but I don't think I fully understood it.
So anyone can help me understand how it works, I will be glad to take it as an answer.






share|improve this answer













Helped by help for bquote function. Here is what I found working for me.



It does not look straight forward to me but it works.



a=1
fn <- eval(bquote( function(arg1 = .(a)) {
print (arg1+1)
} ))
fn
fn(3)


eval(bquote()) and .(a) are the point.



I found the hows but I don't think I fully understood it.
So anyone can help me understand how it works, I will be glad to take it as an answer.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 10:55









KH KimKH Kim

3681311




3681311













  • It's basic computing on the language. You construct an expression and then evaluate it. This ensures that the default function parameter is a value and not a symbol.

    – Roland
    Nov 22 '18 at 12:18



















  • It's basic computing on the language. You construct an expression and then evaluate it. This ensures that the default function parameter is a value and not a symbol.

    – Roland
    Nov 22 '18 at 12:18

















It's basic computing on the language. You construct an expression and then evaluate it. This ensures that the default function parameter is a value and not a symbol.

– Roland
Nov 22 '18 at 12:18





It's basic computing on the language. You construct an expression and then evaluate it. This ensures that the default function parameter is a value and not a symbol.

– Roland
Nov 22 '18 at 12:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427702%2fsetting-a-function-argument-default-value-as-a-variable-value-at-the-time-of-cre%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]