How can I make R read my environmental variables?

前端 未结 3 1341
温柔的废话
温柔的废话 2020-12-17 08:29

I am running R on EC2 spot instances and I need R to terminate the instance and cancel the spot request once the script has run.

For that I have set the \"Request ID

相关标签:
3条回答
  • 2020-12-17 08:56

    Using Sys.getenv() you see all variables listed in the current environment.

    However, they are different from those used in your current shell, for example specified in .profile.

    To set the variables for R create a .Renviron file in your home directory and write there

    MYDIRECTORY="/home/wherever"
    

    After restarting R you will be able to access this variable with

    Sys.getenv("MYDIRECTORY")
    
    0 讨论(0)
  • 2020-12-17 09:03

    I'm pretty new to R but my approach was this: I had project-level environment variables stored in a .env file. To make it accessible in R, I used

    > readRenviron(".env")
    

    Then to access a specific variable

    > Sys.getenv("RDS_UID")
    

    And it worked perfectly.

    0 讨论(0)
  • 2020-12-17 09:07

    You want Sys.getenv() as in Sys.getenv("PATH"), say.

    Or for your example, try

    SIR <- Sys.getenv("SIR")   
    system(paste("ec2-cancel-spot-instance-requests",  SIR))
    

    As for setting variables at startup, see help(Startup) to learn about ~/.Renvironment etc

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