Make a function return silently

13,004
myFunc <- function(x){
  invisible(x*2)
}

> myFunc(4)
> y <-myFunc(4)
> y
[1] 8
> 
Share:
13,004

Related videos on Youtube

Math
Author by

Math

Updated on September 15, 2022

Comments

  • Math
    Math over 1 year

    I would like to write an R function that returns silently, like what I get from the barplot function for example.

    I mean that I can store an output in a variable if I do output = myfunction(), but this output does not get printed if I just use myfunction().

    • jdharrison
      jdharrison almost 10 years
      Use invisible
    • Math
      Math almost 10 years
      I just had the wrong keywords, thank you !
    • jdharrison
      jdharrison almost 10 years
      No problem happy to help
    • Joshua Ulrich
      Joshua Ulrich almost 10 years
      You would have found invisible had you looked at the See Also section of ?return. The See Also section is a great resource, especially when you're not sure of the correct keyword.
    • rawr
      rawr almost 10 years
      or you could have looked at the barplot source code, barplot.default
    • Math
      Math almost 10 years
      @JoshuaUlrich I know that, but I checked and did not see it. And I know why, it's hidden behind debug
    • jbaums
      jbaums almost 10 years
  • jdharrison
    jdharrison almost 10 years
    Mostly likely a duplicate
  • Math
    Math almost 10 years
    I would say it is usefull to keep this question for the keywork silent (instead of invisible). If you do not agree, I can delete it.
  • jdharrison
    jdharrison almost 10 years
    It shouldn't be deleted the question is can a duplicate be found that has already addressed the question.
  • David F
    David F almost 6 years
    I found it by searching 'silent', not 'invisible'