0

Disclaimer: I am very new here.

I am trying to learn R via RStudio through a tutorial and very early have encountered an extremely frustrating issue: when I am trying to use the read.table function, the program consistently reads my files (written as "~/Desktop/R/FILENAME") as going through the path "C:/Users/Chris/Documents/Desktop/R/FILENAME". Note that the program is considering my Desktop folder to be through my documents folder, which is preventing me from reading any files. I have already set and re-set my working directory multiple times and even re-downloaded R and RStudio and I still encounter this error.

When I enter the entire file path instead of using the "~" shortcut, the program is successfully able to access the files, but I don't want to have to type out the full file path every single time I need to access a file. Does anyone know how to fix this issue? Is there any further internal issue with how my computer is viewing the desktop in relation to my other files?

I've attached a pic. Here is how my display looks, error message appears at the bottom of the image.

Best,

Chris L.

Phil
  • 7,287
  • 3
  • 36
  • 66
Chris L.
  • 1
  • 1
  • 1
  • Possible duplicate of [What is the best practice to efficiently load files into R (Windows)](https://stackoverflow.com/questions/23871178/what-is-the-best-practice-to-efficiently-load-files-into-r-windows) – NelsonGon Mar 03 '19 at 05:28
  • Is this file path correct and is this file located in the current working directory?! – NelsonGon Mar 03 '19 at 05:32
  • The discussion in this previous post will help on how to effectively use tilde("~") in the context of file paths. https://stackoverflow.com/questions/3488603/how-do-i-use-tilde-in-the-context-of-paths – Lunalo John Mar 03 '19 at 06:19

2 Answers2

1

The ~ will tell R to look in your default directory, which in Windows is your Documents folder, this is why you are getting this error. You can change the default directory in the RStudio settings or your R profile. It just depends on how you want to set up your project. For example:

  1. Put all the files in the working directory (getwd() will tell you the working directory for the project). Then you can just call the files with the filename, and you will get tab completion (awesome!). You can change the working directory with setwd(), but remember to use the full path not just ~/XX. This might be the easiest for you if you want to minimise typing.

  2. If you use a lot of scripts, or work on multiple computers or cross-platform, the above solution isn't quite as good. In this situation, you can keep all your files in a base directory, and then in your script use the file.path function to construct the paths:

    base_dir <- 'C:/Desktop/R/'

    read.table(file.path(base_dir, "FILENAME"))

I actually keep the base_dir assignemnt as a code snippet in RStudio, so I can easily insert it into scripts and know explicitly what is going on, as opposed to configuring it in RStudio or R profile. There is a conditional in the code snippet which detects the platform and assigns the directory correctly.

gatsky
  • 1,180
  • 7
  • 8
0

When R reports "cannot open the connection" it means either of two things:

  1. The file does not exist at that location - you can verify whether the file is there by pasting the full path echoed back in the error message into windows file manager. Sometimes the error is as simple as an extra subdirectory. (This seems to be the problem with your current code - Windows Desktop is never nested in Documents).

  2. If the file exists at the location, then R does not have permission to access the folder. This requires changing Windows folder permissions to grant R read and write permission to the folder.

In windows, if you launch RStudio from the folder you consider the "project workspace home", then all path references can use the dot as "relative to workspace home", e.g. "./data/inputfile.csv"

GGAnderson
  • 1,993
  • 1
  • 14
  • 25