'Is there a way to call variables from another file in R without having them appear in the workspace?
I have a list of HEX colours that I want to use for my graphs/tables etc in R.
I have written a piece of code that calls these values at the start of the script.
col1 <- '#00573F'
col2 <- '#40816F'
col3 <- '#804B9F'
col4 <- '#C0D5D0'
col5 <- '#A29161'
I then call these values when plotting throughout, for example:
x <- seq(-pi, pi, 0.1)
plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)",
type="l",
col=col1)
This works perfectly.
However, I was wondering if there is a way to store these colour variables within R as a standard set of variables that I don't have to call every time I start a new script?
Also, it would be great if they didn't show up in the Environment as values purely because there are so many of these colours and I have a hard time keeping track of all the other values in there.
Solution 1:[1]
Many have adopted packages as the default way to write R code, to enable organising things like this.
You can get away with a barebone version, which I'll describe here.
You need a R/ folder; dir.create("R").
This directory should not contain scripts, but rather standalone functions, etc. that you have no problem sourcing whenever appropriate.
Inside of this you could make a custom_colors function; file.edit("R/custom_colors.R") (this will open a file in RStudio). Add:
custom_colors <- function(color_id) {
c(
col1 = '#00573F',
col2 = '#40816F',
col3 = '#804B9F',
col4 = '#C0D5D0',
col5 = '#A29161'
)[color_id]
}
Then wherever you need it, you may write source("R/custom_colors.R") to have that single function enter your environment.
Thus you may call custom_colors(1) instead of col1.
Solution 2:[2]
A handful of options to consider
Develop an internal package for your color constants
I won't so far as to write the package, but packages may contain any R object (not just functions and data). You could develop an internal package to hold your color constants. If your package is names myInternals, you can then call
x <- seq(-pi, pi, 0.1)
plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)",
type="l",
col= myInternals::col1)
If you have multiple people that need access to your constants, this is the path I would take. It's a bit more overhead work, but separates the constants into a separate environment that is relatively easy to access.
Truth be told, I have an internal package where I work now that uses @Mossa's strategy.
Use 'hidden objects'
If you precede an object with a ., it won't show up in the list of items in the environment (assuming you're using the RStudio pane)
But run the following:
.col1 <- "#00573F"
# .col1 doesn't show up
ls()
# .col1 does show up
ls(all.names = TRUE)
x <- seq(-pi, pi, 0.1)
plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)",
type="l",
col= .col1)
This is probably the easiest, in my opinion, and what I would do if no one else needed access to my constants.
Use a list
Much like @Mossa's answer, using a list will reduce the number of new objects shown in the environment to just 1.
col_list <- list(col1 = '#00573F'
col2 = '#40816F'
col3 = '#804B9F'
col4 = '#C0D5D0'
col5 = '#A29161')
x <- seq(-pi, pi, 0.1)
plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)",
type="l",
col=col_env$col1)
Use an environment
This also only adds one object to the environment, and stores the constants outside of the current environment. Using them isn't much different than using a list, however, so I'm not sure what exactly is gained.
col_env <- new.env()
assign("col1", "#00573F", col_env)
x <- seq(-pi, pi, 0.1)
plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)",
type="l",
col=col_env$col1)
Solution 3:[3]
You can add them to your .Rprofile as list or a function (as Mossa suggests), that R will run at each startup.
See this post on how to find your .Rprofile.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Benjamin |
| Solution 3 | sindri_baldur |
