6

I have an RScript file (let's call it main.r) which has a reference to another file, using the below code:

source("functions.R")

But, when I run the RScript file, it complains with the below error:

    Error in file(filename, "r", encoding = encoding) : 
      cannot open the connection
    In addition: Warning message:
    In file(filename, "r", encoding = encoding) :
      cannot open file 'functions.R': No such file or directory

I am sure, my main.R file is next to functions.R in the same directory. I can call the functions.R in the Rmd (RMarkdown) file which exist in the same directory

Peter
  • 8,493
  • 4
  • 17
  • 28
Sal-laS
  • 10,023
  • 25
  • 90
  • 156
  • Did you set the working directory in `main`? – niko Jan 29 '19 at 12:43
  • I don't know, what is it. Can you please give more details? I have tried this one as well `source("./functions.R")` – Sal-laS Jan 29 '19 at 12:44
  • 1
    i had the same issue, i resolved it by modifying the file i was doing source() on. That file also was calling a source() on a file that wasn't there. ( i had moved directories and forgot to copy over that other file) – Josh Pachner Aug 23 '21 at 15:19

2 Answers2

9

In your case try to add setwd("path/to/project/") in main.R where path/to/project/ contains main.R.

Then you can source functions.R either directly by source("functions.R") if both files lie in the same directory or source("sub-folder/functions.R") if the latter file is contained in a sub-folder.

If you're not working on a RStudio project, chances are the working directory of main.R might be your home directory.

niko
  • 5,100
  • 1
  • 10
  • 27
  • is it an absolute path? or it can be relational? i have tried `source("./functions.R")` – Sal-laS Jan 29 '19 at 12:47
  • @SalmanLashkarara I believe it depends on your home directory (or your script's current directory). If you have for instance a `RStudio` project then the path will be by default the project's path. Then you can reference a relational path. Or if `/functions.R` is a sub-directory of your home directory that should work as well. – niko Jan 29 '19 at 12:50
  • Hi niko, I have the same issue but I am really lost in my code, can you write an example or tell me where write setwd. I put this: ```{r echo=FALSE} source("C:/Users/myname/Documents/R/data/myfunction.R"). ```. But still doesn't run. – Juan Carlos Rubio Polania Sep 22 '21 at 18:33
0

If the file you source ALSO uses source("...") to reference other files, you will get this same error.

For example:

  1. Your working directory is getwd() ".../projectA"
  2. You have another file you want to reference from another R Project called functions.R. So you try to source this source(".../projectB/functions.R"). This results in the error.
  3. It's because contained in the code of functions.R is source("helper_functions.R"). But your source is using the original path (e.g., .../projectA/), not the project path of functions.R (e.g., .../projectB/)
Jeff Parker
  • 1,467
  • 13
  • 24