Search code examples
rggplot2plotlyshinydashboard

"Warning: Error in : Tibble columns must have compatible sizes." when adding hoverinfo to filtered plot


Updated: Originally I was attempting to use plotly&ggplot with shinydashboard but have since dropped ggplot. I'm trying to assign hoverinfo data however, I'm now getting an error. " Warning: Error in : Tibble columns must have compatible sizes.

  • Size 0: Columns x, y, and color.
  • Size 2: Column text. i Only values of size one are recycled. 114: "

Below is my attempt.

library(shinydashboard)
library(shinyWidgets)
library(shiny)
library(DT)

#______________________________________________________________________________#
server <- function(input, output, session) { 
    df <- reactive({
        subset(iris, Petal.Width %in% input$Petalw)
    })
    
    # Extract list of Petal Lengths from selected data - to be used as a filter
    p.lengths <- reactive({
        unique(df()$Petal.Length)
    })
    
    # Filter based on Petal Length
    output$PetalL <- renderUI({
        pickerInput("PetalLengthSelector", "PetalLength", as.list(p.lengths()), options = list(`actions-box` = TRUE),multiple = T)
        
    })
    
    # Subset this data based on the values selected by user
    df_1 <- reactive({
        foo <- subset(df(), Petal.Length %in% input$PetalLengthSelector)
        return(foo)
    })
    
    output$table <- DT::renderDataTable(
        DT::datatable(df_1(), options = list(searching = FALSE,pageLength = 25))
    )
    
    
    output$correlation_plot <- renderPlotly({
        plot1 <- plot_ly(data=df_1(),
                         x = ~Petal.Length,
                         y = ~Petal.Width,
                         type = 'scatter',
                         #mode ="lines+markers",
                         color =~Petal.Length,
                         text = paste("Sepal.Length:",~Sepal.Length,"<br>",
                                      "Sepal.Width:",~Sepal.Width,"<br>",
                                      "Petal.Length:",~Petal.Length,"<br>",
                                      "Petal.Width:",~Petal.Width,"<br>",
                                      "Species:",~Species),
                         hoverinfo = 'text'
                         
        )
    })
    
}

#______________________________________________________________________________#
ui <- navbarPage(
    title = 'Select values in two columns based on two inputs respectively',
    
    fluidRow(
        column(width = 12,
               plotlyOutput('correlation_plot')
        )
    ),
    
    
    fluidRow(
        column(width = 6,
               pickerInput("Petalw","PetalWidth", choices = unique(iris$Petal.Width),selected = c("PetalWidth"), options = list(`actions-box` = TRUE),multiple = T)
        ),
        column(width = 6,
               uiOutput("PetalL")
        )
    ),
    
    fluidRow(
        column(12,
               tabPanel('Table', DT::dataTableOutput('table'))
        )
    )
)
shinyApp(ui, server)

Solution

  • The whole text= parameter needs to be a formula. The ~ doesn't just indicate a data column name, it's for unevaluated expressions. So a proper working example should look like

    output$correlation_plot <- renderPlotly({
        fig <- plot_ly(
          data = df_1(),
          x = ~Sepal.Length, 
          y = ~Sepal.Width, 
          type = 'scatter', 
          mode = 'markers',
          text = ~paste("Sepal.Length:",Sepal.Length,"<br>",
                      "Sepal.Width:",Sepal.Width,"<br>",
                      "Petal.Length:",Petal.Length,"<br>",
                      "Petal.Width:",Petal.Width,"<br>",
                      "Species:",Species),
          hoverinfo = 'text'
        ) 
    
      })
    

    There does seem to be a problem when you try to use color =~Petal.Length and you only have one point to plot. It seems this combination of events disables the hover text for some reason. This is possibly a bug.