setting a function argument default value as a variable value at the time of creation of the function
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
I would use bquote or substitute and then eval the expression.
– Roland
Nov 22 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 at 10:53
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
r function
asked Nov 22 at 9:30
KH Kim
3581311
3581311
I would use bquote or substitute and then eval the expression.
– Roland
Nov 22 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 at 10:53
add a comment |
I would use bquote or substitute and then eval the expression.
– Roland
Nov 22 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 at 10:53
I would use bquote or substitute and then eval the expression.
– Roland
Nov 22 at 9:53
I would use bquote or substitute and then eval the expression.
– Roland
Nov 22 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 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 at 10:53
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
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
Thank you. It could be a way around.
– KH Kim
Nov 22 at 10:56
add a comment |
up vote
1
down vote
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.
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 at 12:18
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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
Thank you. It could be a way around.
– KH Kim
Nov 22 at 10:56
add a comment |
up vote
1
down vote
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
Thank you. It could be a way around.
– KH Kim
Nov 22 at 10:56
add a comment |
up vote
1
down vote
up vote
1
down vote
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
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
answered Nov 22 at 9:43
Thriving For Perfection
517
517
Thank you. It could be a way around.
– KH Kim
Nov 22 at 10:56
add a comment |
Thank you. It could be a way around.
– KH Kim
Nov 22 at 10:56
Thank you. It could be a way around.
– KH Kim
Nov 22 at 10:56
Thank you. It could be a way around.
– KH Kim
Nov 22 at 10:56
add a comment |
up vote
1
down vote
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.
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 at 12:18
add a comment |
up vote
1
down vote
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.
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 at 12:18
add a comment |
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Nov 22 at 10:55
KH Kim
3581311
3581311
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 at 12:18
add a comment |
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 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 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 at 12:18
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I would use bquote or substitute and then eval the expression.
– Roland
Nov 22 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 at 10:53