1

I have a several names of themes for ggplot stored in a csv file and I want to use them to set themes in ggplot. However, as expected, the themes are stored with quotation marks and I need to remove them in order to use them with the function theme_set(). I have used the functions cat(), noquote() and print(, quote = FALSE) but they haven't work. For example, I have tried this:

a <- "theme_economist()"
theme_set(noquote(a))
theme_set(cat(a,"\n"))
theme_set(print(a, quote = FALSE))

But none of these have worked with ggplot.

Thanks in advance

Marlom
  • 23
  • 3
  • 2
    Why do you have code in a CSV? Your current setup has a solution that should never be used (and I see you got an answer showing that solution just now). This means, you should change your setup (e.g., store the theme-names without parentheses, so you can use `get(a)()` but even that would not be a good setup in my opinion). – Roland Aug 16 '22 at 09:14

1 Answers1

1

Using parse and eval functions seem to work.
You can use it in a loop.

### Libraries
library(ggplot2)
library(ggthemes)

### Storing theme
a <- "theme_economist()"

### Example
df <- data.frame(val=1:3, group=c('a', 'b', 'c'))
ggplot(df, aes(x=group, y=val)) + 
  eval(parse(text=a))
Yacine Hajji
  • 1,124
  • 1
  • 3
  • 20