Change home directory for R - r

Somehow, my home and library directories in R got changed to a cloud location, which is messing up a bunch of paths, and now, I can't seem to change it back. When I type path.expand("~") in R, I get back "C:/Users/MyName/OneDrive/Documents", but I was expecting to get "C:/Users/MyName/Documents".
When I try .libPaths(), I get "C:/Users/MyName/OneDrive/Documents/R/win-library/3.4" and "C:/Program Files/R/R-3.4.3/library", but I only want the latter.
I have tried uninstalling and reinstalling both R and RStudio (thus now working with the most-recent versions of each), but the cloud path persists. I have seen posts elsewhere on SO about setting things in the .Rprofile file, but I don't think that's the right option, especially since the .Rprofile file would then have to be in a cloud location, which I don't want.
I have looked at my environment variables in the control panel (I'm on Windows 10) and looked at PATH, but there's nothing there that specifies the cloud directory, so I don't know where it's coming from.
How do I permanently change my home directory and also make sure that .libPaths is pointing to only the actual library directory?

This is based on your windows environment variable HOME. You need to reset HOME to the path that you want "C:/Users/MyName/Documents"
If you want to do that from within R, you can use:
Sys.setenv(HOME="C:/Users/MyName/Documents")
This change would not be permanent. If you wish to avoid doing this every time you run R, you could put the above statement in your .Rprofile file. There is a nice article on setting up your .Rprofile in the RStudio support

Since you mention you are
in Windows 10
you can also set the R Home Directory just for R without changing your system HOME with a special environment variable,
R_USER
Adding this to your Environment Variables with the path you want for your R Home will set the R Home path without changing your system HOME.
RStudio looks for R_USER first (and then moves on to HOME).

Related

R Studio Fatal error: 'R_TempDir'contains space

I tried to download the latest version (4.2.0) of R on my windows computer and received this error message upon trying to open the program. "Fatal error: 'R_TempDir' contains space". Has anyone ever had this issue or have any ideas on how to resolve it? I have tried reinstalling the program and moving where the program is stored but no luck.
I also experienced this issue with 4.2.0 of R on Windows 10 but not prior versions of R.
In my case, the reason was the default temporary file folder was within my user directory and my user ID has a space in it. Apparently this causes an issue.
I was able to work around it by setting the global environment variable TMPDIR in windows:
Go to the search box next to the start button and type "environment"
Click on the choice which says "Edit the system environment variables"
Click the button that says "Environment Variables" near the bottom of the window
Click "New" and enter TMPDIR for variable name and some suitable directory without spaces under variable value (I chose C:\Temp)
As an aside, I do not believe this is an RStudio specific issue (and 4.2.0 isn't a version for RStudio anyway).
Thank you Venk for your post. It didn't work for me, but it pointed me in the direction of a solution that did.
I changed the values of my TEMP and TMP variables to a folder that had no spaces in it or in any folder name in the path (as you note).
I'm working on a Windows 10 machine.
Rob
P.S. My first solution was to revert to an earlier version of R, which initially worked. Recently though, I got the same error, even using a previous version. I'm no expert, but the whole thing is very weird IMO.
Had the same issue, and the other proposed solutions didn't resolve it for me. (R 4.2.2 and Windows 10.)
Solution found here worked for me:
create a file named renviron.site that contains the following text, and save it to the Desktop, using a text-editing program.
TMP=C:/Temp TEMP=C:/Temp TMPDIR=C:/Temp
if a copy already exists, may need to handle that case-by-case
move the file to C:\Program Files\R\R-4.2.x\etc
I wasn't able to save directly to this folder, despite being in the admin account.
I tried the other solutions and none worked. I came across this post that suggested to not install R in the /Program Files/ folder, to instead modify the installation to the folder C:\R\ which worked.
I didn't go back and undo the work done based on previous suggestions, so I'm not sure if they too are needed.

Rstudio - cannot save files or open Rscripts 'The system cannot find the path specified'

Error when saving Rscripts
Error when opening Rscripts
Hi guys,
Have been stuck on this problem for the past year or so and would really appreciate any help. I am unable to save any Rscripts in Rstudio, as I keep getting the first error above. The one exception is, when running Rstudio with administrative permissions, - when saving into C:/Users, which is apparently the default working directory. I am also able to open Rscripts from C:/Users, but not from anywhere else. When I change my working directory using setwd(), Rstudio accepts the change but I am unable to save or open files in the new directory (or anywhere else).
I have tried fully uninstalling and reinstalling Rstudio and R, as well as adding the folder R and Rstudio are in, and my desired working directory folder into the 'PATH' variable in windows (through 'edit the system environmental variables'). But nothing seems to be working. Have also searched online pretty extensively but haven't managed to find a working solution so far.
I am using the latest 2021.09.1 Build 372 of Rstudio, and R 4.0.4. However, I have also faced the same problem on the last few versions of Rstudio and R over the past year as I have tried to update hoping that it would fix the problem but to no avail. I downloaded R and Rstudio from CRAN and the Rstudio website respectively.
Thanks a lot!
This exact thing happened to me. It turned out for me that when I updated my Window's environment variables, I added an extra slash for my HOME variable. This screwed things up with RStudio.
Search for "Edit the system environment variables" after pressing the Windows button and edit the HOME variable to make sure there is no trailing slash.
Good
C:\Users\leune006
Bad
C:\Users\leune006\
I hope that helps you and whoever else comes across this frustrating "bug"!

How can I create a user library for R on Windows 10?

I want to follow the advice I've read and heard to have both a main library in R_HOME/library and a user library. I'm using W10 on a desktop machine (not important, except that it gives me a name by which to refer to it), and I can't make R use the user library.
I have succeeded in doing that on a W10 laptop: C:/R/R-4.0.2/library contains some 30 recommended packages, and C:/Users/[username]/Documents/R/win-library/4.0 con contains a much larger number of packages in my user library.
As I recall, and as I wrote down when I did an upgrade on a server, all you have to do to create a site-library is to create a directory called C:/R/R-4.0.2/site-library, and R will use that the next time it starts.
To create a user library, create the directory C:/Users/[username]/Documents/R/win-library/4.0.
That seemed to work on my laptop, for I have seemingly a working R library and a user library there.
That seemed to work on the server, too: I have a library and a site-library.
In both cases, .libPaths() shows the same libraries that I see with Dired on the disk.
I tried to do the same thing on the desktop machine, and i can't make it work.
I created a directory C:/Users/[username]/Documents/R/win-library/4.0, restarted R, and ran .libPaths(); the only directory that was listed was C:/R/R-4.0.2/library.
Because I thought the Documents in that path seemed odd, I tried it again using C:/Users/[username]/R/win-library/4.0, still with no success.
https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Managing-libraries seems pertinent, but I'm not sure how to interpret the output of Sys.getenv("R_LIBL_USER). I get "\\[toplevel]\[nextlevel]\Home$\[username]/R/win-library/4.0", which I presume is a long-winded way to get to /Home$/[username]/R/win-library/4.0 (aka C:/Users/[username]/R/win-library/4.0.
Suggestions? I've tried a number of other suggestions from SO, all to no avail.

Locating and opening/ updating the Rprofile.site file in MAC

Running RStudio in MAC (not server) and would like to know where to locate the Rprofile.site file in my working directory. Yes I have read ?Startup and I know exactly what I want to do with it (provided I find the file first) and yes I know where my getwd() is.
I have also gone through a bunch of posts in SO already which vaguely touch on/ answer this. Surely this will keep coming up so I will ask the question: Is there a literal file we can open and update with all the great start up code that has been shared and if so, where is it located in a MAC directory OR is there something more to it as I cannot locate this file.
1 answer I found mentions to create a text file then use terminal to copy visible contents of text file to invisible Rprofile? INVISIBLE??
Rprofile.site and .Rprofile are different files. Only files with a lead "." are treated as invisible by the Mac Finder. You didn't specify which OSX version you're using. If it's pre-Mountain Lion, you can IIRC use System Preferences to choose to display invisible files in Finder windows. Apparently it's more difficult in MountainLion. There are third-party tools like Onyx which allow you do set invisibility as well.
Given all that, I recommend you check the FAQ and forums at RStudio for specific info on where RStudio's installation expects to see these files.

How to load packages automatically when opening a project in RStudio

Every time I restart RStudio-it requires me to reload all of the packages that were loaded in the workspace previously. I can't seem to figure out what the problem is, RStudio is saving the projects when it closes them.
How can I make sure that RStudio reloads the necessary packages when I open the project?
I presume you want to say that you have to reload all of the packages that were loaded in the workspace previously. That's not an error, that's by design.
If you want to load some packages at startup in a project, you can do so by creating a file called .Rprofile in the project directory, and specify whatever code you want RStudio to run when loading the project.
For example:
cat("Welcome to this project.\n")
require(ggplot2)
require(zoo)
would print a welcome message in the console, and load ggplot2 and zoo every time you open the project.
See also http://www.rstudio.com/ide/docs/using/projects
In general there's nothing different to default package loading in RStudio than in R (How to load packages in R automatically?). Upon startup R checks for an .Rprofile file in either your local, or fail, that, home or install directory (on Mac/Linux: ./.Rprofile or else ~/.Rprofile) and executes it, and hence any options(defaultPackages...)) or other package-load-related commands it contains.
The only small difference is that RStudio "helpfully" changes your default path before startup see "RStudio: Working with Projects", so you might load a different or missing .Rprofile or the wrong .Rprofile, depending on whether you've opened an RStudio Project or just plain files, and what your RStudio default working directory is set to. It's not always clear what directory you're in, so sometimes this causes real grief.
I tend to use RStudio without defining my code as an RStudio Project, simply because it's heavy-handed and creates more files and directories without adding anything (to my use case, anyway).
So the solution I found to maintaining .Rprofile and making sure the right one gets loaded is a trusty old Unix link from the project directory to my ~
ln -s ~/.Rprofile ./.Rprofile
(If you're on Windows it's more painful.)
You don't need to have one global .Rprofile, you could keep task-specific ones for different types of projects, or trees, or (say) a .Rprofile.nlp, .Rprofile.financial, .Rprofile.bio and so on. As well as options(default.packages, you can gather all your thematically-related settings: scipen, width, data.table/dplyr-specific options, searchpath...
Power tips:
obviously keep backups or SCM of your valuable .Rprofile(s)). Definitely make sure git is tracking it, so don't put it in .gitignore
if you have multiple .Rprofiles, put a cat("Loading .Rprofile.foo") line in each one so you can see from console that the right .Rprofile.xyz got loaded
after every project, revise, trim, tweak your .Rprofile; add new use case stuff, comment out irrelevant stuff, commit the changes to git

Resources