Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in file(filename, "r", encoding = encoding) : cannot open the connection

Tags:

r

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

like image 974
Sal-laS Avatar asked Jan 29 '19 12:01

Sal-laS


People also ask

How do you fix error in file file RT Cannot open the connection in R?

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.

What does Cannot open the connection mean in R studio?

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.

What does “error in file (con) R” Mean?

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.

Why can't I open a file in R?

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.

What does the error in file(con) mean?

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.

Why can’t I open a CSV file in RStudio?

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…


2 Answers

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.

like image 101
niko Avatar answered Nov 15 '22 04:11

niko


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/)
like image 33
Jeff Parker Avatar answered Nov 15 '22 05:11

Jeff Parker