0

Here is part of my code after getting a table named full,

full <- full %>% pivot_longer(cols = -num)
    p <- ggplot(full, aes(x=num,y=value,group=name,color=name,linetype=name))+
      geom_line()
    
    if(input$po == c(1,2) | input$po == c(2,1) ){
       p <- p + 
        scale_color_manual(values = c(Policy1='red',Policy1reuse ='red', Policy2='blue',Policy2reuse ='blue'))+
        scale_linetype_manual(values = c(Policy1='solid',Policy1reuse ='dashed', Policy2='solid',Policy2reuse ='dashed'))
      }
    else if(input$po == c(1,3) | input$po == c(3,1) ){
      p <- p + 
        scale_color_manual(values = c(Policy3='green',Policy3reuse ='green', Policy1='blue',Policy1reuse ='blue'))+
        scale_linetype_manual(values = c(Policy3='solid',Policy3reuse ='dashed', Policy1='solid',Policy1reuse ='dashed'))}

I am doing a render plot here and input$po is the number user enter in a selectInput, suppose I enter 1,2 or 2,1 or 3,1, it works fine, for example if I enter 3,1: Here is the table: enter image description here

and here are the input and plot: enter image description here enter image description here But if I enter 1,3 it will come out an error: enter image description here enter image description here

I don't understand why this is happening and if I remove the scalar_color_manual and scalar_linetype_manualof the 1,2 and 2,1 condition part, the 1,3 will work out! but not in the linetype and color I want. And if I put 1,3 and 3,1 in if, and 1,2 2,1 in else if, then 1,3 will work out fine but 1,2 will run with this error, how can I fix this. Thank you for any help.

Ziqin He
  • 145
  • 4
  • 1
    `if (input$po == c(1,3))` is incorrect syntax in R, are you suppressing or just ignoring the warning messages that will produce? Do you mean to use `any` or `%in%`? Similarly, while one *can* use `|` in `if` statements, it is bad practice for several reasons, either use `||` or use `any`, `all`, or some aggregator so that `if` gets *exactly one conditional* (never 2 or more, never 1, always just 1). – r2evans Sep 10 '20 at 17:28
  • thank you, but I changed it to ```else if((input$po[1] ==1 & input$po[2] ==3) |(input$po[1] ==3 & input$po[1] ==1) )``` it still has the same problem as before do you know why.@r2evans – Ziqin He Sep 10 '20 at 18:36
  • Frankly, it's just difficult for me to get past `object 'full' not found`, so I can't just play with the code. Please make this question *reproducible*. This includes sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`). Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Sep 10 '20 at 19:04
  • (And ... *please* [edit] your question to bring it up-to-date with your changes. When there are known syntax or logic errors in the code you provide, it can be difficult to justify spending time looking for other bugs/problems.) – r2evans Sep 10 '20 at 19:06

0 Answers0