-2

I want to use a Shiny input as a name in my shiny plot.

Here is my code:

ggplot() + 
    ...
    geom_point(data=data.frame(), aes(x=x, y=y, color=paste(input$name)),
      size = 3) + 
    scale_color_manual(values=c("df1"="blue", "df2"="blue",
      paste(input$name)="red"))

It doesn't recognize paste(input$name) as a string. Here is the error message:

1512:     scale_color_manual(values=c("df1"="blue", "df2"="blue",
1513:       paste(input$name)=
                             ^

Anyone know how to properly structure this?

milkmotel
  • 402
  • 4
  • 13
  • Why would I do that? I provided both tags that are not working properly, all the other geoms are working fine. The issue is the ggplot2 functions don't recognize `paste(input$name)` as functions that call a string within the function. I'm asking if anyone has figured out how to get around that. I didn't post all 20 lines of the plot because I didn't want to have to go through and censor 20 lines containing proprietary information in my code. – milkmotel Dec 07 '16 at 22:14

1 Answers1

1

You can't intermix strings and symbols and expressions like you are doing. If you want to use a string in an aes() mapping, use aes_ or aes_string (no need for paste)

aes_string(x="x", y="y", color=input$name)

And you can't put an expression on the left of = in a named vector. Use something like setNames() instead.

values = setNames(c("blue", "blue", "red"), c("df1", "df2", input$name))

As requested above, it would be easier in the future if you include a reproducible example so that possible solutions can be properly tested. This isn't at all Shiny related. This is just how ggplot and R work.

Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295