是否可以在 R 中自动将工作目录设置为当前打开的文件夹?
示例:假设我当前在我的计算机上打开了文件夹 example_dir。
现在,我想运行一些 R 代码来将此文件夹设置为我的工作目录,而不知道打开文件夹的名称。R 代码应如下所示:
currently_opened_folder <- xxxxxxx some function extracting the path for example_dir xxxxxxxx
setwd(currently_opened_folder)
是否可以在 R 中自动将工作目录设置为当前打开的文件夹?
示例:假设我当前在我的计算机上打开了文件夹 example_dir。
现在,我想运行一些 R 代码来将此文件夹设置为我的工作目录,而不知道打开文件夹的名称。R 代码应如下所示:
currently_opened_folder <- xxxxxxx some function extracting the path for example_dir xxxxxxxx
setwd(currently_opened_folder)
多亏了这篇文章,我才知道如何从资源管理器窗口获取位置 URL 。
首先,在 PowerShell 中执行命令以检索活动资源管理器窗口的路径。然后,使用 grep 从命令返回中提取路径。最后,您需要删除“file:///”前缀并对 URL 进行解码(替换“%20”等特殊字符)。
# Get location URL of opened Explorer windows
location_url <- grep(
"file",
system('powershell -command "$a = New-Object -com "Shell.Application"; $b = $a.windows() | select-object LocationURL; $b"', intern = TRUE),
value = TRUE
)
# Check if there are multiple windows opened
if (length(location_url) > 1) {
message("Multiple Explorer windows are opened.")
} else {
# Clean paths
path <- gsub("file:///", "", URLdecode(location_url))
setwd(path)
}