Changing the temporary directory in R - r

First of all, I am aware of the following discussion covering the same topic. However, following the instructions, I can still somehow not change the temporary directory.
I installed Anaconda for my local user and then installed R. When I try to start R, I get the following error
Fatal error: cannot create 'R_TempDir
The problem seems to be that there is no available space left in /tmp. Unfortunately, I need administration rights to either allocate more space or to remove Rtmp-files.
I followed some answers of the other discussion and changed the TMPDIR path in my Renviron file to a local folder. However, this seems to be ignored since I get the exact same error. My Renviron file look as follows:
TMPDIR="${HOME}/user_name/tmp"
TMP="${HOME}/user_name/tmp"
TEMP="${HOME}/user_name/tmp"
, where I replace my actual user name with "user_name". I was actually suprised that my temporary directory is a root directory and not under my user.
If someone has any idea what's wrong with my approach, I would very much appreciate some help.

Related

Installation packages no longer found after updating my Mac to 12.2.1

I'm using tcsh shell to access installation packages for my research. After a recent update, my login shell is no longer able to find where those installation files are located. I receive a prompt like this:
!! Attention: GEMC installation check /opt/jlab_software/2.5/Darwin_macosx12.2.1-clang13.0.0/gemc/2.9/gemc not found
Where GEMC is one of several installation files.
I see that it is looking in the directory Darwin_macosx12.2.1-clang13.0.0, which is not where the files are located, but rather they are located in Darwin_macosx12.1-clang13.0.0. I tried creating a symbolic link with the following command:
ln -s Darwin_macosx12.2.1-clang13.0.0 Darwin_macosx12.1-clang13.0.0
But I am still receiving the error informing me that the files are not found. I figured that I incorrectly used the symbolic link (and probably still am) so I pasted in the entire paths in the above command, and still no luck.
I am still kind of a newbie when it comes to Unix commands and navigation, so I apologize if I explained this poorly. That being said, I would really appreciate if someone could help me find a solution to this problem. Thank you in advance!

Change temporary directory without opening R

Today morning when I tried to start R in my Ubuntu machine, I got a new error saying :
Fatal error: cannot create 'R_TempDir'
However when I search over Internet, I realized that my TMP directory is full as below :
df /tmp
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 81120924 81104540 0 100% /
There is good discussion available how to change that TMP directory here
Change temporary directory
However, this requires opening R and then change modify the .Renviron file. However since I cant open R now, I cant use above solution to my problem.
So, I am looking for some way to modify the TMP directory to some other custom folder e.g. '/opt/R' without opening R
Any suggestion will be highly appreciated.
Thanks,

Issue: Working Directory in R Studio "stuck" on directory containing open R Markdown file

I am using the most recent version of R (3.3.2), running in the most recent version of RStudio (1.0.136) on MacOS Sierra (10.12.3). I am running into an issue in which my working directory corresponds, and is stuck on, the directory that contains the .RMD file I currently have open in RStudio. Upon opening the file, the working directory is correctly set to the directory holding the .Rproj file. When I go to load in a file with a path relative to that directory, however, I get an error that there is no such file in the current working directory, and the error returns the location of the .RMD file as that working directory.
The working directory, however (using getwd()) still reads where the working directory is supposed to be, and no matter where I try to set it, I still get the same error message when I try to read in a file. Notably, I do NOT get an error message that the working directory cannot be changed--R tells me that the working directory has been changed, and that directory is allegedly the current working directory...but it's not.
I have tried fully (as far as I am aware) uninstalling R and R studio and reinstalling them, to no avail. Does anyone have a solution? This is frustrating the heck out of me right now, since I have to revise all the relative paths in the notebooks that I have defined to do my work in the interim.
Extra information in case it's relevant: I restored from a Time Machine backup that I suspect may have been corrupted somehow; some contents of my Applications folder were missing that I had to move over manually. Could this be causing the issue? Are there other system files that R depends on when interacting with the filesystem that I might look to? I'm trying to avoid doing a clean OS install or a piecemeal rebuilding of my files, since I don't know if that's actually the issue.
Thanks in advance!
This is a known feature/bug of RStudio notebooks (Working Directory about halfway down). Notebooks are executed in the same directory as the file. As #Simon Jackson noted, you can change this using knitr::opts_knit$set(root.dir = normalizePath()).

