R list.files(my_working_directory) shows no files but I know they are there. How to fix? - r

I'm trying to work with R using a directory in my external hard drive as working directory. I can set the working directory to where I want, but when I use list.files R can't see anything. I also can't use read.delim to load a file.
For example:
> rm(list = ls())
> WD<-"../../../Volumes/My_HD/my_working_directory"
> setwd(WD)
> getwd()
[1] "/Volumes/My_HD/my_working_directory"
So far so good, but then:
> list.files(WD)
character(0)
Whilst I would expect a list of the files that I know are there and that I can see and open with Finder and with the Terminal. Actually the R script itself is inside that directory.
If then I try to open a file I get something like this:
> myfile <- read.delim(paste(WD,"/file_name", sep = ""), header = T)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '../../../Volumes/My_HD/my_working_directory/file_name': No such file or directory
I thought I was doing something wrong specifying the path to the directory, but I tried point and click from RStudio (Session>Set Working Directory and either >To Source File Location or >Choose Directory...) and got the same unhappy result.
I'm using R version 2.15.1 on a MacBookPro (OS X 10.7.05). I'm running R from RStudio version 0.97.551, but the problem is not related to RStudio as it happens also when I try directly on R.
Any help will be very much appreciated. Thanks.

Since I think you're wanting to keep some relativity in your paths, and because you're using RStudio, I'll share one strategy that I often use. If I've correctly divined what you're looking for, this should sidestep the issue entirely.
Start by creating a new project from within RStudio. Once you've opened that project, you'll find that your working directory getwd() will be conveniently set where you have your project file at.
If you're doing something larger, you can get yourself off on the right foot by using the "ProjectTemplate" package. On my system:
# install.packages(c("ProjectTemplate"))
library(ProjectTemplate)
create.project("~/Desktop/MyProject")
This sets up a project skeleton, complete with unit test folder, doc folder, and everything else I might need for a "proper" R project.
Next I create a project from RStudio as I mentioned above. Create it from an existing directory. The project will open, and all of your paths will be relative to the root of your 'MyProject' folder.
I also like to initialize a git repository while I'm at it. From an OS X terminal window:
$ cd ~/Desktop/MyProject; git init
...and then add / commit the bare skeleton
$ git add * ; git commit -m "initial project skeleton"
From your RStudio session you can load things relative to your project root. Let's say you had iris.csv in your data subfolder:
read.csv("./data/iris.csv", header=TRUE)

Related

How to run R projects / use their relative paths from the terminal without setwd() resp. cd

