I have multiple user defined functions written in R. I usually source the code and then print the output in R console. My problem is I have 3 function written in one file and all three functions have similar output( here I have z which is common in all three function).. Is there any solution in R where I do not have to type print(z) at the end of every function but after sourcing my code I should be able to print z specific to function?
harry<-function(i){
for(i in 3:5) {
z <- i + 1
print(z)
}
}
harry1<-function(i){
for(i in 1:5) {
z <- i + 1
print(z)
}
}
harry2<-function(i){
for(i in 1:5) {
z <- i + 5
print(z)
}
}
returninstead.sourceregularly used functions once, keep them in your environment, and write either commands or a script to execute the functions. This is in addition to understanding the difference betweenprintand Brandon's suggestion to usereturn.