在 Windows 中使用 R(或特别是 R Studio)在指定文件夹中打开文件资源管理器

Open File Explorer at specified folder using R (or specifically R Studio) in Windows

有没有一种方法可以使用 Windows 上的 R/R Studio 中的命令打开文件资源管理器窗格,并可能将路径指定为参数?例如:

open_folder(getwd())

将在工作目录中打开文件资源管理器。它就像 choose.dir 的对立面,因为您从路径转到文件资源管理器,而不是从文件资源管理器转到路径。这将是单击文件的命令行版本 >> 更多 >> 在 R Studio 中的新 Window 中显示文件夹。

我真的不知道如何编写代码来直接使用 Windows,所以我正在寻找已经实现或可以在 R 中实现的东西。

使用

rstudioapi::selectFile(path = "path/to/directory")

这将打开一个文件选择器。

事实证明,R 执行 运行 PowerShell 命令非常简单(参见 ), and then for PowerShell open to File Explorer (and not simply a dialogue box) at a given directory (see here)。

这是一个围绕 base::system 的包装函数:

od <- function(path = getwd()){
  path <- normalizePath(path)
  text_command <- paste0("powershell explorer ", path)
  system(text_command)
  invisible(TRUE)
}

utils::browseURL() 函数是基础 R 的一部分,因为 utils 包是作为 R 的一部分安装的。该函数打开一个 URL,可以是网站或本地文件夹。

所以要在文件资源管理器中打开当前工作目录 window:

utils::browseURL(getwd())

# or any other folder
utils::browseURL("myfolder/myfolder2/")