R project WD changes "on its own" - r

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.

Related

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.

R set working directory to current folder

I am currently working on a joint project, with all the files stored in a Dropbox. To avoid changing the working directory every time we change something, we use the following code:
this.dir = dirname(parent.frame(2)$ofile)
setwd(this.dir)
This is working fine when sourcing the code in Rstudio, but we are looking for a solution that is working outside RStudio.
The team is working on Mac and Windows.
For Windows, locate the file "Rprofile.site". In there, there will be set the following command setwd("PATH_TO_WD")
The same goes for Mac, the file is usually located at /etc/R/ path.
Just change the setwd in that file and everytime you launch R, the working directory will be set to the directory you want.
EDIT: In my Windows computer, the file is located at "C:\Program Files\R\R-3.2.3\etc

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

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)

Locate the ".Rprofile" file generating default options

In R and RStudio, I think I have messed around with the .Rprofile file a few times, and I currently am loading up an old version of it upon startup of R or RStudio, is there a way that I can quickly find the location of the file that is generating the default options?
Thanks
Like #Gsee suggested, ?Startup has all you need. Note that there isn't just the user profile file, but also a site profile file you could have messed with. And that both files can be found in multiple locations.
You could run the following to list existing files on your system among those listed on the page:
candidates <- c( Sys.getenv("R_PROFILE"),
file.path(Sys.getenv("R_HOME"), "etc", "Rprofile.site"),
Sys.getenv("R_PROFILE_USER"),
file.path(getwd(), ".Rprofile"),
file.path(Sys.getenv("HOME"), ".Rprofile"))
Filter(file.exists, candidates)
Note that it should be run on a fresh session, right after your started R, so that getwd() will return the current directory at startup. There is also the tricky possibility that your profile files do modify the current directory at startup, in which case you would have to start a "no-profile" session (run R --no-site-file --no-init-file) before running the code above.

Resources