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
To fix it for the current session, use the setwd() command. Alternatively, you can specify the exact file name directly in your R code once you have set the working directory properly. It is possible you are having issues with relative R file references to directories above and below the current working directory.
However, as you can see the RStudio console returned the error message “Error in file(file, “rt”) : cannot open the connection”. The reason for this is that we didn't properly specify the working directory in which the csv file is stored.
The “error in file (con, “r”) : cannot open the connection” error message is an example of this problem. This sort of “cannot open folder/access denied” error message always involves a problem opening a file but not necessarily the same one. This is the type of error that is hard to diagnose and fix.
The first reason is a problem with the file path. This would be a case where the directory is off in some fashion making impossible for the program to access the file. The second reason is that the R compiler does not have permission from your computer to access the file.
The error in file (con message occurs when trying to access a file in the program cannot access it. It is not limited to one function but seems to be a general error resulting from a problem accessing a data file. Because this error can occur with different functions and file types, it is hard to understand and correct.
However, as you can see the RStudio console returned the error message “Error in file (file, “rt”) : cannot open the connection”. The reason for this is that we didn’t properly specify the working directory in which the csv file is stored. The following example explains how to solve this error…
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.
If the file you source ALSO uses source("...")
to reference other files, you will get this same error.
For example:
getwd()
".../projectA"
functions.R
. So you try to source this source(".../projectB/functions.R")
. This results in the error.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/
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With