根据 OS 在 Rstudio 中设置工作目录

Set working directory in Rstudio based on OS

我有一个非常基本的问题。

我想根据 OS 设置工作目录,Rstudio 是 运行(MAC, Windows)。您能否建议如何使用 getwd()setwd() 来完成这项工作?哪些函数可以在 R 中提供 OS 详细信息?

您只需在控制台键入 Sys.info() 即可获取 OS 详细信息。我目前无法访问 R,但我认为答案应该是这样的:

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 包含以下值的向量,您也可以在 ?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.

您可以 return OS 名称的第一个元素。