简体   繁体   English

什么是永久设置R工作目录的万无一失的方法?

[英]What is a fool-proof way of permanently setting R working directory?

There is plenty of information on how to change the default working directory in R (every time R or RStudio is started, the working directory would change back to default, so one has to run setwd() every time). 有关如何更改R中的默认工作目录的大量信息(每次启动R或RStudio时,工作目录将更改回默认值,因此每次都必须运行setwd())。 In RStudio, there is a relevant option in Tools>Global Options>General. 在RStudio中,工具>全局选项>常规中有相关选项。 The other solutions seem to involve editing the Rprofile.site file. 其他解决方案似乎涉及编辑Rprofile.site文件。 However, all of this requires the user to be capable of finding the Rprofile and editing it, or browsing through the settings, and all the while not messing up. 但是,所有这一切都要求用户能够找到Rprofile并对其进行编辑,或浏览设置,并且一直没有弄乱。

What I need is a solution for fools students who have no idea how to do this. 我需要的是为那些不知道如何做到这一点的傻瓜学生提供解决方案。 One might say customizing the environment would be good practise, but this is a very short course, and I'd like it to be as painless as possible to the computer-illiterate souls in the audience. 有人可能会认为定制环境是一种很好的做法,但这是一个非常短暂的过程,我希望它能够尽可能轻松地吸引观众中的计算机文盲。

I have already written a script that downloads all the necessary packages for the course, loads the script in RStudio, downloads and loads a workspace with data and functions. 我已经编写了一个脚本,可以下载课程所需的所有软件包,在RStudio中加载脚本,下载并加载包含数据和功能的工作区。 They just have to run it once after installing R+RStudio. 他们只需在安装R + RStudio后运行一次。 For a moment I though this would be a good idea: 有一会儿我觉得这个好主意:

cat("setwd(\"the desired working directory\")", file=file.path(Sys.getenv("R_HOME"), "etc", "Rprofile.site"), append=T)

...but this throws Permission Denied, at least under Windows (Program files are protected I guess). ...但是这会抛出Permission Denied,至少在Windows下(程序文件受到保护我猜)。 The desired solution should be platform independent (most of them have Windows, but some might have Macs or Linux). 所需的解决方案应该是独立于平台的(大多数都有Windows,但有些可能有Mac或Linux)。 But most importantly, it should consist of just pasting the script in the console and pressing enter, nothing more complex (hence the fool-proof part of the title). 但最重要的是,它应该只是在控制台中粘贴脚本并按下回车,而不是更复杂(因此标题的傻瓜部分)。

What about something like 怎么样的

set_default_wd <- function(wd = getwd()) {
  text <- paste0(
    'local({ setwd("', wd, '") })')
  ##
  if (Sys.info()["sysname"] == "Windows") {
    write(
      text,
      file = paste0(Sys.getenv("HOME"), "\\.Rprofile"),
      append = TRUE)
  } else {
    write(
      text,
      file = paste0(Sys.getenv("HOME"), "/.Rprofile"),
      append = TRUE)
  }
}
##
#R> set_default_wd()  #set_default_wd("some/file/path")

This should work on Windows and Unix-like systems, and avoid any permissions issues. 这应该适用于Windows和类Unix系统,并避免任何权限问题。 Really the only requirement on the user's end is to specify a valid file path, which they should (hopefully) be able to work out. 实际上,用户端的唯一要求是指定一个有效的文件路径,他们应该(希望)能够解决这个问题。


It may be worthwhile to have the option of overwriting the $HOME/.Rprofile (instead of forcing lines to be appended) in case a malformed file path is given, etc... 如果给出格式错误的文件路径等,可以选择覆盖$HOME/.Rprofile (而不是强制添加行)。

set_default_wd <- function(wd = getwd(), overwrite = FALSE) {
  text <- paste0(
    'local({ setwd("', wd, '") })')
  ##
  if (Sys.info()["sysname"] == "Windows") {
    write(
      text,
      file = paste0(Sys.getenv("HOME"), "\\.Rprofile"),
      append = !overwrite)
  } else {
    write(
      text,
      file = paste0(Sys.getenv("HOME"), "/.Rprofile"),
      append = !overwrite)
  }
}

You can create a shortcut of RGui.exe on the toolbar. 您可以在工具栏上创建RGui.exe的快捷方式。

Then right-click the Icon, right-click on R, Properties, and in the tab Shortcut, you can set Start in : the folder you want. 然后右键单击“图标”,右键单击“R”,“属性”,然后在“快捷方式”选项卡中,可以设置“ Start in :所需的文件夹。

For example C:/Users/myStudentID/Documents/dev 例如C:/Users/myStudentID/Documents/dev

在此输入图像描述

The most foolproof option may be to install an Rstudio server, configure it with all the packages you want, then give each student an account on the server. 最简单的选择可能是安装Rstudio服务器,使用您想要的所有软件包进行配置,然后为每个学生提供服务器上的帐户。 That way each student starts with an identical setup and their own directory/folder. 这样每个学生都以相同的设置和他们自己的目录/文件夹开始。 Students only need internet access to use it. 学生只需要上网即可使用。 You could then provide instructions for those students who are adventurous enough to install R on their own computer (and are more likely to be able to follow instructions to set it up properly). 然后,您可以为那些冒险到足以在自己的计算机上安装R的学生提供说明(并且更有可能按照说明正确设置)。

You could also try your cat option, but put the instructions into .Rprofile in 'HOME' instead of .Rprofile.site in 'R_HOME'. 您也可以尝试使用cat选项,但将指令放入'HOME'中的.Rprofile而不是'R_HOME'中的.Rprofile.site。

Or you could put the the code in a .First function and have them save their workspace in the default location, then when they run R from the default location, the working directory would be changed by .First . 或者您可以将代码放在.First函数中并让它们将工作区保存在默认位置,然后当它们从默认位置运行R时,工作目录将由.First更改。

Or you could just leave them working at the default directory. 或者你可以让他们在默认目录下工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM