【问题标题】:Difference between tilde expansion in RStudio and RRStudio和R中波浪号扩展之间的区别
【发布时间】:2015-11-25 14:05:15
【问题描述】:

在 Windows path.expand("~") 上(我安装的)RStudio 返回“C:/Users/myusername/Documents”。但是,命令行中的 RScript -e path.expand('~') 返回“C:\Users\myusername”(与 R REPL 相同)。这使得使用代字号并在一种环境中工作的脚本在另一种环境中失败。一个可能的解决方法是在从命令行运行脚本之前执行set R_USER=C:\Users\myusername\Documents,但这似乎是一个杂项;除非我警告他们设置R_USER,否则它也可能会绊倒我脚本的其他用户。我还尝试向~/.Renviron 添加一个条目,但这似乎导致 RStudio 中的“源”按钮失败。

让 RStudio 和 R 就如何扩展波浪号达成一致的最佳方法是什么?

【问题讨论】:

  • 会不会是 RStudio 使用了不同版本的 R?
  • @krlmlr 好点。但是看起来两者都使用相同版本的 R。sessionInfo() 在这两种情况下都会产生 R version 3.2.1 (2015-06-18) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1
  • Rscript 和RStudio 中查看Sys.getenv("R_USER")。在.Renviron 中明确设置
  • 我想说,不要依赖扩展,永远不要。使用固定路径(可能会询问用户)或环境变量(但这可能会根据程序的调用方式而改变)
  • @Tensibai 我认为不依赖波浪号扩展是有道理的。我会用一个充实的版本来回答我的问题。

标签: r windows rstudio


【解决方案1】:

正如@Tensibai 所建议的,不依赖波浪号扩展可能是最好的解决方案。相反,我使用以下功能:

Home <- function() {
  # Returns a string with the user's home directory
  #
  # Serves as a replacement for "~", and works both in RStudio and RScript.
  #
  # Returns:
  #   On Windows returns C:/Users/<username>, where <username> is the current user's username.

  normalizePath(file.path(Sys.getenv("HOMEDRIVE"), Sys.getenv("HOMEPATH")), winslash = .Platform$file.sep)
}

将其扩展为跨平台工作应该很简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-20
    • 2011-06-06
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多