R set working directory to current folder - r

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

Related

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.

Changing directories in Spyder 3.5

I just updated my copy of Spyder on my Windows XP desktop to Spyder 3.5. How do I change directories so that the working directory is the one (in a subfolder in My documents)in which I have my Python Script and .txt data files (e.g. for running a regression)?
I had the same problem. I'm using the latest Spyder. I wanted my files to be in other than my home directory. I tried setting the current directory in Preferences. My files still wound up in my home directory. I tried the "cd" command. It said that the current directory was the one I wanted, but still the files were written to my home directory.
So I saved my script file in the directory I wanted. Now my files go where I would like.

How to permanently set fix my working dir?

Every time I restart R I issue the following command:
setwd("C:/Users/avtarsingh/Downloads")
How do make this my permanent working dir
Create a file called ".Rprofile" and add that command. R will look for this file every time it starts and change to that directory accordingly. More info in the R documentation here
In order to change your working directory permanently, you can find the following file “Rprofile.site” that is located in your installation path “etc” folder and open it in your favorite text editor (i.e., VSCode). Then you can add your permeant working directory to the first line, like the following line of code:
setwd(“C:\your permeant working directory”)
and save it. Next time you open the R-CRAN, your working directory is set to what you have saved.
Any code which you wish to run every R session can be added to Rprofile.site.
On a Windows machine, this file is located for example in C:\Program Files\R\R-3.1.2\etc. On Windows you will need to run your favourite text editor as an administrator to make changes to this file, since it is in Program Files.
This is also a good place to set your local CRAN repository, etc.

Working directory error

I am working with PyCharm 4.04. Since I installed it, every time I open it, as default directory I get this:
C:\Users\Laura\AppData\Local\Temp\main.py1.tmp>cd
Which gives me an error when trying to use the console:
Error:Cannot start process, the path specified for working directory
is not a directory
But even if I change the directory, the message does not disappear.
The terminal, though, it does work and I can run projects, but I would like to use the console.
Another solution is to close the project, run rm -rf .idea and re-open it. Apparently Pycharm gets confused by some direct folder manipulation and doesn't reflect it properly in his .idea/*.xml files
I also got this error, and it got resolved by setting the default working directory. Follow the below path, and set the Working Directory to the folder where your code resides.
File > Settings > Build, Execution, Deployment > Console > Python Console > Working Directory ...
I finally solved the problem.
I think it all started because the first project that I opened with pycharm was in my "download" folder, so the working directory was automatically set to a temporal folder by default and allthough I moved the project to another folder and I manually changed the working directory from the terminal, it was not working.
The solution was creating a new project and giving a correct path to the new project. It seems very easy but it was not that obvious.
In the upper right corner click on small ▼ next to your main to run (look to the left from green right-pointing triangle)
Select Edit configurations.
In ▼ Python select the proper configuration name.
Look at the Configuration panel.
Fix items Script and Working directory.
pycharm
I had this same problem and just had to reinstall pycharm. It's a quickfix and I can't ensure it won't happen again.
I solved this by replacing all instances of the old filename and old directory with the new one in .idea/workspace.xml
It can be done with PyCharm running.
#user1068430 has the answer in the comments to the question:
When you open a project open the directory not a specific python file.
Instead of ~/Documents/myProject/main.py open ~/Documents/myProject
If you "open" the .py file then you'll have to set the working directory (File > Settings > Build, Execution, Deployment > Console > Python Console > Working Directory) every time. If you "open" the directory containing the .py file, then PyCharm will open and all of your .py files will be available in the left window. Select one of them and you're good to go.
i had the same issue, the error comes up when i want to upgrade my packages and when i run my project "this FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\youruser\AppData\Local\Temp\gen_py\3.7\__init__.py'" i found that the Temp file in C:\Users\youruser\AppData\Local was corrupt and it was fixed by deleting the "Temp" file, once deleted it will automatically create a new one with "gen_py" in it
To anyone with similar issues: Python interpreter virtual environment is where your python.exe sits. The working directory is where your script sits. To make everything easier, open a new project, scroll to location where you script is stored, and select. Click the interpreter option, click existing (if old one worked) or choose the python.exe. When asked, open the project in a new window, close old one to avoid confusion.
source = banging my head against the console for past few hours.
if the above mentioned solutions are not working, you can restart a new project.
file > New project...
then,
create a new project.

How to change .Rprofile location in RStudio

I am working with a "factory fresh" version of RStudio on Windows 7. R is installed under C:/Program Files which means the default libraries are stored here, and the two locations contained in .libPaths() on startup are both within this folder.
I want to work with another R library (igraph). Since the C:\Program Files folder is write-protected, I have set up another area to work in: C:\Users\nick\R and installed the igraph library in C:\Users\nick\R\library. I can manually add this location to the .libPaths() variable and use the library with no problems.
However, my problem is getting RStudio to automatically add this location to the .libPaths() variable on startup. I read that I could add the relevant command to my .Rprofile file - but I couldn't find any such file (presumably they are not automatically created when RStudio is installed). I then created a file called .Rprofile containing only this command. This only seemed to work when the .Rprofile file was saved in C:\Users\nick\Documents (which is the path stored in both the R_USER and HOME environmental variables). What I would like is to have the .Rprofile file stored in C:\Users\nick\R.
I have read all the information in ?Startup and it talks about where to store commands that run on startup. But I just can't make this work. For example there seems to be no way to change the location of the home directory without reading a file stored in the home directory. I don't seem to have any .Renviron files and creating these myself doesn't seem to work either.
I would really appreciate an answer in simple terms that explains how I could go about changing where the .Rprofile file is read from.
In Windows, you set the R_USER profile by opening up a command line and running:
SETX R_PROFILE_USER "C:/.../.Rprofile"
Where (obviously) the path is the path to your desired .Rpofile. In R, you can check that it worked:
Sys.getenv("R_PROFILE_USER")
Should return the path you specified. Note that you likely need to have all R sessions closed before setting the R_USER variable.

Resources