R通过快捷方式读取RDS文件

R readRDS file through shortcut

我在 Windows 上创建了指向 RDS 文件的快捷方式。有没有办法 'read' 快捷方式和加载文件?

我尝试了以下命令,但失败了。

readRDS("...")  # with correct location

您可以使用 R.Utils 包和它的 readWindowsShortcut 函数 -> CRAN link

这是一个两步程序,其中:

  1. 使用 readWindowsShortcut().lnk 文件转换为列表
  2. 使用 readRDS 从列表中提取 relativePath 元素

这是一个带有分步说明的工作示例:

data <- data.frame(a = c(1,2,3,4),
                   b = c("a", "b", "c", "d"))

# Save to disk (in working directory)
saveRDS(data, file = "data.Rds")

##
# Create a windows link `.lnk` manally
##

# Load (install if necessary) library
library(R.utils)

# Read content of .lnk and store it to an object
path <- readWindowsShortcut("data.Rds - Shortcut.lnk")

# See what's inside `path` object
summary(path)
Length Class  Mode     
header           12     -none- list     
fileLocationInfo  4     -none- list     
relativePath      1     -none- character
workingDirectory  1     -none- character
relativePathname  1     -none- character
pathname          1     -none- character

# Read RDS from `relativePath`
readRDS(path$relativePath)

  a b
1 1 a
2 2 b
3 3 c
4 4 d