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)