Skip to Content

Express a string as a function in R

I am automating the creation of a series of plots each of which is based on a class of chemicals (e.g., metals, PCBs, etc.); for reasons I'll leave out, I am plotting the legend outside of the plot and using negative values for the inset argument for the legend() function to do this (e.g., inset = c(-0.2, 0)). As each of the chemical classes requires different values for the inset I thought of creating a hash table using the hash package to store the values needed for each chemical class. However, in order to store these in the hash table I was storing the vector of values as a string (e.g., "c(-0.2, 0)").

My code for the hash table looks like this:
legend.hash <- hash(chem.class, c('c(-0.2, 0)', 'c(-0.2, 0)', 'c(-0.25, -0.4)', 'c(-0.25, -0.3)', 'c(-0.2, 0)', 'c(-0.4, -0.2)', 'c(-0.2, 0)', 'c(-0.2, 0)'))
where chem.class is a vector of chemical classes.

Retrieving the values from the resulting hash table are obviously as a string "c(-0.2, 0)", is there a way of converting this string of text so that R interprets it as a function that could be used like the following: legend(..., inset = legend.hash[[chem.class[i]]])?

Or is there a better way to implement this using the traditional graphics system?