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
})
}
)
Any ideas? Thank you!