R readRDS文件通过快捷方式

问题描述 投票:2回答:1

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

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

readRDS("...")  # with correct location
r file import shortcut
1个回答
2
投票

你可以使用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
© www.soinside.com 2019 - 2024. All rights reserved.