What is '.R' folder and where to look for it?

I am a beginner in R. I use Rstudio on my MAC OSX.
I have no idea what does this mean 'create a folder ~/.R and put a Makevars in it.'
I went word by word and tried creating '.R' in my home(~) directory. MAC doesnt allow it saying "You can’t use a name that begins with a dot “.”, because these names are reserved for the system. Please choose another name." ( of course!)
Next I tried to look for .R folder, hoping that it might already be there. But couldn't find it anywhere.
Can anyone please explain where is this '.R' folder on our system? Or how is the whole R folder hierarchy is built.
I tried doing this:
path2 = normalizePath("~/.R")
Warning message:
In normalizePath("~/.R") :
path[1]="/Users/as82986/.R": No such file or directory
setwd(path2)
Error in setwd(path2) : cannot change working directory
Problem is I couldn't even find anything on google because looking for '.R' folder threw pretty vague and unrelated results.
Also, would appreciate if I am not directed to any more documentations. Please. And thank you so much for helping me.
You need to use the Terminal.app to open a Unix console and then type:
$ mkdir ~/.R
The dollar sign is there to remind you that this is not the R console which has a ">" prompt. (Do not type the "$".) You are going to need to learn a few more lessons about the Unix commands if you plan to compile packages from source. I generally run my Finder with ShowAllFiles set to Yes. Do a google search if that is not meaningful to you.

How do I change the default library path for R packages

