Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting Global Environment Variables #3

Open
sebastiandig opened this issue Nov 3, 2023 · 1 comment
Open

Setting Global Environment Variables #3

sebastiandig opened this issue Nov 3, 2023 · 1 comment

Comments

@sebastiandig
Copy link
Collaborator

@7yl4r

I found the method to create environment variables using .Renviron and call it to set the variables. From what I understand, .Renvrion is used as API key protection, but this file should be ignored in the .gitignore file.

You would set up the .Renvrion by usethis::edit_r_environ("project") for the project level (or using "user" for computer level), then edit:

<UNQUOTED_KEY_VALUE> = "<quoted key>" 
# Note: this won't work if you use `<-`,
# use `=`, the only time to do this for setting a variable

After restarting R, you will have access to the environment, but to actually set it to a variable:

global_var <- Sys.getenv("<quoted <UNQUOTED_KEY_VALUE>>") # the key needs to be quoted

Calling the "key value" can be done in the .Rprofile file too because this file is executed after the .Renviron. Edit .Rprofile using usethis::edit_r_profile("project").

Here is an example I have in my personal repo: .Renviron for Global Variables

Here is some more info about the order of operations when starting R:

@sebastiandig
Copy link
Collaborator Author

My thought would be to have a function that will edit the .Renviron function when creating a new cruise ID to include the cruise ID, like <CRUISE_ID> = "<cruise id>". Then, calling the last cruise in a list from the .Rprofile where there is a vector of cruise IDs and the user would select one from the list and would be set

# vector of cruise IDs
cruise_id_select <- c("<cruise ID1>", "<cruise ID2>", "<cruise ID etc>")

# ask which cruise to set
cruise_id_select <- menu(cruise_id_select) 

# or have it use the last in the list?
cruise_id_select <- cruise_id_select[length(cruise_id_select)] 

# set cruise_id using the key value from `cruise_id_select`
cruise_id <- Sys.getenv(cruise_id_select)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant