Joe
Joe

Reputation: 369

Unsupported index type: NULL --> plotly chart in shiny

I am getting an error with the plotting index using plotly in conjunction with reactive values in shiny. The sidebar panel loads with no issues but there is a problem displaying the chart that I cannot determine. Any help solving the index problem would be much appreciated. Thanks!

library(shiny)
library(plotly)

data(economics, package = "ggplot2")


nms <- names(economics) 

ui <- fluidPage(

  headerPanel("TEST"),
  sidebarPanel(
    selectInput('x', 'X', choices = nms, selected = nms[[1]]),
    selectInput('y', 'Y', choices = nms, selected = nms[[2]]),
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
                min = 100, max = 2000, value = 1000)
  ),
  mainPanel(
    plotlyOutput('trendPlot', height = "900px")
  )
)

server <- function(input, output) {

  #add reactive data information. Dataset = built in diamonds data
  dataset  <- reactive({economics[, c(input$xcol, input$ycol)]
  })

  output$trendPlot <- renderPlotly({

    # build graph with ggplot syntax
    p <- ggplot(dataset(), aes_string(x = input$x, y = input$y)) + 
      geom_line()


    ggplotly(p) %>% 
      layout(height = input$plotHeight, autosize=TRUE)

  })

}

shinyApp(ui, server)

Warning: Error in : Unsupported index type: NULL

Upvotes: 0

Views: 150

Answers (1)

amrrs
amrrs

Reputation: 6325

You have mistakenly used xcol and ycol not sure why. Without those names the code works fine.

library(shiny)
library(plotly)
library(tidyverse)

data(economics, package = "ggplot2")


nms <- names(economics) 

ui <- fluidPage(

  headerPanel("TEST"),
  sidebarPanel(
    selectInput('x', 'X', choices = nms, selected = nms[[1]]),
    selectInput('y', 'Y', choices = nms, selected = nms[[2]]),
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
                min = 100, max = 2000, value = 1000)
  ),
  mainPanel(
    plotlyOutput('trendPlot', height = "900px")
  )
)

server <- function(input, output) {

  #add reactive data information. Dataset = built in diamonds data
  dataset  <- reactive({
    economics[, c(input$x, input$y)]

  })

  output$trendPlot <- renderPlotly({


    # build graph with ggplot syntax
    p <- ggplot(dataset(), aes_string(input$x, input$y)) + 
      geom_line()


    ggplotly(p, height = input$plotHeight)

  })

}

shinyApp(ui, server)

Upvotes: 1

Related Questions