R: Function changing print behavior when returning NULL
up vote
2
down vote
favorite
This question is only for curiosity. My colleague and I were trying to write a function which returns NULL
, but doesn't print it.
Before we found return(invisible(NULL))
, I tried return({dummy<-NULL})
which works, but only once. After the first evaluation, the functions starts printing again:
test <- function() {
return({x<-NULL})
}
# no printout
test()
# with printout
test()
# with printout
test()
How does this come about?
r printing null return-value
add a comment |
up vote
2
down vote
favorite
This question is only for curiosity. My colleague and I were trying to write a function which returns NULL
, but doesn't print it.
Before we found return(invisible(NULL))
, I tried return({dummy<-NULL})
which works, but only once. After the first evaluation, the functions starts printing again:
test <- function() {
return({x<-NULL})
}
# no printout
test()
# with printout
test()
# with printout
test()
How does this come about?
r printing null return-value
did u call the function for any number of times in code?
– sai saran
Nov 8 at 10:17
I dont understand your question sai. OP says he calls the function n-times. The first time nothing is printed any time afterNULL
is printed.
– Andre Elrico
Nov 8 at 10:28
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This question is only for curiosity. My colleague and I were trying to write a function which returns NULL
, but doesn't print it.
Before we found return(invisible(NULL))
, I tried return({dummy<-NULL})
which works, but only once. After the first evaluation, the functions starts printing again:
test <- function() {
return({x<-NULL})
}
# no printout
test()
# with printout
test()
# with printout
test()
How does this come about?
r printing null return-value
This question is only for curiosity. My colleague and I were trying to write a function which returns NULL
, but doesn't print it.
Before we found return(invisible(NULL))
, I tried return({dummy<-NULL})
which works, but only once. After the first evaluation, the functions starts printing again:
test <- function() {
return({x<-NULL})
}
# no printout
test()
# with printout
test()
# with printout
test()
How does this come about?
r printing null return-value
r printing null return-value
edited Nov 8 at 10:30
Andre Elrico
4,6671827
4,6671827
asked Nov 8 at 10:12
eladin
494
494
did u call the function for any number of times in code?
– sai saran
Nov 8 at 10:17
I dont understand your question sai. OP says he calls the function n-times. The first time nothing is printed any time afterNULL
is printed.
– Andre Elrico
Nov 8 at 10:28
add a comment |
did u call the function for any number of times in code?
– sai saran
Nov 8 at 10:17
I dont understand your question sai. OP says he calls the function n-times. The first time nothing is printed any time afterNULL
is printed.
– Andre Elrico
Nov 8 at 10:28
did u call the function for any number of times in code?
– sai saran
Nov 8 at 10:17
did u call the function for any number of times in code?
– sai saran
Nov 8 at 10:17
I dont understand your question sai. OP says he calls the function n-times. The first time nothing is printed any time after
NULL
is printed.– Andre Elrico
Nov 8 at 10:28
I dont understand your question sai. OP says he calls the function n-times. The first time nothing is printed any time after
NULL
is printed.– Andre Elrico
Nov 8 at 10:28
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I think this is due to some older return handling built into R. There are many return functions, withVisible
, invisible
, etc. When you return an assignment x<-null
inside the return function it will not automatically print. If you want an assignment to print...
test <- function() {
withAutoprint(x<-NULL)
}
# with printout this time
test()
# with printout
test()
# with printout
test()
I think this just may be hard coded into the return function, maybe pulling something from this logic below, just a shot in the dark though.
Source: R Documentation
x <- 1
withVisible(x <- 1) # *$visible is FALSE
x
withVisible(x) # *$visible is TRUE
Again if we do not use an expression and simply return a variable or value inside our return function we get automatic printing. The reason I am guessing it returns on a second call has to do with the fact x was already assigned previously.
EDIT: I found this deep into the documentation on auto printing. "Whether the returned value of a top-level R expression is printed is controlled by the global boolean variable R_Visible. This is set (to true or false) on entry to all primitive and internal functions based on the eval column of the table in file src/main/names.c: the appropriate setting can be extracted by the macro PRIMPRINT."(Source)
Learning something new everyday :)
– eladin
Nov 8 at 16:37
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I think this is due to some older return handling built into R. There are many return functions, withVisible
, invisible
, etc. When you return an assignment x<-null
inside the return function it will not automatically print. If you want an assignment to print...
test <- function() {
withAutoprint(x<-NULL)
}
# with printout this time
test()
# with printout
test()
# with printout
test()
I think this just may be hard coded into the return function, maybe pulling something from this logic below, just a shot in the dark though.
Source: R Documentation
x <- 1
withVisible(x <- 1) # *$visible is FALSE
x
withVisible(x) # *$visible is TRUE
Again if we do not use an expression and simply return a variable or value inside our return function we get automatic printing. The reason I am guessing it returns on a second call has to do with the fact x was already assigned previously.
EDIT: I found this deep into the documentation on auto printing. "Whether the returned value of a top-level R expression is printed is controlled by the global boolean variable R_Visible. This is set (to true or false) on entry to all primitive and internal functions based on the eval column of the table in file src/main/names.c: the appropriate setting can be extracted by the macro PRIMPRINT."(Source)
Learning something new everyday :)
– eladin
Nov 8 at 16:37
add a comment |
up vote
1
down vote
accepted
I think this is due to some older return handling built into R. There are many return functions, withVisible
, invisible
, etc. When you return an assignment x<-null
inside the return function it will not automatically print. If you want an assignment to print...
test <- function() {
withAutoprint(x<-NULL)
}
# with printout this time
test()
# with printout
test()
# with printout
test()
I think this just may be hard coded into the return function, maybe pulling something from this logic below, just a shot in the dark though.
Source: R Documentation
x <- 1
withVisible(x <- 1) # *$visible is FALSE
x
withVisible(x) # *$visible is TRUE
Again if we do not use an expression and simply return a variable or value inside our return function we get automatic printing. The reason I am guessing it returns on a second call has to do with the fact x was already assigned previously.
EDIT: I found this deep into the documentation on auto printing. "Whether the returned value of a top-level R expression is printed is controlled by the global boolean variable R_Visible. This is set (to true or false) on entry to all primitive and internal functions based on the eval column of the table in file src/main/names.c: the appropriate setting can be extracted by the macro PRIMPRINT."(Source)
Learning something new everyday :)
– eladin
Nov 8 at 16:37
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I think this is due to some older return handling built into R. There are many return functions, withVisible
, invisible
, etc. When you return an assignment x<-null
inside the return function it will not automatically print. If you want an assignment to print...
test <- function() {
withAutoprint(x<-NULL)
}
# with printout this time
test()
# with printout
test()
# with printout
test()
I think this just may be hard coded into the return function, maybe pulling something from this logic below, just a shot in the dark though.
Source: R Documentation
x <- 1
withVisible(x <- 1) # *$visible is FALSE
x
withVisible(x) # *$visible is TRUE
Again if we do not use an expression and simply return a variable or value inside our return function we get automatic printing. The reason I am guessing it returns on a second call has to do with the fact x was already assigned previously.
EDIT: I found this deep into the documentation on auto printing. "Whether the returned value of a top-level R expression is printed is controlled by the global boolean variable R_Visible. This is set (to true or false) on entry to all primitive and internal functions based on the eval column of the table in file src/main/names.c: the appropriate setting can be extracted by the macro PRIMPRINT."(Source)
I think this is due to some older return handling built into R. There are many return functions, withVisible
, invisible
, etc. When you return an assignment x<-null
inside the return function it will not automatically print. If you want an assignment to print...
test <- function() {
withAutoprint(x<-NULL)
}
# with printout this time
test()
# with printout
test()
# with printout
test()
I think this just may be hard coded into the return function, maybe pulling something from this logic below, just a shot in the dark though.
Source: R Documentation
x <- 1
withVisible(x <- 1) # *$visible is FALSE
x
withVisible(x) # *$visible is TRUE
Again if we do not use an expression and simply return a variable or value inside our return function we get automatic printing. The reason I am guessing it returns on a second call has to do with the fact x was already assigned previously.
EDIT: I found this deep into the documentation on auto printing. "Whether the returned value of a top-level R expression is printed is controlled by the global boolean variable R_Visible. This is set (to true or false) on entry to all primitive and internal functions based on the eval column of the table in file src/main/names.c: the appropriate setting can be extracted by the macro PRIMPRINT."(Source)
edited Nov 8 at 15:56
answered Nov 8 at 15:47
Chabo
614318
614318
Learning something new everyday :)
– eladin
Nov 8 at 16:37
add a comment |
Learning something new everyday :)
– eladin
Nov 8 at 16:37
Learning something new everyday :)
– eladin
Nov 8 at 16:37
Learning something new everyday :)
– eladin
Nov 8 at 16:37
add a comment |
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%2f53205544%2fr-function-changing-print-behavior-when-returning-null%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
did u call the function for any number of times in code?
– sai saran
Nov 8 at 10:17
I dont understand your question sai. OP says he calls the function n-times. The first time nothing is printed any time after
NULL
is printed.– Andre Elrico
Nov 8 at 10:28