How can you set the working directory quickly to a file's location in RStudio? - r

The old method is the following :

There's a new feature on RStudio.
Just right-click on the a script's tab and choose "Set Working Directory"
The script has to be saved first as an R file for this to work (see this comment)

Related

Rprofile and worksplace

I'm using macOS and I have a Rstudio, I always use my personal folder to work with R in Rstudio, I set the default working directory by
Rstudio -> Preference -> General -> R Session
and add
~/R
as the Default working directory.
Then I created a .Rprofile just to increase the history size by creating a .Rprofile file as:
usethis::edit_r_profile()
and the add the next line
## Increase the size of my Rhistory file
Sys.setenv(R_HISTSIZE='100000')
and then save it using the floppy icon, and it create the .Rprofile in:
~/.Rprofile
my question is, do I have to change the .Rprofile to the working directory ?
~/R/.Rprofile
or it has to be in home ?
~/.Rprofile
and if I want to move it to my personal working directory (~/R), how to make that Rstudio load it from my personal folder ???
Thanks

Can I change where RStudio saves snippets?

I would like to change where RStudio saves the r.snippets file that stores my code snippets. According to this site, RStudio should save to ~/.R/snippets/r.snippets. I use R_USER=C:/Users/JT/R in my Renviron.site file to set the location of ~. I think this code works, because when I check the location of ~ in RStudio I get:
> path.expand("~")
[1] "C:/Users/JT/R"
However, when I edit the snippets in RStudio it creates the r.snippets file in the folder C:\Users\JT\Documents\.R\snippets\. I want the r.snippets file to be saved in the folder C:\Users\JT\R\.R\snippets\.
Any ideas what I'm doing wrong? Thanks.
Over on the RStudio Community site this same question was asked as Can you change the directory the Rstudio looks for the r.snippets file?
Very helpful answer by RStudio employee kevinushey was:
While that directory isn't currently configurable, you might have luck
creating a symbolic link (or, on Windows, a junction point) to
re-route the snippets directory to another location.
tom_greenwood, the user who asked the question followed up with details of the steps he used:
1. Put you existing r.snippets file in the new directory on the shared drive. I called mine 'snippet files'
2. Delete the snippets directory which is inside the .R directory
3. Run cdm as an administrator.
4. Enter the command mklink /D "C:\Users\name.surname\Documents\.R\snippets" "T:\shared directory\snippet files"
5. Restart Rstudio.

How do you edit a .rds file in RStudio?

I am trying to run an R script that I've inherited from a colleague. This script references a .rds file called config.rds. It stores some configuration settings. I need to change those settings. However, when I attempt to open the file in the Rstudio editor, a "Load R object" prompt pops up. I cannot figure out how to open the file for editing.
You can't open the file for editing - it is a binary file that stores the internal representation of R data objects.
You can only really read it into R to create a new R object, and then save a modified copy of that R object into a new or (the same) .RDS file. Example:
config = readRDS("config.rds")
config$username = "fnord"
saveRDS(config, "config.rds")

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.

Resources