Sourcing in R

Sourcing in R

by Prisca Hsu -
Number of replies: 5

Hi Dr. Spence,

I've finished writing my script in atom and every part works if I paste each line individually into R. However, I am having trouble sourcing my script into R (I keep getting an error in source message). Could you elaborate on the steps to call my script into R? 

Thanks,

Prisca

In reply to Prisca Hsu

Re: Sourcing in R

by Erik Spence -
What command do we use to run an R script from the bash prompt?
In reply to Erik Spence

Re: Sourcing in R

by Leanna Lui -
Hi Dr. Spence,

I'm trying to source my R script in terminal but I keep getting this error: zsh: command not found: myFuncs.R

I entered the directory which holds myFuncs.R and did Rscript myFuncs.R and this is the message i'm getting.

When I try opening it in R using source "myFuncs.R", I get: Error in eval(ei, envir) : object 'x' not found

How can I troubleshoot this?
In reply to Leanna Lui

Re: Sourcing in R

by Erik Spence -
The error message that you're getting does not suggest that you're typing "Rscript myFuncs.R". It looks like you're just typing "myFuncs.R".

You use the command "source mybashscript.sh" at the bash prompt. You use "source('myFuncs.R')" at the R prompt. You can't mix these two commands. If you actually did "source('myFuncs.R')" at the R prompt then there's something wrong with your R code.
In reply to Erik Spence

Re: Sourcing in R

by Leanna Lui -
Hi Dr. Spence

I inputted source('myFuncs.R') into the R prompt and received this response:
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'myFuncs.R': No such file or directory

So in that case, I entered the file where myFuncs.R is stored, and when I inputted source('myFuncs.R') into the R prompt, I received this response:
Error in eval(ei, envir) : object 'x' not found

What are some potential reasons why this would happen?

When I was testing out my code in R , everything was fine (i.e., output matched output from test code)
In reply to Leanna Lui

Re: Sourcing in R

by Erik Spence -

It looks like you've got an extra 'x' floating around in your file.  If you can't find it, email us.