R: Function changing print behavior when returning NULL











up vote
2
down vote

favorite
1












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?










share|improve this question
























  • 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















up vote
2
down vote

favorite
1












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?










share|improve this question
























  • 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













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 after NULL 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










  • 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
















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












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)






share|improve this answer























  • Learning something new everyday :)
    – eladin
    Nov 8 at 16:37











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',
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%2f53205544%2fr-function-changing-print-behavior-when-returning-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























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)






share|improve this answer























  • Learning something new everyday :)
    – eladin
    Nov 8 at 16:37















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)






share|improve this answer























  • Learning something new everyday :)
    – eladin
    Nov 8 at 16:37













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)






share|improve this answer














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)







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff