set R_USER for multiple users under windows - r

Windows seems to put R libraries in a onedrive directory by default if onedrive is in use. This is undesirable especially if you're using both R and onedrive on multiple computers with the same onedrive account.
How would I set my library to be put inside of C:\users<username>\documents instead of in C:\users<username>\onedrive\documents? There are good solutions here (How do I change the default library path for R packages), but they're mostly focused on solving this for a single windows account. Is there a general way to solve it for all accounts?

Every R installation has an etc/ directory with configuration, in it you can set Rprofile.site or, easier still, Renviron.site.
Files ending in .site should not get overwritten on the next install. Make sure you don't bulk delete though.
You can query where it is via R.home("etc"). On my (Linux) system:
> R.home("etc")
[1] "/usr/lib/R/etc"
>

Really excellent solution from here (https://github.com/r-windows/docs/issues/3):
just create an Renviron.site file in the /etc folder of your Rinstallation, then copy the following line to it:
R_USER=C:/Users/${USERNAME}/Documents
This sets R_USER which in turn sets R_LIBS_USER according to the user directory of each account under windows 10.

Related

Unable to use correct file paths in R/RStudio

Disclaimer: I am very new here.
I am trying to learn R via RStudio through a tutorial and very early have encountered an extremely frustrating issue: when I am trying to use the read.table function, the program consistently reads my files (written as "~/Desktop/R/FILENAME") as going through the path "C:/Users/Chris/Documents/Desktop/R/FILENAME". Note that the program is considering my Desktop folder to be through my documents folder, which is preventing me from reading any files. I have already set and re-set my working directory multiple times and even re-downloaded R and RStudio and I still encounter this error.
When I enter the entire file path instead of using the "~" shortcut, the program is successfully able to access the files, but I don't want to have to type out the full file path every single time I need to access a file.
Does anyone know how to fix this issue? Is there any further internal issue with how my computer is viewing the desktop in relation to my other files?
I've attached a pic.
Best,
Chris L.
The ~ will tell R to look in your default directory, which in Windows is your Documents folder, this is why you are getting this error. You can change the default directory in the RStudio settings or your R profile. It just depends on how you want to set up your project. For example:
Put all the files in the working directory (getwd() will tell you the working directory for the project). Then you can just call the files with the filename, and you will get tab completion (awesome!). You can change the working directory with setwd(), but remember to use the full path not just ~/XX. This might be the easiest for you if you want to minimise typing.
If you use a lot of scripts, or work on multiple computers or cross-platform, the above solution isn't quite as good. In this situation, you can keep all your files in a base directory, and then in your script use the file.path function to construct the paths:
base_dir <- 'C:/Desktop/R/'
read.table(file.path(base_dir, "FILENAME"))
I actually keep the base_dir assignemnt as a code snippet in RStudio, so I can easily insert it into scripts and know explicitly what is going on, as opposed to configuring it in RStudio or R profile. There is a conditional in the code snippet which detects the platform and assigns the directory correctly.
When R reports "cannot open the connection" it means either of two things:
The file does not exist at that location - you can verify whether the file is there by pasting the full path echoed back in the error message into windows file manager. Sometimes the error is as simple as an extra subdirectory. (This seems to be the problem with your current code - Windows Desktop is never nested in Documents).
If the file exists at the location, then R does not have permission to access the folder. This requires changing Windows folder permissions to grant R read and write permission to the folder.
In windows, if you launch RStudio from the folder you consider the "project workspace home", then all path references can use the dot as "relative to workspace home", e.g. "./data/inputfile.csv"

How to install tools for everyone to use in unix

I'm a freshman, and I created a server with my roomates in order to practice in maintaining a server.
We installed CentOS7. And I would like to ask how I can install a tool for everyone to use?
More particularly, we want to install Cromwell. But since, they don't have instructions on how to install on Unix, I downloaded Linuxbrew and installed it like this.
The downside is that it's not visible to the other users connected to the servers.
I know this is a noob question, but any response would be appreciated.
A standard unix machine has programs (tools and so on) installed in predefined directories like /bin, /usr/bin, perhaps /usr/local/bin. Which to choose is another matter, probably you want /usr/bin. Also the environ variable PATH plays a role.
Into the chosen directory there should be a file representing the "tool". You can put a copy of the executable file in that directory, and set (or check) its permissions. Execution permission can be granted to all users, or only some, it depends. In other words,
/home/me/.linuxbrew/Cellar/cromwell
is not a good place for a "system" tool or app; you should copy that executable in /usr/bin, set ownership (perhaps to root?) with chown, and set the correct permissions with chmod.
You can make a hard link of your executable into the directory; this saves space, but also means that there is only one copy of the executable. Having two different copies (the "stable" one, and the other one you can fiddle with) can be handy.
After the executable is reachable and executable from the chosen users, maybe it needs some support files. To find them, it can rely on fixed locations, or some environment variable, or some configuration file. But all these things are outside of the scope of the question.
Try this command:
you#machine$ sudo chmod [who][op][permissions] filename
"who" refers to the users that have a particular permission: the user ("u"), the group ("g"), or other users ("o", also known as "world"). "op" determines whether to add ("+"), remove ("-") or explicitly set ("=") the particular permissions. "permissions" are whether the file should be readable ("r"), writable ("w"), or executable ("x"). As an example:
you#machine$ chmod o+x file
will add executable permission for others to file.

How do I setup a shared R package directory on a server?

I have a shared R package directory on a server in order to maintain consistent package versions for all users. This becomes problematic when someone attempts to install a new version of a package that another user originally installed, or they attempt to install it when that package is loaded elsewhere. In these instances R creates a 00LOCK-PackageName directory in the shared package directory, and the permissions are such that the installer doesn't have write access to many files within the directory. This then requires several people chmod-ind the directory to allow it to be deleted, or having one of our system administrators do the same.
This is an especially acute problem since we use R packages to maintain and deploy our reporting infrastructure. It's something that we're constantly updating and deploying to our shared server.
Are there settings or programs that facilitate shared R package management? Any general tips?
One common solution is to
have everybody be a member of a common group, mayne rapps
have the directory where you share the R packages be group-owned by rapps, and you want to make that 'sticky' -- chmod g=rwt if I recall correctly
have your umask default set in /etc/profile or equivalent to make sure your default
creation mode in in fact 'g+w'; I have used a file /etc/profile.d/local_umask.sh for this with a single command umask u=rwx,g=rwx,o=rx
We ended up having our systems administrator create a script that:
Opened permissions on all directories, subdirectories, and files within our shared package directory
Deleted any directories starting with 00LOCK that were older than 15 minutes
Ran every minute
We haven't run into any problems since.

How to get R to recognize your working directory as its working directory?

I use R under Windows on several machines.
I know you can set the working directory from within an R script, like this
setwd("C:/Documents and Settings/username/My Documents/x/y/z")
... but then this breaks the portability of the script. It's also annoying to have to reverse all the slashes (since Windows gives you backslashes)
Is there a way to start R in a particular working directory so that you don't need to do this at the script level?
You should copy shortcut to R (R.lnk file) to desire folder. Then in "Properties" (right mouse button -> last option) delete anything in field "Start in..." in second tab ("Shortcut"?). If you start R with this shortcut working directory will be that one where the shortcut is.
I don't have english version of Windows so I'm not sure about field names, but they should be easy to find.
Similar questions were in R-windows-faq:
2.5 How do I run it?
2.10 How can I keep workspaces for different projects in different directories?
2.14 What are HOME and working directories?
In 2.14 is mentioned that
The working directory is the directory from which Rgui or Rterm was launched, unless a shortcut was used when it is given by the `Start in' field of the shortcut's properties.
You could use an environmental variable. This can work with Sys.getenv() and Sys.setenv(). For instance:
> Sys.setenv(R_TEST="testit")
> Sys.getenv("R_TEST")
R_TEST
"testit"
If you sent the variable in your script, you should be able to access it from within, and then call setwd() on that output.
Save your workspace to the desired directory and thereafter you just open the workspace from Windows explorer.
I put the following line in front of my scripts and it allows me to work across my computers.
setwd(path.expand("~/path/to/working/directory/") )
where ~ is = to your home directory.
Sys.setenv(HOME = "path") or Sys.setenv(R_USER = "path") can both set the home directory.
In my case, I work on several windows boxes, each have fairly different directory structures, but by setting the home directory properly I can sync code between computers and have them run properly on each one since where I run my R projects have similar directory structures.
If you're using Emacs/ESS, this isn't a problem. I navigate to the directory where my R script is located, open it, then start an R ESS process. An R console pops up with the current directory as R's working directory.
If you haven't converted to Emacs/ESS, I recommend it. (Though to prevent a flame war, I also note there are similar options for Vi users.)
Hope that helps.
Just a detail: instead of reversing the slashes as you say, just add another backslash. Two of these \\ works the same way as one of these /. That makes it at least a little easier.
For Ubuntu:
Insert the following command into your .Rprofile file (usually in your home directory):
setwd(Sys.getenv("PWD"))
Now your default working directory will be whatever directory you launched R from. Keep in mind you can also set up default workspaces in different directories by saving your workspace image as .RData wherever you plan to launch R (startup sources .Rprofile before searching for .Rdata in the cwd).
To set the R work directory like the current directory of the R script that I'm working, I always use a combination of the commands getwd() and setwd(), like this:
path <- getwd()
setwd(path)
or
setwd(getwd())
If you want learn more about it, see this article.
Cheers,
[]'s
To set working directory in R Studio:
Refer detailed slide deck with screen-shots here.
Using setwd(): windows users would need to replace backward slashes '' with forward slashes '/' or double backward slashes '\'
You can do the former using find & replace (Short-cut: Ctrl+F)
Another option: Go to Session --> set working directory --> choose working directory & browse the folder which you want to set as the working directory, click on open
Quickest method (my favorite) use the shortcut 'Ctr+Shift+H' (on windows system), browse the folder which you want to set as the working directory, click on open
To set a permanent working directory (when not in a project) in R Studio:
Refer my quick video on the same: https://youtu.be/hMjzO4bAi70
Go to Tools --> Global Options --> R General [Basic] --> Default Working Directory (when not in a project)
browse the folder which you want to set as the working directory, click on 'Apply' and 'OK'
However, the efficient & better way to organize your work is to create projects & use version control.
Put a shortcut for the R gui into your desired directory. Right-click and look at the shortcut properties. Delete the entry for "Start In" and click OK. When you launch the R gui from this shortcut the default directory will be the folder from which you have launched. Copy/paste this shortcut wherever you desire.

Getting R to store the working directory for every session

I asked this on Super User, but someone suggested that I take it here because there are many more R experts.
The question:
I have to keep navigating to my directory when I go to File > Change dir..., which is particularly annoying.
Does anyone know how to make R remember the previously used directory?
I keep all the code associated with a particular project in a file (or more often a series of files). The first line is usually
setwd(...)
which sets the directory.
Once a workspace has been saved in the desired directory, just start R by opening that workspace (rather than from the desktop or start menu). Then the directory is already set to where you want it.
I may not be answering your question, because it's a bit vague, but some thoughts:
You can store the location of 'my directory' in R's .GlobalEnv so that it starts there when you start R.
This article discuses how to have different working directories with corresponding different ".RData" files.
You could write a custom function that remembers the current directory before you set the new directory
cd <- function(x = "") {
logical (length = 0)
if (!is.logical(x)) {
cwd <- getwd()
Sys.setenv("R_OLDWD"=cwd)
setwd(x)
} else {
setwd(print(paste(Sys.getenv("R_OLDWD"))))
}
}
From the R for Windows FAQ:
The working directory is the directory from which Rgui or Rterm was launched, unless a shortcut was used when it is given by the `Start in' field of the shortcut's properties. You can find this from R code by the call getwd().
The home directory is set as follows: If environment variable R_USER is set, its value is used. Otherwise if environment variable HOME is set, its value is used. After those two user-controllable settings, R tries to find system-defined home directories. It first tries to use the Windows "personal" directory (typically C:\Documents and Settings\username\My Documents on Windows XP and C:\Users\username\Documents on Vista). If that fails, if both environment variables HOMEDRIVE and HOMEPATH are set (and they normally are), the value is ${HOMEDRIVE}${HOMEPATH}. If all of these fail, the current working directory is used.
You can find this from R code by Sys.getenv("R_USER").
This depends on the system that your using. There are a few tricks to use but if your looking to run R from a system menu and have it remember the directory the quick answer is no that won't happen. Linux is pretty easy just navigate to the directory in the terminal first and that will solve the problem. I have no idea about macs, But I can talk about windows extensively. First if you navigate to the directory and save your workspace once then you can use the saved .RData file to double click and restore your workspace including current directory. My personal, and biased opinion is to use an editor like Notepad++ with NppToR that way when you spawn a Rgui window you inherit the active directory from the current script. It also provides a menu command to adjust the working directory to the current script's directory.
Another point is that you can always set the working directory with the setwd("path/to/dir/") command inside any R session on any platform.
I use StatET and Eclipse as my R user interface. It automatically sets the working directory to the location of my project folder. workspace = ${project_loc}.
It also automatically loads any saved workspace, when starting R from a particular project.
On Windows, I put a file Rgui.bat from code.google.com/p/batchfiles in my project directory and use this to start R.

Resources