I have attempted to install R and R studio on the local drive on my work computer as opposed to the organization network folder because anything that runs through the network is really slow. When installing, the destination path shows that it's my local C:drive. However, when I install a new package, the default path shown is my network drive and there is no option to change:
.libPaths()
[1] "\\\\The library/path/I/don't/want"
[2] "C:/Program Files/R/R-3.2.1/library"
I'm running windows 7 professional. How can I remove library path [1] and make path [2] my primary for all base packages and all new packages that I install?
Windows 7/10: If your C:\Program Files (or wherever R is installed) is blocked for writing, as mine is, then you'll get frustrated editing RProfile.site (as I did). As specified in the accepted answer, I updated R_LIBS_USER and it worked. However, even after reading the fine manual several times and extensive searching, it took me several hours to do this. In the spirit of saving someone else time...
Let's assume you want your packages to reside in C:\R\Library:
Create the folder C:\R\Library. Next I need to add this folder to the R_LIBS_USER path:
Click Start --> Control Panel --> User Accounts --> Change my environmental variables
The Environmental Variables window pops up. If you see R_LIBS_USER, highlight it and click Edit. Otherwise click New. Both actions open a window with fields for Variable and Value.
In my case, R_LIBS_USER was already there, and Value was a path to my desktop. I added to the path the folder that I created, separated by semicolon. C:\R\Library;C:\Users\Eric.Krantz\Desktop\R stuff\Packages.
(NOTE: In the last step, I could have removed the path to the Desktop location and simply left C:\R\Library).
See help(Startup) and help(.libPaths) as you have several possibilities where this may have gotten set. Among them are
setting R_LIBS_USER
assigning .libPaths() in .Rprofile or Rprofile.site
and more.
In this particular case you need to go backwards and unset whereever \\\\The library/path/I/don't/want is set.
To otherwise ignore it you need to override it use explicitly i.e. via
library("somePackage", lib.loc=.libPaths()[-1])
when loading a package.
Facing the very same problem (avoiding the default path in a network) I came up to this solution with the hints given in other answers.
The solution is editing the Rprofile file to overwrite the variable R_LIBS_USER which by default points to the home directory.
Here the steps:
Create the target destination folder for the libraries, e.g.,
~\target.
Find the Rprofile file. In my case it was at C:\Program Files\R\R-3.3.3\library\base\R\Rprofile.
Edit the file and change the definition the variable R_LIBS_USER. In my case, I replaced the this line file.path(Sys.getenv("R_USER"), "R", with file.path("~\target", "R",.
The documentation that support this solution is here
Original file with:
if(!nzchar(Sys.getenv("R_LIBS_USER")))
Sys.setenv(R_LIBS_USER=
file.path(Sys.getenv("R_USER"), "R",
"win-library",
paste(R.version$major,
sub("\\..*$", "", R.version$minor),
sep=".")
))
Modified file:
if(!nzchar(Sys.getenv("R_LIBS_USER")))
Sys.setenv(R_LIBS_USER=
file.path("~\target", "R",
"win-library",
paste(R.version$major,
sub("\\..*$", "", R.version$minor),
sep=".")
))
Windows 10 on a Network
Having your packages stored on the network drive can slow down the performance of R / R Studio considerably, and you spend a lot of time waiting for the libraries to load/install, due to the bottlenecks of having to retrieve and push data over the server back to your local host. See the following for instructions on how to create an .RProfile on your local machine:
Create a directory called C:\Users\xxxxxx\Documents\R\3.4 (or whatever R version you are using, and where you will store your local R packages- your directory location may be different than mine)
On R Console, type Sys.getenv("HOME") to get your home directory (this is where your .RProfile will be stored and R will always check there for packages- and this is on the network if packages are stored there)
Create a file called .Rprofile and place it in :\YOUR\HOME\DIRECTORY\ON_NETWORK (the directory you get after typing Sys.getenv("HOME") in R Console)
File contents of .Rprofile should be like this:
#search 2 places for packages- install new packages to first directory- load built-in packages from the second (this is from your base R package- will be different for some)
.libPaths(c("C:\Users\xxxxxx\Documents\R\3.4", "C:/Program Files/Microsoft/R Client/R_SERVER/library"))
message("*** Setting libPath to local hard drive ***")
#insert a sleep command at line 12 of the unpackPkgZip function. So, just after the package is unzipped.
trace(utils:::unpackPkgZip, quote(Sys.sleep(2)), at=12L, print=TRUE)
message("*** Add 2 second delay when installing packages, to accommodate virus scanner for R 3.4 (fixed in R 3.5+)***")
# fix problem with tcltk for sqldf package: https://github.com/ggrothendieck/sqldf#problem-involvling-tcltk
options(gsubfn.engine = "R")
message("*** Successfully loaded .Rprofile ***")
Restart R Studio and verify that you see that the messages above are displayed.
Now you can enjoy faster performance of your application on local host, vs. storing the packages on the network and slowing everything down.
I was struggling for a while with this as my work computer (with Windows 10) created the default user library on a network drive, which would slow down R and RStudio to an unusable state.
In case this helps someone, this is the easiest way I found, without requiring admin rights:
make sure the directory you want to install your packages into exists. If you want to respect the convention, use: C:\Users\username\R\win-library\rversion (for example, something like: C:\Users\janebloggs\R\win-library\3.6)
create a .Renviron file in your home directory (which might be on the network drive?), and in it, write one single line that defines the R_LIBS_USER variable to be your custom path:
R_LIBS_USER=C:\Users\janebloggs\R\win-library\3.6
(feel free to add comments too, with lines starting with #)
If a .Renviron file exists, R will read it at startup and use the variables as they are defined in there, before running the code in the .Rprofile. You can read about it in help(Startup).
Now it should be persistent between sessions!
After a couple of hours of trying to solve the issue in several ways, some of which are described here, for me (on Win 10) the option of creating a Renviron file worked, but a little different from what was written here above.
The task is to change the value of the variable R_LIBS_USER. To do this two steps needed:
Create the file named Renviron (without dot) in the folder \Program\etc\ (Program is the directory where R is installed--for example, for me it was C:\Program Files\R\R-4.0.0\etc)
Insert a line in Renviron with new path: R_LIBS_USER = "C:/R/Library"
After that, reboot R and use .libPaths() to confirm the default directory changed.
I think I tried all of the above and it didn't work for me. This worked, though:
In home directory, make a file called ".Renviron"
In that file, write:
.libPaths(new = "/my/path/to/libs")
Save and restart R if you had it open

Resources