Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect which computer I'm running an R script on

Tags:

r

rstudio

I am looking for an R function to return an identifier of the computer the script is being run on, or at the least to distinguish between one of two known computers.

I have two PCs, both running Windows and RStudio. I use the desktop in the office and the laptop over VPN, typically working on the same projects, always using RStudio.

My scripts and permanent data sets are in a commmon repository. However, since I/O to that repository is slow, I keep a local directory for temp files.

On the desktop, I have a dedicated drive, and each project lives in its folder 'D:/workspace/this_project/'. On the laptop, the path is 'C:/Users/myself/Documents/workspace/this_project/' or just '~/workspace/this_project/'.

Currently, I keep two setwd() statements at the top of each script, and I just rely on the fact that one of them will fail because of the file structure.

setwd('~/workspace/this_project') # will fail on the desktop
setwd('D:/workspace/this_project') # will fail on the laptop

This seems like a bad practice.

I've looked through ?"environment variables" and don't see how to get my computer's name on the network or something else that is persistent and unique to the computer.

The desired solution could modify the laptop's tilde expansion to D:/ on the laptop only so that a common '~/workspace/' could be used, or a function using_laptop() like this:

set_project_wd <- function(folder_nm){
  if(using_laptop()) setwd(paste0('~/workspace/',folder_nm))
  else setwd(paste0('D:/workspace/',folder_nm))
}
like image 1000
C8H10N4O2 Avatar asked Oct 15 '15 14:10

C8H10N4O2


Video Answer


1 Answers

If you call Sys.info() you can get your details:

names(Sys.info())
[1] "sysname"        "release"        "version"        "nodename"       "machine"        "login"         
[7] "user"           "effective_user"

the entry under nodename will be your pc name.

Then you can do something like:

set_project_wd <- function(folder_nm){
  if(Sys.info()[[4]]=="mylaptopname") setwd(paste0('~/workspace/',folder_nm))
  else setwd(paste0('D:/workspace/',folder_nm))
}
like image 126
jeremycg Avatar answered Oct 22 '22 00:10

jeremycg