0

I have a very basic question.

I want to set the working directory based on the OS the Rstudio is running(MAC, Windows). Can you please suggest on how to use getwd() and setwd() for getting this done ? What functions can give OS details in R ?

Prradep
  • 5,506
  • 5
  • 43
  • 84
  • Check this: http://stackoverflow.com/questions/4463087/detecting-operating-system-in-r-e-g-for-adaptive-rprofile-files –  Jun 29 '15 at 07:47

1 Answers1

2

You can get the OS details by simply type Sys.info() at the console. I don't have access to R currently but I think the answer should be like this:

a = Sys.info()[1]
if( a == "Windows") { set the working dir in windows}
if( a != "Windows") { set the working dir in other OS}

Sys.info() returns a vector containing the following values which you can find also in the help by ?Sys.info:

sysname 
The operating system name.

release 
The OS release.

version 
The OS version.

nodename    
A name by which the machine is known on the network (if any).

machine 
A concise description of the hardware, often the CPU type.

login   
The user s login name, or "unknown" if it cannot be ascertained.

user    
The name of the real user ID, or "unknown" if it cannot be ascertained.

effective_user
The name of the effective user ID, or "unknown" if it cannot be ascertained. This may differ from the real user in ‘set-user-ID’ processes.

You can return the first element for OS name.

PNS
  • 82
  • 1
  • 6
  • `Sys.info()["sysname"]` gives you the system name without using a magical constant (1). – kasterma Jun 29 '15 at 09:31
  • you can return the values inside a vector by name or by its vector. If you read the R help you will see that the return of Sys.info() function is a vector. Anyways, I tried to clarify the answer as much as possible and also Prradep needs to set the working directory based on the OS. So the magical constant is needed! – PNS Jun 30 '15 at 09:58