Shiny, plotly and DT

Hi, I'm working with Shiny and I can't display a data table using DataTable (DT) while using ggplotly (Plotly). No warning or message appears. The table is simply not displayed.

Thank you for your help.

# Define UI --------------------------------------------------------------------

library(shiny)
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout

ui <- fluidPage(
  
  sidebarLayout(
    
    # Inputs
    sidebarPanel(
      
      # Filter
      selectInput(inputId = "Species", 
                  label = "Select species",
                  choices = iris$Species, 
                  selected = "setosa"),
   
            # Show data table
      checkboxInput(inputId = "Table",
                    label = "Show table", 
                    value = TRUE) #TRUE es que debe aparecer marcado.
      
    ),
    
    # Output: Show colplot
    mainPanel(
      plotlyOutput(outputId = "graph"),
      # Show data table
      dataTableOutput(outputId = "data")
    )
  )
)

# Define server ----------------------------------------------------------------

server <- function(input, output, session) {
  
  output$graph <- renderPlotly({
    
    petal <- iris %>% 
      filter(Species==input$Species) %>% 
      ggplot(aes(x = Petal.Length,
                 text = c(paste("Species:", Species,
                                "\nPetal.Length:", Petal.Length))))
    
    a<-petal + 
      geom_histogram()
    
    ggplotly(a, tooltip = "text")
    
  })
  
  # Print data table if checked 
  output$data <- DT::renderDataTable({
    if(input$Table){
      DT::datatable(data = iris %>% 
                      select(1:5),
                    options = list(pageLength = 10),
                    rownames = FALSE)
    } 
  })
  
}

# Create a Shiny app object ----------------------------------------------------

shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents

Created on 2022-07-03 by the reprex package (v2.0.1)

You're missing the inclusion of the dplyr and DT libraries at the start of the app. Just put these in and it works.

library(dplyr)
library(DT)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.