I'm kinda lost on that one:
I have set up an R project, let's call it "Test Project.Rproj". The beauty of R projects is the possibility to use relative paths (relative to the .Rproj file). My project consists of a "main.R" script, which is saved on the same level as the .Rproj file.
Additionally I have a directory called 'Output', where I want my plots and exported data to be saved. My "main.R" file looks like the following:
my_df <- data.frame(A = 1:10, B = 11:20)
my_df |>
writexl::write_xlsx(here::here("Output",
paste0("my_df_",
stringr::str_replace_all(as.character(Sys.time()), ":", ""),
".xlsx")))
My final goal is to automate the execution of the 'main.R' file using the Windows Task Scheduler. But in order to do so, I have to be able to run the script from the terminal. The problem here is the working directory. When opening an R project, all the paths are relative to .Rproj file. But in the terminal the current working directory is <C:\Users\my_name>. Of course I could manually set the working directory via cd "path\to\my\project. But I would like to avoid that.
My current call for the execution of the main.R file in the terminal is the following:
"C:\Program Files\R\R-4.1.0\bin\Rscript" -e "source('C:/Users/my_name/path/to/my/project/main.R')"
My two ideas for a solution are the following, but I am happy for other suggestions as well.
In order to replicate the usual use of a project: Is there a way to execute the .Rproj
file from the terminal? In order to create a similar environment as in RStudio, where all the relative paths are working, when executing scripts from the project afterwards?
There are two packages adressing the problem of relative paths: rprojroot and here, where the former is the basis for the latter. I am pretty sure that here does not provide the needed functionality. I tried adding here::i_am("main.R) to my main.R file, but the project root directory still is not found when executing in the terminal from a working directory outside the project.
For rprojroot to work, I think it is also necessary to have your current working directory somewhere within the project. But this package offers a lot of functionality, so I am not sure wheter I am overlooking something.
So I would be happy about any help. Maybe it is impossible and I have to change the working directory manually - then I would be glad to know that as well.
Some links I used in my research:
https://www.tidyverse.org/blog/2017/12/workflow-vs-script/
https://malco.io/2018/11/05/why-should-i-use-the-here-package-when-i-m-already-using-projects/
http://jenrichmond.rbind.io/post/how-to-use-the-here-package/
Thanks a lot!
Edit: My current implementation is an additional R script, where I manually set the working directory via setwd() and source the main.R file. However it is always suggested to avoid setwd, which is why this whole question exists.

R project WD changes "on its own"

I'm working on a project involving several people. We are using GitHub and a Rproject to be able to work on this project on different computers.
But I'm facing a really weird issue when I try to load files and/or get my working directory:
When I type getwd() in R console, I indeed get the path where the project is saved on my computer
> getwd()
[1] "/media/Data/Documents/my_R_project"
When I save getwd() in an object, the WD is not the same and now moves to the path were the script are saved:
folderpath <- getwd()
> folderpath
[1] "/media/Data/Documents/my_R_project/R/Script"
I get the same issue when I try to load a file which is located in /media/Data/Documents/my_R_project/R/Data: when I use read_csv (or any other function like it) and write the file path as
read_csv("R/Data/file.csv") I get and error stating me that there is no such file in the directory /media/Data/Documents/my_R_project/R/Script/R/Data/.
How could I resolve this and make the WD used in read_csv() be the right one ("/media/Data/Documents/my_R_project") so that I don't have to specify the full path every time and that people on another computer can run my script?
I'm working on Ubuntu LTS 20.04
In the end, the error was due to a conflict between the WD of the project and the WD used when I knitted the .Rmd file. The problem was solved by changing the knit options and set its directory as the project directory.

R studio will not remember my file path for a code chunk

When I run the following code I get an error.
library(tidyverse)
day <- read.csv("day.csv")[, -1]
glimpse(day)
Result:
Show in New Window
Error in file(file, "rt") : cannot open the connection
However when I specify the file path it works:
library(tidyverse)
setwd("~/UofUProjects/IS6489")
day <- read.csv("day.csv")[, -1]
glimpse(day)
I don't want to have to specify the file path for every single code chunk I run. Is this a setting I may have accidentally disabled? I tried deleting an re adding the file. I also tried clicking more and choosing set working directory and I still get the same error. I also tried updating Rstudio and R to the newest versions.
here is my working directory
The R markdown file I was using was saved in my download folder instead of my project folder. I was trying to specify the working directory folder with the R studio settings, and thought it would recognize however it did not work unless I actually saved the R markdown file in the project folder or used setwd(). I ran the code without the setwd() after I saved in the correct location and it worked just the way I wanted it to.

Unable to set working directory using here package in R to another location

I have a series of pieces of R code which have been designed to be run on other computers. That is, all code is relative to a root directory, which contains a Rstudio project file, .Rproj. There are no absolute file paths. This works fine when I actually open Rstudio, load the .Rproj file and then run the code.
However some of my code takes hours to run, and I need to set multiple scripts to run one after the other. This means creating a .sh file, and running the R script in turn from the command line. However non of my programs run successfully from the command line, as the root directory is no longer set to that of the .Rproj file. I have read about the here package can be used, which will automatically set the root directory to where ever a .here file is located. This is not the case for me.
The working directory it automatically uses is the home directory I have on the computational cluster I am using. The area where all my files, including the .Rproj and .here files is located in a different directory in which I have a lot more space allocated. Both are accessible from a common parent directory, so I assumed there here() function would be able to locate the directory I want to actually use to run my work. But this is not the case.
Effectively, I would like to set the root directory to a location which is not the default root directory on the system I am using. I have put a .here file there, but this is not located by there here() function, which I believe is its primary objective. Any ideas on how to proceed?
EDIT: I am working on a UNIX system. R version 3.4.2.
My problem was similar, but not exactly the same as yours. Perhaps my solution will work for you. When I opened an RStudio project, I found that if I called "library(here)", the root directory is set where the .Rproj file is located and that "set_here" would not change that directory, despite the 'here' package documentation. Perhaps I was doing something wrong, but I decided to solve the problem with a simple R function that moves up the directory tree until it finds a ".here" file. It then loads the "here" package and that sets the root directory where I want it.
I use "touch .here" in a Terminal outside of R to set my root directory, and then call "init_here()" from my newly opened R project:
init_here <- function() {
`%!in%` = Negate(`%in%`)
files <- dir( all.files = T )
while ( ".here" %!in% files & getwd()!="/" ) {
setwd("..")
files <- dir( all.files = T )
}
library(here)
}
Use Case -
In Unix:
cd( '~/myRoot' )
touch( '.here' )
In RStudio, when I open a project, the calls look like:
R version 4.0.2 (2020-06-22) -- "Taking Off Again"
< R information removed for clarity >
[Workspace loaded from ~/myRoot/myProject/.RData]
> getwd()
[1] "/Users/me/myRoot/myProject"
> init_here()
here() starts at /Users/me/myRoot
> here()
[1] "/Users/me/myRoot"
>
I can now put a ".here" file at the root of each of my RStudio projects and set the expected root directory independently for each project. If you want to get fancy, you could put the function in each project's .Rprofile so that it runs whenever the project is opened. All of my projects have the .Rproj file in the directory above my "R" directory, so my .Rprofile looks like:
source("./R/init_here.R")
init_here()
Hope that helps.
Did you try simply adding a cd /the/path/where/you/put/the/files command in your shell script?
According to this documentation, here() "uses a reasonable heuristics to find your project's files, based on the current working directory at the time when the package is loaded". The "cd" (change directory) command in a shell script changes the current working directory.

Having trouble setting working directory

I created a folder in order for it to be the main work directory meaning all the files I create go there, and files I read will be from there.
For some reason after I created the folder and I'm trying to set it as the working directory I get this message:
Error in setwd("~/") : cannot change working directory
When I try to create files they are saved somewhere different and I get an error when trying to load them. I used the scan function and got:
Error in file(file, "r") : cannot open the connection
In addition: Warning message:
In file(file, "r") : cannot open file 'ff': No such file or directory
The command setwd("~/") should set your working directory to your home directory. You might be experiencing problems because the OS you are using does not recognise "~/" as your home directory: this might be because of the OS, or it might be because of not having set that as your home directory elsewhere.
As you have tagged the post using RStudio:
In the bottom right window move the tab over to 'files'.
Navigate through there to whichever folder you were planning to use as your working directory.
Under 'more' click 'set as working directory'
You will now have set the folder as your working directory. Use the command getwd() to get the working directory as it is now set, and save that as a variable string at the top of your script. Then use setwd with that string as the argument, so that each time you run the script you use the same directory.
For example at the top of my script I would have:
work_dir <- "C:/Users/john.smith/Documents"
setwd(work_dir)
This may help...
use the following code and browse the folder you want to set as the working folder
setwd(choose.dir())
I just had this error message happen. When searching for why, I figured out that there's a related issue that can occur if you're not paying attention - the same error occurs if the directory you are trying to move into does not exist.
Maybe it is the case that you have your path in couple of lines, you used enter to make it? If so, then part of you paths might look like that "/\nData/" instead of "/Data/", which causes the problem. Just set it to be in one line and issue is solved!

Resources