How to get R to recognize your working directory as its working directory?

后端 未结 9 726
鱼传尺愫
鱼传尺愫 2020-12-25 13:27

I use R under Windows on several machines.

I know you can set the working directory from within an R script, like this

setwd(\"C:/Documents and Setti         


        
相关标签:
9条回答
  • 2020-12-25 13:35

    I put the following line in front of my scripts and it allows me to work across my computers.

    setwd(path.expand("~/path/to/working/directory/") )
    

    where ~ is = to your home directory.

    Sys.setenv(HOME = "path") or Sys.setenv(R_USER = "path") can both set the home directory.

    In my case, I work on several windows boxes, each have fairly different directory structures, but by setting the home directory properly I can sync code between computers and have them run properly on each one since where I run my R projects have similar directory structures.

    0 讨论(0)
  • 2020-12-25 13:38

    Put a shortcut for the R gui into your desired directory. Right-click and look at the shortcut properties. Delete the entry for "Start In" and click OK. When you launch the R gui from this shortcut the default directory will be the folder from which you have launched. Copy/paste this shortcut wherever you desire.

    0 讨论(0)
  • 2020-12-25 13:40

    Save your workspace to the desired directory and thereafter you just open the workspace from Windows explorer.

    0 讨论(0)
  • 2020-12-25 13:42

    You should copy shortcut to R (R.lnk file) to desire folder. Then in "Properties" (right mouse button -> last option) delete anything in field "Start in..." in second tab ("Shortcut"?). If you start R with this shortcut working directory will be that one where the shortcut is.

    I don't have english version of Windows so I'm not sure about field names, but they should be easy to find.

    Similar questions were in R-windows-faq:

    2.5 How do I run it?

    2.10 How can I keep workspaces for different projects in different directories?

    2.14 What are HOME and working directories?

    In 2.14 is mentioned that

    The working directory is the directory from which Rgui or Rterm was launched, unless a shortcut was used when it is given by the `Start in' field of the shortcut's properties.

    0 讨论(0)
  • 2020-12-25 13:42

    To set the R work directory like the current directory of the R script that I'm working, I always use a combination of the commands getwd() and setwd(), like this:

    path <- getwd() setwd(path)

    or

    setwd(getwd())

    If you want learn more about it, see this article.

    Cheers,

    []'s

    0 讨论(0)
  • 2020-12-25 13:45

    If you're using Emacs/ESS, this isn't a problem. I navigate to the directory where my R script is located, open it, then start an R ESS process. An R console pops up with the current directory as R's working directory.

    If you haven't converted to Emacs/ESS, I recommend it. (Though to prevent a flame war, I also note there are similar options for Vi users.)

    Hope that helps.

    0 讨论(0)
提交回复
热议问题