0

I have issue with checkboxGroupInput() in shiny dashboread

I want the scatter plot to be plotting based on the gender selection, meaning when I put a check on the male, only the male part is drawn, so on for the female part, and when check all then will display all dataset

This error appears when selecting female (ERROR: Aesthetics must be either length 1 or the same as the data (3889): x, y and colour)

Also, i have selectInput() to select x axis and y axis and radioButtons() to select coorelation methods but it all works without issue

i try the following code but it does not work

#UI
tabItem(tabName = "corr1",
              fluidRow(
                column(8,h2("Scatter plot correlation")),
                box(selectInput("var1","Select one variable for X axis",choices = names(dfs)) ),
                box(selectInput("var2","Select one variable for Y axis",choices = names(dfs)) )),
              
              fluidRow(
                
                box(background="light-blue",
                    width = 8, girafeOutput("plot2",width = "100%", height = "500px")),
                column(2, h3("Gender") , checkboxGroupInput('g', label = p("Select the gender to plot"),
                                                            choices =
                                                              unique(dfs$Gender),
                                                            selected = list("Male","Female"))),
                
                column(2, h3("Correlation methods"), radioButtons('c', label = p("Select one method"),
                                                                 choiceNames =
                                                                   list("pearson" , "kendall" ,"spearman"),
                                                                 choiceValues =
                                                                   list("pearson" , "kendall" ,"spearman"),
                                                                 selected = "pearson"))
                
         
                           
                       )
#SERVER
   plot<-reactive({
  
      filter(dfs, Gender%in%input$g)
  
  
  
   })
  
 
  myplot2 <- reactive({
    
    x1=as.numeric(dfs[[input$var1]])
    y1=as.numeric(dfs[[input$var2]])
    if(input$c=="pearson"){ c= cor.test(x1, y1, na.omit=TRUE,method="pearson")}
    if(input$c=="spearman"){ c= cor.test(x1, y1, na.omit=TRUE,method="spearman")}
    if(input$c=="kendall"){ c= cor.test(x1, y1, na.omit=TRUE,method="kendall")}
    
    # c= cor.test(x1, y1, na.omit=TRUE)
    a=round(c$p.value,3)
    b=round(c$estimate,3)
    x_str = paste0("P-value = " ,a ,"\n", input$c," correlation coefficient = ",b)
    
    
    scatter=ggplot(plot(), aes(x=x1, y=y1, color=dfs$Gender)) +
      geom_point_interactive(size=2.5,tooltip=dfs$p_ID,data_id=dfs$p_ID,position=position_jitter(h=5,w=2)) +   #jitter solve the overlapping issue
      theme_ipsum()+
      stat_smooth(method = "lm",
                  col = "#C42126",
                  se = FALSE,
                  size = 1)+
      xlab(input$var1)+
      ylab(input$var2)
       
    
  })
  
  
  
  ##############
  output$plot2 <- renderGirafe({
    
   p2<- girafe(ggobj = myplot2(),width_svg = 14, height_svg = 8,
                options = list(
                  opts_sizing(rescale = FALSE) ,opts_toolbar(saveaspng = FALSE),opts_tooltip(use_fill = TRUE)))
    p2
   
  })
Joman
  • 21
  • 5
  • You don't return anything from `myplot2`. Add `return(scatter)` and see if that improves the situation. – Limey Nov 14 '22 at 07:27
  • @Limey do you mean i have to add return(scatter) at the end of myplot2 function? – Joman Nov 14 '22 at 07:32
  • The issue is most likely that you use vectors from different dfs and with different lengths for your plot, i.e. the error message is telling you that your vectors `x1`, `y1` and `dfig$Gender` have different length than the number of rows of the dataframe `plot()`. That's one of the reason why as a general rule mapping vectors on aesthetics or arguments should be avoided. If you need more help then provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Nov 14 '22 at 07:44
  • @stefan thank you, i modified the code and I used only dfs for all inputs but still, I have the same issue! – Joman Nov 14 '22 at 10:49
  • `plot()` is a filtered version of your `dfs`. Hence you run in the same issue. As I said using something like `color=dfs$Gender` should avoided. Why not doing `color=Gender` and instead of passing a vector to x and y use the `.data` pronoun, e.g. `x=.data[[input$var1]]` inside `aes()`. – stefan Nov 14 '22 at 11:13
  • thanks a lot, @stefan the code worked but there is an issue in the tooltip when I delete tooltip=dfs$p_ID,data_id=dfs$p_ID the code works without an issue so how to solve it. this is the error that appears ERROR [object Object] – Joman Nov 15 '22 at 08:19

0 Answers0