I’ve got a simple function script, the last line of which is to call the function
fun<-function(){
readline("This is a test")
x<-c(4,5)
y<-c(5,6)
add<-x+y
sub<-x-y
return(list(add,sub))
}
fun()
If I copy this into my R console I get what is to be expected
This is a test
[[1]]
[1] 9 11
[[2]]
[1] -1 -1
But I have been trying to use “source” to automatically pull up my scripts. If I try and use source
source("/Users/Me/Desktop/R.test.R")
I loose my output. I still get the readline, but I loose the return data so I just get this:
This is a test
I can still use source command to access my function if I delete the last line, and then manually type in “fun()”
source("/Users/Me/Desktop/R.test.R")
fun()
but I’d like to make this a one step deal.
Can someone explain why the difference between a “cut and paste” vs using source, and how I use source to call my functions in one step?
Thanks
When you type things directly in the console, R by default calls
printon that object in order to display it. But it only does that directly in the console.Try setting
print.eval = TRUEwhen you callsource. To see what’s going on, try it both ways with this script:Now it’s a little more explicit. Just typing
xat the console silently causes R toprintit. If you want that behavior when sourcing, you have to say so.