Can I change where RStudio saves snippets? - r

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.

Related

Issue with serve_site() r blogdown

I'm using blogdown and Hugo to generate a website.
I've created a new R project and have downloaded the theme using:
blogdown::new_site(dir = "Theme", theme = 'pacollins/hugo-future-imperfect-slim') and I can preview the template once downloaded. However, if I close R and then restart and I use the add-in "serve site" I get the following error message:
Error in site_root(config) :
Could not find config.toml / config.yaml under...
and then the directory that the config.toml file is saved in (and all of the rest of the theme files) are listed.
I can't understand what's changing between being able to preview the site in R Studio Viewer, then quitting R and restarting and it not being able to find the config file.
I did find this advice: https://github.com/rstudio/blogdown/issues/48 which suggests that my working directory is not the root directory of the website. There are two things I don't understand:
1) How can the the root directory and working directory work together okay when I initially download the theme, until I quit R and restart, and then they appear to not be the same?
2) How to I change the root directory of the website to fix the issue?
Thanks!
If you followed the blogdown's documentation to create a new site with the command "blogdown::new_site(dir="/your/website/dir")", it will set your working directory to "/your/website/dir".
When you restart R/Rstudio, your current working directory is reset (probably to your home directory). You can check the current working directory with getwd().
The solution is simply set up the working directory:
The simpler way is to execute setwd("/your/website/dir"), then you should be able to run blogdown::serve_site()
The recommended way is to create a Rstudio project at /your/website/dir with File-New Project-Existing Directory. It will create a .Rproj file under the root directory of your website. After that, you can "open project" and work on your website, then preview with serve_site().

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

Changing R options persistently

I want to change the prompt in R from > to R> and I know I should use the options command options(prompt="...") and it works, but then when I restart R the prompt is back to >.
Is there anyway to save the change so that it sticks?
Use .Rprofile file:
You can customize the R environment through a site initialization file or a directory initialization file. R will always source the Rprofile.site file first. On Windows, the file is in the C:\Program Files\R\R-n.n.n\etc directory. You can also place a .Rprofile file in any directory that you are going to run R from or in the user home directory.
At startup, R will source the Rprofile.site file. It will then look for a .Rprofile file to source in the current working directory. If it doesn't find it, it will look for one in the user's home directory. There are two special functions you can place in these files. .First( ) will be run at the start of the R session and .Last( ) will be run at the end of the session.
More details are here

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.

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