File & Connection Errors

Cannot open connection

Matches: cannot open the connection

WHAT THIS ERROR MEANS

R couldn't open a file or URL. The file might not exist, the path might be wrong, or you don't have permission to access it.

WHY IT HAPPENS — 4 COMMON CAUSES

1. The path is relative to a different working directory

Relative paths resolve against getwd(), which is not necessarily where the script lives. The same read.csv("data.csv") succeeds from one working directory and fails from another, which is why a script can work in RStudio and fail under Rscript.

2. The file name is not quite right

A wrong extension, a hidden .txt appended by Windows, or a case mismatch. macOS and Windows are case-insensitive for file names while Linux is not, so Data.csv and data.csv are the same file locally and different files on a server.

3. Backslashes in a Windows path

"C:\data\file.csv" is invalid because backslash is R’s escape character. Use forward slashes, which R accepts on Windows, or double the backslashes.

4. Permissions, locks, or a network problem

The file may exist but be unreadable — open exclusively in Excel, on a disconnected network drive, or owned by another user. For a URL, the cause is more often connectivity, a proxy, or an HTTPS endpoint than a wrong address.

HOW TO DIAGNOSE WHICH ONE IT IS

  1. Test the path directly: file.exists("data/file.csv") returns TRUE or FALSE with no ambiguity.
  2. Print getwd() to see what relative paths are resolving against.
  3. List what is actually there with list.files("data") — this catches extension and case mismatches at once.
  4. Build paths with file.path("data", "file.csv") rather than pasting separators by hand.
  5. For a URL, try it in a browser first to separate a bad address from a network or proxy problem.

HOW TO FIX IT

1. Check the file path with file.exists().

2. Use getwd() to see your working directory.

3. Use an absolute path or setwd() to the correct directory.

CODE EXAMPLES

BAD — THIS CAUSES THE ERROR
read.csv("data.csv")  # file not in working directory
GOOD — CORRECT APPROACH
read.csv("/path/to/data.csv")  # absolute path
# or
setwd("/path/to/")
read.csv("data.csv")

MORE SCENARIOS THAT TRIGGER THIS

Windows path with backslashes

BAD
read.csv("C:\Users\me\data.csv")
# Error: '\U' used without hex digits in character string
GOOD
read.csv("C:/Users/me/data.csv")
# or
read.csv("C:\\Users\\me\\data.csv")

R treats backslash as an escape, so \U starts a Unicode escape sequence. Forward slashes work on every platform and are the simpler habit.

Paths that survive being run from anywhere

BAD
read.csv("data/sales.csv")
# breaks whenever the working directory is not the project root
GOOD
library(here)
read.csv(here("data", "sales.csv"))

here() anchors paths to the project root — found via an .Rproj file or a .here marker — so the same code works from the console, from a subdirectory, under Rscript and inside a knitted document.

PACKAGE-SPECIFIC NOTES

This arrives as a warning followed by an error, and the warning carries the useful detail — the exact path R tried and the reason, such as "No such file or directory" versus "Permission denied". Read it before the error. readr and data.table produce clearer messages than base R for the same situation. In WebR the browser virtual filesystem is separate from your machine, so files must be written into it before they can be read; a path that works in desktop R will not resolve there until the file has been mounted or uploaded.

FUNCTIONS WORTH KNOWING

file.exists()getwd()setwd()list.files()file.path()here::here()normalizePath()

FREQUENTLY ASKED QUESTIONS

Why does it work in RStudio but fail with Rscript?

RStudio sets the working directory to the project root; Rscript uses whatever directory you invoked it from. Any relative path then resolves differently. The here package removes the difference entirely.

Should I use setwd() at the top of my scripts?

Better not to — it hardcodes one machine’s layout and breaks for anyone else. An RStudio project plus here() gives the same convenience without the fragility.

The file exists and I still get the error. What now?

Check readability rather than existence: file.access("data.csv", 4) returns 0 when the file is readable. A file open exclusively in Excel, or on a disconnected network share, exists but cannot be opened.

Still stuck?

Paste your code in RChat and the AI will fix this error in context.

Try RChat Free →

RELATED FILE & CONNECTION ERRORS