Home > database >  How to print one result from function but store others
How to print one result from function but store others

Time:10-14

Consider this made up function:

Made_up <- function(x) {
one <- x 
two <- x   1
three <- x   2
}

How can I have the function only print the result of the object three and store the other variables to be called when I write

  answer <- Made_up(1)

  answer$....

CodePudding user response:

Similar to Vasily A's answer, you could use return and store the results from the function in answer.

Made_up <- function(x) {
  one <- x 
  two <- x   1
  three <- x   2
  print(three)
  return(list(one=one, two=two, three=three))
}

answer <- Made_up(1)
answer$one

[1] 1

CodePudding user response:

how about this:

Made_up <- function(x) {
one <- x 
two <- x   1
three <- x   2
print(three)
invisible(list(one=one, two=two, three=three))
}
  • Related