R readRDS文件通过快捷方式

时间:2018-02-02 04:12:25

标签: r file import shortcut

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

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

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

1 个答案:

答案 0 :(得分:2)

您可以使用R.Utils包,它是readWindowsShortcut功能 - > CRAN link

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

  1. 使用readWindowsShortcut().lnk文件转换为列表
  2. 使用readRDS一个从列表中提取relativePath元素
  3. 以下是一个分步说明的工作示例:

    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