0

I'm hoping to implement this ggplotly bug fix offered here:

https://community.plot.ly/t/bug-with-ggplot2-stat-ecdf-function/1187/4

into a Shiny reactive expression. The top plot below shows the ggplot() call results within Shiny (as expected), the bottom is from ggplotly().

When I try to insert data <- data[order(data$val), ] inside the reactive expression, I'm unable to subset as suggested by the fix: Error in data$val : object of type 'closure' is not subsettable and I can't seem to find any other place for it to work.

reproducible app.r:

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

 # generate sample p & t observation data
 zone <- c(rep("a", 6), rep("b", 6), rep("c", 6), rep("d", 6))
 set.seed(1)
 val <- rnorm(24, 12, 18)
 param <- rep(c("p", "t"), 12)
 p_t <- data.frame(zone, val, param, stringsAsFactors = FALSE)

# sample elevation frequency data - too many obs to uncount all at once
set.seed(2)
val <- sample(50, 24)
count <- sample(200000, 24)
 e_countcsv <- data.frame(zone, val, count,  stringsAsFactors = FALSE) %>%
                mutate(param = "elev")

 shinyApp(

 ui = fluidPage(

 sidebarLayout(sidebarPanel(

  selectizeInput(
    "zone", "zone", choices = unique(p_t$zone),
    selected = c("a"),
    multiple = TRUE),

  checkboxGroupInput("param", "parameter",
                     choices = c("elev", "p", "t"), selected =c("elev", "p"))
  ),

mainPanel(

  tabsetPanel(position=c("right"),

              tabPanel(strong("static cdf"), 
                       br(),
                       plotOutput("reg_plot",  height = "750px")) ,

              tabPanel(strong("interactive cdf"), 
                       br(),
                       plotlyOutput("plotlyPlot",  height = "750px")) )))

),

server = function(input, output) {

data <- reactive({

  p_t %>%
    filter(param %in% input$param,
           zone %in% input$zone) %>%
    bind_rows({e_countcsv %>%
        filter(param %in% input$param,
               zone %in% input$zone) %>%
        uncount(count)})

})

output$reg_plot <- renderPlot({

  ggplot(data(), aes(val, color = param, linetype = zone)) +
    labs(y = "proportion of total", x = NULL) +
    stat_ecdf(pad = FALSE)  + coord_flip()
})

output$plotlyPlot <- renderPlotly({

 p <-  ggplot(data(), aes(val, color = param, linetype = zone)) +
    labs(y = "proportion of total", x = NULL) +
    stat_ecdf(pad = FALSE)  + coord_flip()
  p <- ggplotly(p)
  p

  })

}  
)

enter image description here

enter image description here

Any ideas? Thank you!

dbo
  • 1,174
  • 1
  • 11
  • 19
  • 1
    You get the error `data$val : object of type 'closure' is not subsettable` because at that time `data` points to a *function* `data` (either the default in base R or your reactive). Hence, try `sdata <- data(); sdata <- sdata[order(sdata$val),]`. – MrGumble Jul 24 '19 at 06:37
  • thank you @MrGumble, but no dice for me after trying a handful of combinations of placements. – dbo Jul 24 '19 at 08:13

1 Answers1

1

Like @MrGumble suggested you should not use data as a name because it points to a function (try to print data in your console and you will see the function).

Just give your dataset in the reactive expression another name and it will work:

data <- reactive({
  dataset <- p_t %>%
    filter(param %in% input$param,
           zone %in% input$zone) %>%
    bind_rows({e_countcsv %>%
        filter(param %in% input$param,
               zone %in% input$zone) %>%
        uncount(count)})
  dataset[order(dataset$val), ]
})
Wilmar van Ommeren
  • 7,469
  • 6
  • 34
  • 65