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

后端 未结 9 727
鱼传尺愫
鱼传尺愫 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:47

    You could use an environmental variable. This can work with Sys.getenv() and Sys.setenv(). For instance:

    > Sys.setenv(R_TEST="testit")
    > Sys.getenv("R_TEST")
      R_TEST 
    "testit" 
    

    If you sent the variable in your script, you should be able to access it from within, and then call setwd() on that output.

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

    Just a detail: instead of reversing the slashes as you say, just add another backslash. Two of these \\ works the same way as one of these /. That makes it at least a little easier.

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

    For Ubuntu:
    Insert the following command into your .Rprofile file (usually in your home directory):

    setwd(Sys.getenv("PWD"))
    

    Now your default working directory will be whatever directory you launched R from. Keep in mind you can also set up default workspaces in different directories by saving your workspace image as .RData wherever you plan to launch R (startup sources .Rprofile before searching for .Rdata in the cwd).

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