I have been looking for a question that deals with this but I haven't seen any.. I am creating a shiny app which uses ggplotly()
to make my graph interactive. The graph is reactive based on a user selectInput()
drop down menu. Everything works fine but when I click a new parameter in the drop down menu, it takes a long time for the plot to render. From looking into this I found this article,Improving ggplotly conversions,that explains why the plot takes a long time to render(I have a lot of data). On the website it says to use plotlyProxy()
. However, I am having a difficult time trying to implement this into my code. More specifically, I don't understand how to use the plotlyProxyInvoke()
function that you must use with it. I would greatly appreciate any guidance!
Sample data:
df<-structure(list(stdate = structure(c(17694, 14581, 14162, 14222,
17368, 16134, 17414, 13572, 17613, 15903, 14019, 12457, 15424,
13802, 12655, 14019, 16143, 17191, 13903, 12362, 12929, 13557,
16758, 13025, 15493, 16674, 15959, 15190, 16386, 11515, 12640,
15295, 15664, 15145, 17077, 14914, 14395, 14992, 13271, 12730
), class = "Date"), sttime = structure(c(35460, 42360, 32880,
30600, 26760, 45000, 36000, 32700, 39000, 35460, 34200, 28800,
26400, 33900, 39600, 29280, 34500, 28920, 31320, 34800, 37800,
42000, 34560, 27000, 35280, 37800, 36000, 32940, 30240, 42900,
28800, 35100, 35400, 39600, 30420, 41100, 34500, 32040, 37800,
36000), class = c("hms", "difftime"), units = "secs"), locid = c("BTMUA-SB1",
"BTMUA-INTAKE", "BTMUA-SA", "USGS-01394500", "BTMUA-NA", "USGS-01367785",
"NJDEP_BFBM-01411461", "BTMUA-SD", "NJDEP_BFBM-01443293", "BTMUA-SL",
"USGS-01396660", "USGS-01390400", "BTMUA-SA", "21NJDEP1-01407670",
"USGS-01477440", "BTMUA-NA", "BTMUA-SA", "BTMUA-SE", "BTMUA-SA",
"USGS-01405340", "USGS-01444990", "BTMUA-SG", "BTMUA-SB1", "USGS-01467359",
"BTMUA-SA", "USGS-01382000", "USGS-01412800", "BTMUA-NA", "BTMUA-SI",
"31DRBCSP-DRBCNJ0036", "21NJDEP1-01410230", "USGS-01465861",
"BTMUA-NF", "USGS-01445210", "BTMUA-NA", "USGS-01464020", "BTMUA-SL",
"BTMUA-SA", "USGS-01382500", "USGS-01408598"), charnam = c("Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids",
"Total dissolved solids", "Total dissolved solids", "Total dissolved solids"
), val = c(126, 84, 97, 392, 185, 157, 62, 149.4, 274, 60, 134,
516, 121, 144, 143, 99, 154, 120, 96, 99, 278, 96.2, 135, 101,
110, 460, 147, 117, 102, 250, 75, 121, 129, 242, 172, 279, 51,
205, 88, 38), valunit = c("mg/l", "mg/l", "mg/l", "mg/l", "mg/l",
"mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l",
"mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l",
"mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l",
"mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l", "mg/l",
"mg/l", "mg/l", "mg/l"), HUC14 = c("02040301030050", "02040301040020",
"02040301030050", "02030104050040", "02040301020050", "02020007020030",
"02040206130020", "02040301030050", "02040105040040", "02040301030010",
"02030105020030", "02030103140040", "02040301030050", "02030104090040",
"02040202160010", "02040301020050", "02040301030050", "02040301030040",
"02040301030050", "02030105140020", "02040105070040", "02040301030040",
"02040301030050", "02040202120010", "02040301030050", "02030103040010",
"02040206080040", "02040301020050", "02040301030030", "02040105050050",
"02040301200110", "02040202060040", "02040301020020", "02040105080020",
"02040301020050", "02040105240060", "02040301030010", "02040301030050",
"02030103050060", "02040301080050"), WMA = c("13", "13", "13",
"7", "13", "2", "17", "13", "1", "13", "8", "4", "13", "12",
"18", "13", "13", "13", "13", "9", "1", "13", "13", "18", "13",
"6", "17", "13", "13", "1", "14", "19", "13", "1", "13", "11",
"13", "13", "3", "13"), year = c(2018L, 2009L, 2008L, 2008L,
2017L, 2014L, 2017L, 2007L, 2018L, 2013L, 2008L, 2004L, 2012L,
2007L, 2004L, 2008L, 2014L, 2017L, 2008L, 2003L, 2005L, 2007L,
2015L, 2005L, 2012L, 2015L, 2013L, 2011L, 2014L, 2001L, 2004L,
2011L, 2012L, 2011L, 2016L, 2010L, 2009L, 2011L, 2006L, 2004L
)), .Names = c("stdate", "sttime", "locid", "charnam", "val",
"valunit", "HUC14", "WMA", "year"), row.names = c(NA, -40L), class = c("tbl_df",
"tbl", "data.frame"))
UI
library(shiny)
library(shinydashboard)
library(tidyverse)
library(plotly)
header<-dashboardHeader(title="test app")
sidebar<-dashboardSidebar(selectInput("huc","Please Select HUC14:",choices=df$HUC14,selected = df$HUC14))
body<- dashboardBody(plotlyOutput("plot"))
ui <- dashboardPage(header = header,
sidebar = sidebar,
body = body)
Server:
server<- function(input,output,session) {
df_reac<-reactive({
df%>%
filter(HUC14 == input$huc)
})
output$plot<-renderPlotly({
ggplot(df_reac(), aes(x = year, y = val)) +
geom_point(aes(color="Discrete"),size=3) +
geom_hline(aes(yintercept = 500,color="Freshwater Aquatic Life Criteria\nfor TDS = 500 mg/L"),size=1.3)+
xlab("Year") + ylab(" TDS Concentration (mg/L)")})
observeEvent(input$huc,{
plotlyProxy("plot",session)%>%
plotlyProxyInvoke("relayout")
})
}
shinyApp(ui,server)
The data I'm actually using is over 300,000 observations and the app is a lot more complex.. but I will use this to keep it short and sweet. I hope this is enough for a reproducible example.. if not please let me know!