Plot not rendering on starting an app when combined with leaflet map - r

I have a problem with this Shiny app:
Minimum working example
The shiny app that has the following structure (full code pasted below):
ui <- fluidPage(
fluidRow(
column(6, plotOutput("plot_main", click = "plot_click",height = 800)),
column(6, leafletOutput("Map_Clicked",height = 800))
),
fluidRow(...))
The "plot_main" is generated using ggplot2 and "Map_Clicked" is generated using leaflet. Everything works well except that the "plot_main" is not rendered unless I change some input to it. For example, if I even change the size of the browser window, the plot is rendered. Even if i restore it to the original size, it is again rendered and everything works well.
Also, if I display the plot without the map, it is rendered okay. It is only when I keep them in the same fluid row does it not render at start up. Has anyone else experienced this before?
Thanks.
Update (Working Example):
library(tidyverse)
library(shiny)
library(leaflet)
set.seed(5)
Ramps_Summary = read.csv(text = '"LINK_ID","FC","UNTLD1","SC","UNTLD2","UNTLD3","UNTLD4","UNTLD5","UNTLD6","UNTLD7","UNTLD8","UNTLD9","UNTLD10","SIZE","NET_ANGLE_CHANGE","MAX_ANGLE_CHANGE","SUM_ANGLE_CHANGE","All","LENGTH"
1,7,1,3,6,1,7,8,0,7,9,1,3,8,7,1,9,"All",1
2,7,1,6,8,2,5,5,6,8,8,3,3,7,8,8,1,"All",3
3,3,3,6,7,5,3,5,10,10,10,6,1,8,2,3,5,"All",4
4,7,5,2,7,10,7,2,4,1,6,2,2,7,9,4,6,"All",8
5,7,3,7,3,9,5,1,6,3,7,6,6,9,5,9,2,"All",4
6,4,1,6,6,5,4,7,9,8,1,5,6,2,3,0,6,"All",1
7,9,1,6,2,4,7,9,2,10,5,8,2,5,5,6,9,"All",10
8,8,8,6,4,7,7,9,3,4,8,7,6,8,1,2,8,"All",5
9,6,3,6,2,3,3,2,7,7,9,1,7,9,5,4,6,"All",6
10,0,7,3,0,5,4,1,3,6,6,1,1,8,7,0,6,"All",8')
Ramps_Geom = read.csv(text = '"LINK_ID","SEQ_NUM","LAT","LON","SEG_LEN_M","SEG_BEARING_F","SEG_BEARING_CHANGE_F","SEG_BEARING_T"
8,2,40.7584690905452,-73.9854012361834,5,9,6,2
8,9,40.7580278345045,-73.9845919920848,1,4,9,3
8,10,40.7592006631164,-73.9858852190994,0,7,9,1
8,0,40.7586196884458,-73.9844548667748,3,3,10,2
8,3,40.7591699132473,-73.9847476982669,9,7,8,5
8,9,40.7585382646798,-73.98427762401,8,2,5,7
8,5,40.7591325809128,-73.9859416944449,2,3,4,7
1,10,40.7592611607891,-73.9841969481268,8,7,1,2
1,3,40.7579691848779,-73.9841531244925,2,8,1,6
1,1,40.7580401211238,-73.985581492138,3,2,6,10
1,8,40.7586413385701,-73.9846689757252,10,3,6,0
1,7,40.7582596906369,-73.984743276664,8,6,6,4
1,1,40.7593930484598,-73.9844509336994,0,4,6,0
1,5,40.7592530296455,-73.9856903517036,3,7,5,7
1,7,40.7579347945071,-73.9859777377261,8,9,9,0
1,1,40.7583761536871,-73.985834911717,9,8,5,3
1,8,40.7581450762773,-73.9846474281865,2,7,9,8
1,2,40.7586051049932,-73.9856250339732,4,6,2,8
1,8,40.7589391507032,-73.9857582769048,7,7,5,1
1,2,40.7598552355292,-73.9844582986537,9,2,2,5
1,6,40.7580882019504,-73.9842737141497,5,6,10,2
1,8,40.758482203899,-73.984892300622,3,7,8,0
1,4,40.7595600512131,-73.9853101640096,0,5,5,3
1,9,40.7579476152585,-73.9854878299431,6,3,3,2
1,3,40.7597597162114,-73.9860975942206,1,10,8,1
7,3,40.7595991433715,-73.9845124059053,0,1,2,6
7,4,40.7589240646857,-73.9852887547709,8,4,7,6
7,6,40.7582982003354,-73.984795892687,10,0,9,4
7,5,40.7583415660123,-73.9853145215459,3,1,9,9
7,7,40.7582091776255,-73.9846772547321,5,0,5,7
7,0,40.759734659534,-73.9843338904697,0,5,7,0
7,1,40.7594001690942,-73.9854380581019,9,9,10,10
7,9,40.7590412120936,-73.9850591692658,4,0,2,6
7,7,40.7588734937418,-73.9856713185361,3,4,2,2
7,8,40.7586753599794,-73.9848406082056,8,3,4,8
7,5,40.7598752633991,-73.984487231959,3,5,4,3
7,6,40.7592213707949,-73.9846949898339,0,2,5,3
7,2,40.7585497230814,-73.9859657678892,5,3,8,10
7,5,40.7579495843329,-73.9846391945266,10,0,2,5
7,3,40.7590545088692,-73.985204895608,5,1,7,5
7,8,40.7590973448289,-73.9850388495628,1,4,10,7
7,8,40.7589251276777,-73.98548084496,5,0,5,8
7,4,40.7588324825094,-73.985305814383,5,8,3,10
2,5,40.7579735615324,-73.9842997173447,3,7,5,1
2,0,40.7588064078382,-73.9857434038226,3,3,7,0
2,3,40.7588438029234,-73.9853819513693,1,10,2,0
2,2,40.7589889551621,-73.9850063797225,2,3,6,6
2,1,40.7598287909185,-73.9854019674537,6,6,8,2
2,9,40.7579780119989,-73.9860675165336,4,3,7,6
2,0,40.7592434179868,-73.985385958427,1,10,8,0
2,2,40.7597305128403,-73.985350591638,1,10,7,2
2,4,40.7581336771004,-73.9855458280159,0,10,6,7
4,9,40.7594592438155,-73.9856411212472,5,9,4,10
4,2,40.7590960428371,-73.9851565797007,5,8,6,4
4,10,40.7598059624486,-73.9857454456295,9,2,4,7
4,8,40.7588052926572,-73.9843365588932,6,7,9,2
5,6,40.7596997030639,-73.9845484906225,3,4,3,9
5,8,40.7594079103298,-73.9843059319443,9,4,6,4
5,1,40.7580521274005,-73.9848191759981,5,9,9,3
5,2,40.7580591968774,-73.9853747195652,1,7,10,3
5,8,40.7592267197683,-73.9856930289356,4,6,5,9
5,1,40.7593349264903,-73.9848542028534,8,0,6,3
5,1,40.7581897167685,-73.9854442236809,1,5,6,2
10,9,40.7593763320126,-73.985722746896,3,9,1,10
10,7,40.7581679780936,-73.9855977234323,1,5,7,4
10,2,40.7582430664954,-73.9851915615535,10,5,2,8
10,5,40.758919744772,-73.9856768202422,7,9,4,7
10,0,40.7580296377396,-73.9849412224911,2,6,9,9
10,4,40.7588329302399,-73.9842071913517,3,1,10,4
10,8,40.7591101752534,-73.9842493810592,8,5,4,5
10,9,40.758090480168,-73.985085225359,9,2,6,5
10,2,40.7591293557329,-73.9843426210027,8,4,4,6
10,8,40.7582648615032,-73.9858213687925,10,7,9,8
10,10,40.7598125346728,-73.9844107882403,1,9,7,6
10,1,40.7597037591218,-73.9842972417785,6,4,9,0
10,5,40.759266425551,-73.9847140831755,4,4,2,5
10,1,40.7591004658575,-73.9852144306124,2,1,1,7
10,1,40.7597089746358,-73.9860958891319,4,10,9,8
10,9,40.7583202379645,-73.98607786299,8,7,4,3
10,3,40.7598290836421,-73.9853889658273,4,4,8,9
10,6,40.7594440670936,-73.9851675134761,5,4,4,9
10,1,40.7595995113143,-73.9853460876852,8,2,1,6
10,3,40.7591956555719,-73.9849422159539,4,4,9,3
10,2,40.759294507539,-73.9848677072943,1,6,5,7
10,9,40.7591977825459,-73.9843187623293,8,6,9,9
3,1,40.7585935046092,-73.9854441336728,7,0,9,1
3,1,40.759228982465,-73.9850963676305,3,3,8,7
3,9,40.758030982141,-73.9841336344829,6,6,9,6
3,1,40.758715084409,-73.9859045443883,3,0,2,4
3,6,40.7589047791684,-73.985315999431,3,9,4,10
3,9,40.759171358958,-73.985408751551,5,1,3,10
3,4,40.7595074527316,-73.9843717109071,7,1,1,6
3,0,40.7581955873013,-73.9841819500187,9,3,7,9
3,3,40.7594345190028,-73.9852962841391,4,2,6,1
3,1,40.7590129174642,-73.9844062979374,9,2,1,8
3,1,40.759172729659,-73.9854539673146,9,2,4,6
3,5,40.7589785798184,-73.9855022002788,8,8,8,8
3,3,40.7597266024068,-73.9849189526335,8,1,9,4
3,8,40.7592082168056,-73.9844975548823,8,5,4,10
3,6,40.7586728399397,-73.9842586159226,5,1,6,10
3,7,40.7595887803283,-73.9845113334091,10,1,9,9
3,3,40.7598410282178,-73.9843736079879,3,5,8,9
3,6,40.7595902268566,-73.985918375336,9,5,3,8
3,2,40.7588861368425,-73.9846246924609,8,7,2,4
3,1,40.757955951314,-73.9843968936139,2,8,0,2
3,1,40.7593115495635,-73.985858447561,5,4,1,9
3,7,40.7593976605177,-73.9858385586407,2,3,5,2
3,4,40.7584891686818,-73.9859703907724,4,3,7,6
3,6,40.7595548377708,-73.9845896137505,0,5,10,4
3,7,40.758835561731,-73.9848472897489,8,7,4,1
3,2,40.7588991569812,-73.984812421739,2,10,1,3
3,2,40.7588763589902,-73.9856649895324,2,9,1,2
3,5,40.7582791691107,-73.9846166529799,1,8,9,6
6,3,40.7587845130955,-73.9841068085855,6,6,2,7
6,10,40.7596552377293,-73.9854399399882,4,3,6,2
6,2,40.7588913330814,-73.985206265571,7,6,1,5
6,8,40.7586733157533,-73.9857137405886,7,1,5,8
6,8,40.7580679814802,-73.985472745411,7,7,4,8
9,4,40.7587146079518,-73.9844868738134,1,2,2,6
9,9,40.7593981206595,-73.9852633114899,8,3,4,0
9,3,40.7591942572553,-73.9854898190665,6,4,3,0
9,7,40.758148415953,-73.9855082171246,4,1,2,2
9,5,40.7579604690644,-73.9856520136319,8,2,5,3
9,6,40.7592274112507,-73.9851933038401,8,9,7,7
9,4,40.7590423501838,-73.984100669261,7,2,9,7')
ui <- fluidPage(
fluidRow(
column(6, plotOutput("plot_main", click = "plot_click",height = 800)),
column(6, leafletOutput("RampMap_Clicked",height = 800))
),
fluidRow(
column(3,
selectInput(
inputId = "SelectX",
label = "X-Axis",
choices = colnames(Ramps_Summary)[c(2,4,14,15,16,17,19)],
selected = colnames(Ramps_Summary)[c(2,4,14,15,16,17,19)][7]
)),
column(3,
selectInput(
inputId = "SelectY",
label = "Y-Axis",
choices = colnames(Ramps_Summary)[c(2,4,14,15,16,17,19)],
selected = colnames(Ramps_Summary)[c(2,4,14,15,16,17,19)][6]
)),
column(3,
selectInput(
inputId = "FacetX",
label = "X-Facet",
choices = c("None" = "All",
"FC" = "FC",
"SC" = "SC",
"NET_ANGLE_CHANGE" ="NET_ANGLE_CHANGE",
"MAX_ANGLE_CHANGE" = "MAX_ANGLE_CHANGE",
"SUM_ANGLE_CHANGE" = "SUM_ANGLE_CHANGE"),
selected = c("None" = "All",
"FC" = "FC",
"SC" = "SC",
"NET_ANGLE_CHANGE" ="NET_ANGLE_CHANGE",
"MAX_ANGLE_CHANGE" = "MAX_ANGLE_CHANGE",
"SUM_ANGLE_CHANGE" = "SUM_ANGLE_CHANGE")[1]
)),
column(3,
selectInput(
inputId = "FacetY",
label = "Y-Facet",
choices = c("None" = ".",
"FUNCTIONAL_CLASS" = "FUNCTIONAL_CLASS",
"SPEED_CATEGORY" = "SPEED_CATEGORY",
"GEO_SIZE" = "GEO_SIZE",
"NET_ANGLE_CHANGE" ="NET_ANGLE_CHANGE",
"MAX_ANGLE_CHANGE" = "MAX_ANGLE_CHANGE",
"SUM_ANGLE_CHANGE" = "SUM_ANGLE_CHANGE"),
selected = c("None" = ".",
"FUNCTIONAL_CLASS" = "FUNCTIONAL_CLASS",
"SPEED_CATEGORY" = "SPEED_CATEGORY",
"GEO_SIZE" = "GEO_SIZE",
"NET_ANGLE_CHANGE" ="NET_ANGLE_CHANGE",
"MAX_ANGLE_CHANGE" = "MAX_ANGLE_CHANGE",
"SUM_ANGLE_CHANGE" = "SUM_ANGLE_CHANGE")[1]
))
)
)
server <- function(input,output, session){
output$plot_main <- renderPlot({
plot <- ggplot(Ramps_Summary,
aes_string(input$SelectX,input$SelectY)) +
geom_point(alpha = 0.5,
size = 5) +
facet_grid(reformulate(input$FacetX,input$FacetY))
plot
}
)
output$RampMap_Clicked <- renderLeaflet({
Clicked_Point <- nearPoints(Ramps_Summary, input$plot_click, threshold = 10, maxpoints = 1)
Ramps_Geom_Clicked <- Ramps_Geom[Ramps_Geom$LINK_ID==Clicked_Point$LINK_ID,]
m <- leaflet() %>%
addTiles() %>%
addCircleMarkers(lng=Ramps_Geom_Clicked$LON,
lat=Ramps_Geom_Clicked$LAT,
label = paste('Angle Change = ',
as.character(round(Ramps_Geom_Clicked$SEG_BEARING_CHANGE_F)),
'Seq Num =',
as.character(round(Ramps_Geom_Clicked$SEQ_NUM))
)) %>%
setView(lng=mean(Ramps_Geom_Clicked$LON), lat=mean(Ramps_Geom_Clicked$LAT) , zoom=17)
m
}
)
}
shinyApp(ui = ui, server = server)

I have a working solution. I changed the sequence of output objects and it works fine now. But I do feel it is a little hacky. If someone has a better reasoning as to why the original case caused a problem, please let me know.
library(tidyverse)
library(shiny)
library(leaflet)
set.seed(5)
Ramps_Summary = read.csv(text = '"LINK_ID","FC","UNTLD1","SC","UNTLD2","UNTLD3","UNTLD4","UNTLD5","UNTLD6","UNTLD7","UNTLD8","UNTLD9","UNTLD10","SIZE","NET_ANGLE_CHANGE","MAX_ANGLE_CHANGE","SUM_ANGLE_CHANGE","All","LENGTH"
1,7,1,3,6,1,7,8,0,7,9,1,3,8,7,1,9,"All",1
2,7,1,6,8,2,5,5,6,8,8,3,3,7,8,8,1,"All",3
3,3,3,6,7,5,3,5,10,10,10,6,1,8,2,3,5,"All",4
4,7,5,2,7,10,7,2,4,1,6,2,2,7,9,4,6,"All",8
5,7,3,7,3,9,5,1,6,3,7,6,6,9,5,9,2,"All",4
6,4,1,6,6,5,4,7,9,8,1,5,6,2,3,0,6,"All",1
7,9,1,6,2,4,7,9,2,10,5,8,2,5,5,6,9,"All",10
8,8,8,6,4,7,7,9,3,4,8,7,6,8,1,2,8,"All",5
9,6,3,6,2,3,3,2,7,7,9,1,7,9,5,4,6,"All",6
10,0,7,3,0,5,4,1,3,6,6,1,1,8,7,0,6,"All",8')
Ramps_Geom = read.csv(text = '"LINK_ID","SEQ_NUM","LAT","LON","SEG_LEN_M","SEG_BEARING_F","SEG_BEARING_CHANGE_F","SEG_BEARING_T"
8,2,40.7584690905452,-73.9854012361834,5,9,6,2
8,9,40.7580278345045,-73.9845919920848,1,4,9,3
8,10,40.7592006631164,-73.9858852190994,0,7,9,1
8,0,40.7586196884458,-73.9844548667748,3,3,10,2
8,3,40.7591699132473,-73.9847476982669,9,7,8,5
8,9,40.7585382646798,-73.98427762401,8,2,5,7
8,5,40.7591325809128,-73.9859416944449,2,3,4,7
1,10,40.7592611607891,-73.9841969481268,8,7,1,2
1,3,40.7579691848779,-73.9841531244925,2,8,1,6
1,1,40.7580401211238,-73.985581492138,3,2,6,10
1,8,40.7586413385701,-73.9846689757252,10,3,6,0
1,7,40.7582596906369,-73.984743276664,8,6,6,4
1,1,40.7593930484598,-73.9844509336994,0,4,6,0
1,5,40.7592530296455,-73.9856903517036,3,7,5,7
1,7,40.7579347945071,-73.9859777377261,8,9,9,0
1,1,40.7583761536871,-73.985834911717,9,8,5,3
1,8,40.7581450762773,-73.9846474281865,2,7,9,8
1,2,40.7586051049932,-73.9856250339732,4,6,2,8
1,8,40.7589391507032,-73.9857582769048,7,7,5,1
1,2,40.7598552355292,-73.9844582986537,9,2,2,5
1,6,40.7580882019504,-73.9842737141497,5,6,10,2
1,8,40.758482203899,-73.984892300622,3,7,8,0
1,4,40.7595600512131,-73.9853101640096,0,5,5,3
1,9,40.7579476152585,-73.9854878299431,6,3,3,2
1,3,40.7597597162114,-73.9860975942206,1,10,8,1
7,3,40.7595991433715,-73.9845124059053,0,1,2,6
7,4,40.7589240646857,-73.9852887547709,8,4,7,6
7,6,40.7582982003354,-73.984795892687,10,0,9,4
7,5,40.7583415660123,-73.9853145215459,3,1,9,9
7,7,40.7582091776255,-73.9846772547321,5,0,5,7
7,0,40.759734659534,-73.9843338904697,0,5,7,0
7,1,40.7594001690942,-73.9854380581019,9,9,10,10
7,9,40.7590412120936,-73.9850591692658,4,0,2,6
7,7,40.7588734937418,-73.9856713185361,3,4,2,2
7,8,40.7586753599794,-73.9848406082056,8,3,4,8
7,5,40.7598752633991,-73.984487231959,3,5,4,3
7,6,40.7592213707949,-73.9846949898339,0,2,5,3
7,2,40.7585497230814,-73.9859657678892,5,3,8,10
7,5,40.7579495843329,-73.9846391945266,10,0,2,5
7,3,40.7590545088692,-73.985204895608,5,1,7,5
7,8,40.7590973448289,-73.9850388495628,1,4,10,7
7,8,40.7589251276777,-73.98548084496,5,0,5,8
7,4,40.7588324825094,-73.985305814383,5,8,3,10
2,5,40.7579735615324,-73.9842997173447,3,7,5,1
2,0,40.7588064078382,-73.9857434038226,3,3,7,0
2,3,40.7588438029234,-73.9853819513693,1,10,2,0
2,2,40.7589889551621,-73.9850063797225,2,3,6,6
2,1,40.7598287909185,-73.9854019674537,6,6,8,2
2,9,40.7579780119989,-73.9860675165336,4,3,7,6
2,0,40.7592434179868,-73.985385958427,1,10,8,0
2,2,40.7597305128403,-73.985350591638,1,10,7,2
2,4,40.7581336771004,-73.9855458280159,0,10,6,7
4,9,40.7594592438155,-73.9856411212472,5,9,4,10
4,2,40.7590960428371,-73.9851565797007,5,8,6,4
4,10,40.7598059624486,-73.9857454456295,9,2,4,7
4,8,40.7588052926572,-73.9843365588932,6,7,9,2
5,6,40.7596997030639,-73.9845484906225,3,4,3,9
5,8,40.7594079103298,-73.9843059319443,9,4,6,4
5,1,40.7580521274005,-73.9848191759981,5,9,9,3
5,2,40.7580591968774,-73.9853747195652,1,7,10,3
5,8,40.7592267197683,-73.9856930289356,4,6,5,9
5,1,40.7593349264903,-73.9848542028534,8,0,6,3
5,1,40.7581897167685,-73.9854442236809,1,5,6,2
10,9,40.7593763320126,-73.985722746896,3,9,1,10
10,7,40.7581679780936,-73.9855977234323,1,5,7,4
10,2,40.7582430664954,-73.9851915615535,10,5,2,8
10,5,40.758919744772,-73.9856768202422,7,9,4,7
10,0,40.7580296377396,-73.9849412224911,2,6,9,9
10,4,40.7588329302399,-73.9842071913517,3,1,10,4
10,8,40.7591101752534,-73.9842493810592,8,5,4,5
10,9,40.758090480168,-73.985085225359,9,2,6,5
10,2,40.7591293557329,-73.9843426210027,8,4,4,6
10,8,40.7582648615032,-73.9858213687925,10,7,9,8
10,10,40.7598125346728,-73.9844107882403,1,9,7,6
10,1,40.7597037591218,-73.9842972417785,6,4,9,0
10,5,40.759266425551,-73.9847140831755,4,4,2,5
10,1,40.7591004658575,-73.9852144306124,2,1,1,7
10,1,40.7597089746358,-73.9860958891319,4,10,9,8
10,9,40.7583202379645,-73.98607786299,8,7,4,3
10,3,40.7598290836421,-73.9853889658273,4,4,8,9
10,6,40.7594440670936,-73.9851675134761,5,4,4,9
10,1,40.7595995113143,-73.9853460876852,8,2,1,6
10,3,40.7591956555719,-73.9849422159539,4,4,9,3
10,2,40.759294507539,-73.9848677072943,1,6,5,7
10,9,40.7591977825459,-73.9843187623293,8,6,9,9
3,1,40.7585935046092,-73.9854441336728,7,0,9,1
3,1,40.759228982465,-73.9850963676305,3,3,8,7
3,9,40.758030982141,-73.9841336344829,6,6,9,6
3,1,40.758715084409,-73.9859045443883,3,0,2,4
3,6,40.7589047791684,-73.985315999431,3,9,4,10
3,9,40.759171358958,-73.985408751551,5,1,3,10
3,4,40.7595074527316,-73.9843717109071,7,1,1,6
3,0,40.7581955873013,-73.9841819500187,9,3,7,9
3,3,40.7594345190028,-73.9852962841391,4,2,6,1
3,1,40.7590129174642,-73.9844062979374,9,2,1,8
3,1,40.759172729659,-73.9854539673146,9,2,4,6
3,5,40.7589785798184,-73.9855022002788,8,8,8,8
3,3,40.7597266024068,-73.9849189526335,8,1,9,4
3,8,40.7592082168056,-73.9844975548823,8,5,4,10
3,6,40.7586728399397,-73.9842586159226,5,1,6,10
3,7,40.7595887803283,-73.9845113334091,10,1,9,9
3,3,40.7598410282178,-73.9843736079879,3,5,8,9
3,6,40.7595902268566,-73.985918375336,9,5,3,8
3,2,40.7588861368425,-73.9846246924609,8,7,2,4
3,1,40.757955951314,-73.9843968936139,2,8,0,2
3,1,40.7593115495635,-73.985858447561,5,4,1,9
3,7,40.7593976605177,-73.9858385586407,2,3,5,2
3,4,40.7584891686818,-73.9859703907724,4,3,7,6
3,6,40.7595548377708,-73.9845896137505,0,5,10,4
3,7,40.758835561731,-73.9848472897489,8,7,4,1
3,2,40.7588991569812,-73.984812421739,2,10,1,3
3,2,40.7588763589902,-73.9856649895324,2,9,1,2
3,5,40.7582791691107,-73.9846166529799,1,8,9,6
6,3,40.7587845130955,-73.9841068085855,6,6,2,7
6,10,40.7596552377293,-73.9854399399882,4,3,6,2
6,2,40.7588913330814,-73.985206265571,7,6,1,5
6,8,40.7586733157533,-73.9857137405886,7,1,5,8
6,8,40.7580679814802,-73.985472745411,7,7,4,8
9,4,40.7587146079518,-73.9844868738134,1,2,2,6
9,9,40.7593981206595,-73.9852633114899,8,3,4,0
9,3,40.7591942572553,-73.9854898190665,6,4,3,0
9,7,40.758148415953,-73.9855082171246,4,1,2,2
9,5,40.7579604690644,-73.9856520136319,8,2,5,3
9,6,40.7592274112507,-73.9851933038401,8,9,7,7
9,4,40.7590423501838,-73.984100669261,7,2,9,7')
ui <- fluidPage(
fluidRow(
column(6, plotOutput("plot_main", click = "plot_click",height = 800)),
column(6, leafletOutput("RampMap_Clicked",height = 800))
),
fluidRow(
column(3,
selectInput(
inputId = "SelectX",
label = "X-Axis",
choices = colnames(Ramps_Summary)[c(2,4,14,15,16,17,19)],
selected = colnames(Ramps_Summary)[c(2,4,14,15,16,17,19)][7]
)),
column(3,
selectInput(
inputId = "SelectY",
label = "Y-Axis",
choices = colnames(Ramps_Summary)[c(2,4,14,15,16,17,19)],
selected = colnames(Ramps_Summary)[c(2,4,14,15,16,17,19)][6]
)),
column(3,
selectInput(
inputId = "FacetX",
label = "X-Facet",
choices = c("None" = "All",
"FC" = "FC",
"SC" = "SC",
"NET_ANGLE_CHANGE" ="NET_ANGLE_CHANGE",
"MAX_ANGLE_CHANGE" = "MAX_ANGLE_CHANGE",
"SUM_ANGLE_CHANGE" = "SUM_ANGLE_CHANGE"),
selected = c("None" = "All",
"FC" = "FC",
"SC" = "SC",
"NET_ANGLE_CHANGE" ="NET_ANGLE_CHANGE",
"MAX_ANGLE_CHANGE" = "MAX_ANGLE_CHANGE",
"SUM_ANGLE_CHANGE" = "SUM_ANGLE_CHANGE")[1]
)),
column(3,
selectInput(
inputId = "FacetY",
label = "Y-Facet",
choices = c("None" = ".",
"FUNCTIONAL_CLASS" = "FUNCTIONAL_CLASS",
"SPEED_CATEGORY" = "SPEED_CATEGORY",
"GEO_SIZE" = "GEO_SIZE",
"NET_ANGLE_CHANGE" ="NET_ANGLE_CHANGE",
"MAX_ANGLE_CHANGE" = "MAX_ANGLE_CHANGE",
"SUM_ANGLE_CHANGE" = "SUM_ANGLE_CHANGE"),
selected = c("None" = ".",
"FUNCTIONAL_CLASS" = "FUNCTIONAL_CLASS",
"SPEED_CATEGORY" = "SPEED_CATEGORY",
"GEO_SIZE" = "GEO_SIZE",
"NET_ANGLE_CHANGE" ="NET_ANGLE_CHANGE",
"MAX_ANGLE_CHANGE" = "MAX_ANGLE_CHANGE",
"SUM_ANGLE_CHANGE" = "SUM_ANGLE_CHANGE")[1]
))
)
)
server <- function(input,output, session){
output$RampMap_Clicked <- renderLeaflet({
Clicked_Point <- nearPoints(Ramps_Summary, input$plot_click, threshold = 10, maxpoints = 1)
Ramps_Geom_Clicked <- Ramps_Geom[Ramps_Geom$LINK_ID==Clicked_Point$LINK_ID,]
m <- leaflet() %>%
addTiles() %>%
addCircleMarkers(lng=Ramps_Geom_Clicked$LON,
lat=Ramps_Geom_Clicked$LAT,
label = paste('Angle Change = ',
as.character(round(Ramps_Geom_Clicked$SEG_BEARING_CHANGE_F)),
'Seq Num =',
as.character(round(Ramps_Geom_Clicked$SEQ_NUM))
)) %>%
setView(lng=mean(Ramps_Geom_Clicked$LON), lat=mean(Ramps_Geom_Clicked$LAT) , zoom=17)
m
}
)
output$plot_main <- renderPlot({
plot <- ggplot(Ramps_Summary,
aes_string(input$SelectX,input$SelectY)) +
geom_point(alpha = 0.5,
size = 5) +
facet_grid(reformulate(input$FacetX,input$FacetY))
plot
}
)
}
shinyApp(ui = ui, server = server)

This has been solved here.
https://community.rstudio.com/t/plot-does-not-render-unless-some-input-is-changed/18839/3?u=gibran
Basically the default state didn't have a selected point to plot on the leaflet plot which caused an error which stopped from anything being displayed. Changing the order solved that without addressing the underlying problem.

Related

How do I add the option to connect observations by ID in Shiny?

I'm working on my Shiny app that visualizes/summarizes PK data. Anyways, I have a small question. I want to add in the option for the user to connect observations by ID in Shiny, so I want them to choose. This could be a single tickbox which would be: "Connect observations by ID', or just a statement like: 'Connect observations by ID:" with boxes as 'Yes' or 'No'. I hope you get what I mean. How do I do this? I have a pretty large code for my app, as I've come a long way already.
Small note, I can't generate a report yet, as the code is not right, but you can just ignore this. Tab 2 is not finished yet, but the base is there.
UI
ui <- fluidPage(
tabsetPanel(tabPanel("Tab 1",
titlePanel("Shiny App: Concentration vs Time Graphs"),
sidebarLayout(
mainPanel("Concentration vs Time graphs", plotOutput(outputId = "plot")),
sidebarPanel(style = "height:90vh; overflow-y: auto",
p("This app is developed to visualize pharmacokinetic data of different antibodies. Please select the data you want to visualize before running the graph. The graph can be reset with the reset button."),
strong("1. Filter your data for these following variables:"),
checkboxInput('checkbox1', 'Filter by study', FALSE),
conditionalPanel(condition = "input.checkbox1 == 1",
selectInput(inputId = "study", label = "Include study:",
choices = c("GLP Toxicity" = "GLPTOX", "Dose Range Finding" = "DRF", "Single Dose" = "SD", "Repeat Dose" = "RD"),
selected = c("GLPTOX", "DRF", "SD", "RD"),
multiple = T)
),
checkboxInput('checkbox2', 'Filter by platform', FALSE),
conditionalPanel(condition = "input.checkbox2 == 1",
selectInput(inputId = "platform", label = "Include platform:",
choices = c("Hexabody", 'Duobody' = "Doubody", "Bispecific"), selected = c("Hexabody", "Doubody", "Bispecific"),
multiple = T)
),
checkboxInput('checkbox3', 'Filter by species', F),
conditionalPanel(condition = "input.checkbox3 == 1",
selectInput(inputId = "species", label = "Include species:",
choices = c("Monkey", 'Mouse'), selected = c('Monkey', 'Mouse'), multiple = T)
),
checkboxInput('checkbox4', 'Filter by administration route', F),
conditionalPanel(condition = "input.checkbox4 == 1",
selectInput(inputId = "route", label = "Include administration route:",
choices = c('Route 1' = "ROUTE1", 'Route 2' = "ROUTE2"), selected = c("ROUTE1", "ROUTE2"),
multiple = T)
),
selectInput(inputId = "x", label = "2. X-axis:", choices = c("Time" = "TIME", "TLD"), selected = "Time"
),
selectInput(inputId = 'column', label = "3. Columns for:",
choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE", "Animal ID" = "ANIMALID"),
selected = "DOSEMGKG"
),
conditionalPanel(condition = "input.column == 'DOSEMGKG'",
selectInput(inputId = 'dose', label = "Choose dose(s):",
choices = c("0.05", '0.5', "20", '5'), selected = c('0.05', '0.5', '20', '5'), multiple = T
)
),
selectInput(inputId = 'row', label = "4. Rows for:",
choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE", "Animal ID" = "ANIMALID",
"Platform" = "PLATFORM", "Mutation" = "MUTATION"),
selected = "ABXID"
),
conditionalPanel(condition = "input.row == 'MUTATION'",
selectInput(inputId = 'mutation', label = "Choose mutation(s):", choices = c('M1', "M2", "M3"), selected = c('M1', "M2", "M3"), multiple = T
)
),
conditionalPanel(
condition = "input.row == 'ABXID'",
selectInput(
inputId = 'antibody',
label = "Choose antibody(s):",
choices = c('Duobody-XXXXX', "Duobody-CD3x5T4"), selected = c('Duobody-XXXXX', 'Duobody-CD3x5T4'), multiple = T
)
),
selectInput(
inputId = "group",
label = "5. Group by:",
choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE", "Animal ID" = "ANIMALID",
'Administration route' = 'ROUTE'),
selected = "ANIMALID"
),
sliderInput(
inputId = 'trange',
label = "6. Time range:",
min = 0,
max = 1704,
value = c(0, 1704 )
),
actionButton(
inputId = 'runbutton',
label = 'Run graph'
),
actionButton(
inputId = 'resetbutton',
label = 'Reset graph'
),
downloadButton(outputId = 'report', label = "Generate report"),
br(),
br(),
br(),
p("----------")
))
)),
tabsetPanel(tabPanel("Tab 2",
titlePanel("Tab 2"),
sidebarLayout(
mainPanel("Plot #2", plotOutput(outputId = "plot2")),
sidebarPanel(helpText("Whatever text..."),
selectInput(
inputId = 't',
label = "Example",
choices = c("#1", "#2", "#3"),
selected = "#1"
)
)
)))
)
Server
server <- function(input, output, session){
observeEvent(input$runbutton, {output$plot <- renderPlot({
ggplot(data = df %>% filter(STUDYID %in% input$study & ABXID %in% input$antibody & MUTATION %in% input$mutation & PLATFORM %in% input$platform
& SPECIES %in% input$species & DOSEMGKG %in% input$dose & ROUTE %in% input$route),
aes_string(x = input$x, y = "DV", col = input$group)) + xlab("Time") + ylab("Concentration (ug/mL)") +
geom_point() + facet_grid(get(input$row) ~ get(input$column)) + scale_x_continuous(limits = input$trange) +
scale_color_viridis(discrete = T, option = 'F', begin = 0, end = 0.8) + theme_bw() + scale_y_log10()})})
observeEvent(input$resetbutton, {output$plot <- renderPlot({ NULL })})
output$report <- downloadHandler(filename = "report.pdf", content = function(file){
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = T)
params <- list(n = input$x)
rmarkdown::render(tempReport, output_file = file, params = params, envir = new.env(parent = globalenv()))
})
}
shinyApp(ui = ui, server = server)
I know that it's something with geom_line(aes(group = "ANIMALID")), but I do not yet know how to make this an option to include/exclude.
Here is a simple app, that has a ggplot2 with some data, and whether the points are to be drawn connected by lines (within relevant groups) is toggleable.
I hope it helps you; your posted code is not reproducible as it uses private data, (and it is not minimal, its a lot of content to look at).
perhaps you can use this example as a base to ask further questions from as you complicate it, or account for relevant differences. but notice how my example is at least reproducible (you can run it; it is based on public, not private data).
library(shiny)
library(tidyverse)
some_data <- distinct(
iris,
Species, Petal.Width, Petal.Length
) |>
group_by(Species, Petal.Width) |>
summarise(avg_Petal.Length = mean(Petal.Length)) |>
ungroup()
ui <- fluidPage(
plotOutput("myplot", width = 400, height = 400),
checkboxInput("mytog", "line?")
)
server <- function(input, output, session) {
output$myplot <- renderPlot({
plot_to_show <-
ggplot(data = some_data) +
aes(
x = Petal.Width,
y = avg_Petal.Length,
colour = Species
) +
geom_point()
if (isTruthy(input$mytog)) {
plot_to_show <- plot_to_show + geom_line()
}
plot_to_show
})
}
shinyApp(ui, server)

How to prevent filtering after second date slider change?

Below is my observeEvent update options for a shiny app. A user can select a date range, which will then update the unique wildernesses available. The user can then select a wilderness which will update the available species in that wilderness. Then finally they can select a species and see which unique lake_ids are available to choose which will then get plotted.
My question is, how do I prevent the filtering from trickling down each option when I change the date range for the second time? So the user has chosen all their inputs. They then want to only adjust the date range keeping all the same inputs as first selected. However my script will reset all the ui options once the date is changed and the user has to walk through each selection again.
bd_data <- data.frame(id = c(10008, 10008,10008), date = c(2018,2019,2020),
species = c("ramu", "ramu", "ramu"),
wilderness = c("yosemite", "yosemite", "yosemite"),
visual_life_stage = c("adult", "adult", "adult"),
bd = c(1,2,3) )
ui <- fluidPage(
fluidRow(column(8,
h1(strong("National Park Service App - RIBBiTR ")))),
navbarPage("", inverse = T,
tabPanel("Home", icon = icon("info-circle"),
fluidPage(
fluidRow(
h1(strong("Disclaimer"), style = "font-size:20px;"),
column(12, p(""))),
fluidRow(
h1(strong("Intended Use"),style = "font-size:20px;"),
column(12, p(""))),
fluidRow(
h1(strong("Data Collection"),style = "font-size:20px;"),
column(12, p(""))))),
tabPanel(title = "Site Map", icon = icon("globe-asia"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bd_date",
label = "Select an annual range",
min = min(bd_data$date), max = max(bd_data$date),
value = c((max(bd_data$date) - 5), max(bd_data$date)),
sep = ""),
pickerInput(inputId = "wilderness_2",
label = "Select a wilderness",
choices = unique(bd_data$wilderness),
multiple = F,
selected = ""),
pickerInput(inputId = "bd_species",
label = "Select a species",
choices = unique(bd_data$species),
multiple = F,
selected = "ramu"),
pickerInput(inputId = "stage",
label = "Select a life stage",
choices = unique(bd_data$visual_life_stage),
selected = "adult",
multiple = F),
pickerInput(inputId = "bd_id",
label = "select site",
choices = unique(bd_data$id),
multiple = F)
),
mainPanel(plotOutput(outputId = "bd_plots"))
)
)
)
)
server <- function(input, output, session){
bd_reac <- reactive({
req(input$bd_date,input$wilderness_2,input$bd_species,input$stage,input$bd_id)
bd_data %>%
dplyr::filter((date <= input$bd_date[2] & date >= input$bd_date[1]),
wilderness == input$wilderness_2, species == input$bd_species,
visual_life_stage == input$stage, id == input$bd_id)
})
### I believe my error is occuring with the filtering or plotting of the data here or just above
output$bd_plots <- renderPlot({
req(bd_reac())
ggplot(data = bd_reac(), aes(x = date, y = bd)) +
geom_point() + geom_line() +
xlim(c(input$bd_date[1:2]))
})
observeEvent(input$bd_date, {
updatePickerInput(session, inputId = "wilderness_2",
choices = unique(bd_data$wilderness[bd_data$date %in% input$bd_date[1:2]]),
selected = "yosemite")
})
observeEvent(input$wilderness_2, {
updatePickerInput(session, inputId = "bd_species",
choices = unique(bd_data$species[bd_data$date %in% input$bd_date[1:2]
& bd_data$wilderness == input$wilderness_2]))
})
observeEvent(input$bd_species, {
updatePickerInput(session, inputId = "stage",
choices = unique(bd_data$visual_life_stage[bd_data$date %in% input$bd_date[1:2]
& bd_data$wilderness == input$wilderness_2
& bd_data$species == input$bd_species]))
})
observeEvent(input$stage, {
updatePickerInput(session, inputId = "bd_id",
choices = unique(bd_data$id[bd_data$date %in% input$bd_date[1:2]
& bd_data$wilderness == input$wilderness_2
& bd_data$species == input$bd_species
& bd_data$visual_life_stage == input$stage]))
})
}
shinyApp(ui, server)

Trying to create a reactive graph that will take in multiple different inputs in shiny

I have added multiple select inputs to my shiny app in the sidebar and in the main body and want to create a graph that will change when any of those inputs have been selected or changed but I keep getting the error Warning: Error in : Result must have length 56127, not 0.
UI:
ui <- dashboardPage(
dashboardHeader(title = "Human Trafficking"),
dashboardSidebar(
sidebarMenu(
selectInput("Source", "Choose a Data Source: ", choices = " ", selected = NULL,
multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
dateInput("startdate", "Start Date:", value = "2009-01-01", format = "dd-mm-yyyy",
min = "2009-01-01", max = "2019-08-26"),
dateInput("enddate", "End Date:", value = "2019-08-27", format = "dd-mm-yyyy",
min = "2009-01-02", max = "2019-08-27"),
selectInput("Nationality", "Select a nation: ", choices = " "),
actionButton("button", "Apply")
)
),
dashboardBody(
fluidRow(
box(width = 4, solidHeader = TRUE,
selectInput("traffickingType", "Choose a trafficking type: ", choices = " ", selected = NULL,
multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
),
box(width = 4, solidHeader = TRUE,
selectInput("traffickingSubType", "Choose a trafficking sub type: ", choices = " ", selected = NULL,
multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
),
box(width = 4, solidHeader = TRUE,
selectInput("gender", "Choose a gender: ", choices = " ", selected = NULL,
multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
)
),
fluidRow(
box(width = 12,
plotlyOutput('coolplot')
)
)
)
)
Server:
server <- function(input, output, session) {
genderVic = sort(unique(ngo$Victim.Gender))
updateSelectInput(session, "gender", choices = genderVic)
traffickingSub = sort(unique(ngo$Trafficking.Sub.Type))
updateSelectInput(session, "traffickingSubType", choices = traffickingSub)
trafficking = sort(unique(ngo$Trafficking.Type))
updateSelectInput(session, "traffickingType", choices = trafficking)
traffickerNationalities = sort(unique(ngo$Trafficker.Nationality))
updateSelectInput(session, "TraffickerNation", choices = traffickerNationalities)
dataSource = sort(unique(ngo$Data.Provided.By))
updateSelectInput(session, "Source", choices = dataSource)
nationalities = sort(unique(ngo$Victim.Nationality))
updateSelectInput(session, "Nationality", choices = nationalities)
output$coolplot <- renderPlotly({
ngo <-
ngo %>%
filter(Victim.Nationality == input$Nationality,
Victim.Gender == input$gender,
Trafficking.Type == input$traffickingType
)
p = ggplot(ngo, aes(x = Victim.Age, fill = Trafficking.Type)) +
geom_bar(position = "stack")
ggplotly(p) %>%
layout(showlegend = FALSE)
})
}
So currently only have it calling three of the inputs to test it but still getting an error.
It should work after the error is displayed once you select a gender and trafficking type in your example. The reason for the error is that renderPlotly is expecting values for input$traffickingType and input$gender but these start out as NULL.
Add a req for each of those selectInputs:
output$coolplot <- renderPlotly({
ngo <-
ngo %>%
filter(Victim.Nationality == input$Nationality,
Victim.Gender == req(input$gender),
Trafficking.Type == req(input$traffickingType)
)
p = ggplot(ngo, aes(x = Victim.Age, fill = Trafficking.Type)) +
geom_bar(position = "stack")
ggplotly(p) %>%
layout(showlegend = FALSE)
})

ggplot2 isn't recognizing shiny's reactive data

everyone!
I'm trying hard here to get the data I grouped by the 'fc' function and it was supposed to be in this new dataset 'reac', where I wanted it to be updated when a user input which variable he/she wants to use at shiny's UI (input$x, input$y, input$color). Then I want to use this new reac dataset in ggplot, but the 'aes_string() isn't working, the compiler doesn't recognize the data type as a dataframe and I'm getting a "Can't convert NULL to a quosure" error and "Unknown input:tbl_df" error.
Sorry for the bad english, it isn't my mothertongue! :(
Thanks in advance!
PS: Link for the code
library(shiny)
library(plotly)
library(rsconnect)
library(sidrar)
library(dplyr)
library(ggplot2)
#dados261<-get_sidra(api="/t/261/n2/all/n3/all/v/allxp/p/last%2011/c1/allxt/c2/allxt/c58/1140,1141,1142,1143,1144,1145,1146,1147,1148,1149,1150,1151,1152,1153,2792,2793,3244,3245/d/v93%203")
load("C:/Users/Fausto/Desktop/dados_1.RData")
colnames(dados261)<-c("nt_cod","nt","regiao_cod","regiao","va_cod","va","ano_cod","ano","dom_cod","dom","sexo_cod","sexo","id_cod","id","um_cod","um","valor")
names1 <- c("nt_cod","regiao_cod","va_cod","ano_cod","dom_cod","sexo_cod","id_cod","um_cod")
dados261[names1] <- sapply(dados261[names1],as.numeric)
years<-as.numeric(sort(unique(dados261$ano)))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
h1("Tabela 261 - SIDRA - Dados Gerais"),
selectInput("x", label = "Eixo x", choices = list("Região" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicílio" = "dom", "Valores" = "valor"), selected = "regiao"),
selectInput("y", label = "Eixo Y", choices = list("Região" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicílio" = "dom", "Valores" = "valor"), selected = "valor")
),
mainPanel(
tabsetPanel(
tabPanel("Gráfico de barras",
plotOutput("plot", width = "80%", height = "80%"),
radioButtons("color", label = "Preenchimento", choices = list("Região" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicílio" = "dom", "Nenhuma" = "id"), selected = "nn", inline = TRUE)),
tabPanel("Série Temporal",
plotOutput("plot2", width = "100%", height = "100%"),
sliderInput("ano", label = "Anos", min = min(years), max= max(years), value=c(min(years),max(years)))
)
)
)
)
)
server <- function(input, output){
fc<- function(data, ...) {
data %>% group_by_(...) %>%
summarise(valor2 = sum(valor, na.rm = TRUE)) -> data
return(data)
}
reac<-reactive({
fc(dados261, input$x, input$y, input$color)
})
output$plot <- renderPlot({
eixox<-as.character(reac()[,1])
eixoy<-as.numeric(reac()$valor2)
eixoz<-as.character(reac()[,2])
p<- reac() %>%
ggplot() +
aes_q(eixox, eixoy, fill= eixoz) +
geom_bar(stat = "identity")
ggplotly(p)
}, height = 600, width = 900)
}
shinyApp(ui = ui, server = server)
When I loaded your .Rdata, and ran the code with changes suggested by Alejandro, my graphs changed.
loaded the libraries excluding sidrar and rsconnect, and ran the code:
library(shiny)
library(plotly)
library(dplyr)
library(ggplot2)
load("/Users/username/Downloads/RData.RData")
#Renomeando as colunas
colnames(dados261)<-c("nt_cod","nt","regiao_cod","regiao","va_cod","va","ano_cod","ano","dom_cod","dom","sexo_cod","sexo","id_cod","id","um_cod","um","valor")
#transformando as variáveis que estao como "char" em "integer"
names1 <- c("nt_cod","regiao_cod","va_cod","ano_cod","dom_cod","sexo_cod","id_cod","um_cod")
dados261[names1] <- sapply(dados261[names1],as.numeric)
years<-as.numeric(sort(unique(dados261$ano)))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
h1("Tabela 261 - SIDRA - Dados Gerais"),
selectInput("x", label = "Eixo x", choices = list("Regiao" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicilio" = "dom", "Valores" = "valor"), selected = "regiao"),
selectInput("y", label = "Eixo Y", choices = list("Regiao" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicilio" = "dom", "Valores" = "valor"), selected = "valor")
),
mainPanel(
tabsetPanel(
tabPanel("Grafico de barras",
plotOutput("plot", width = "80%", height = "80%"),
radioButtons("color", label = "Preenchimento", choices = list("Regiao" = "regiao", "Idade" = "id", "Sexo" = "sexo", "Domicilio" = "dom"), selected = "id", inline = TRUE)),
tabPanel("Serie Temporal",
plotOutput("plot2", width = "100%", height = "100%"),
sliderInput("ano", label = "Anos", min = min(years), max= max(years), value=c(min(years),max(years)))
)
)
)
)
)
server <- function(input, output){
fc<- function(data, ...) {
data %>% group_by_(...) %>%
summarise(valor2 = sum(valor, na.rm = TRUE)) -> data
return(data)
}
reac<-reactive({
fc(dados261, input$x, input$y, input$color)
})
output$plot <- renderPlot({
eixox<-as.character(reac()[,1])
eixoy<-as.numeric(reac()$valor2)
eixoz<-as.character(reac()[,3])
reac() %>%
ggplot() +
aes_string(eixox, eixoy, fill= eixoz) +
geom_bar(stat = "identity")
}, height = 600, width = 900)
}
shinyApp(ui,server)

Shiny radio button not getting rendered initially when the app starts

I am doing some timeseries analysis and have created a shiny app where when the app starts sample timeseries data is uploaded or the user can upload csv dataset from his local directory....
Sample Dataset:
df
month passengers
1 01-01-2000 2072798
2 01-02-2000 2118150
3 01-03-2000 2384907
4 01-04-2000 2260620
5 01-05-2000 2386165
6 01-06-2000 2635018
7 01-07-2000 2788843
8 01-08-2000 2942082
9 01-09-2000 2477000
10 01-10-2000 2527969
11 01-11-2000 2161170
12 01-12-2000 2175314
13 01-01-2001 2307525
14 01-02-2001 2196415
15 01-03-2001 2545863
library(signal)
library(shiny)
library(AnomalyDetection) #devtools::install_github("twitter/AnomalyDetection")
library(ggplot2)
# Define UI for application that draws a histogram
library(shinydashboard)
library(shinycssloaders)
library(googleVis)
shinyUI(dashboardPage(skin = "green",
dashboardHeader(title = "Anomaly Detection in Time series",
titleWidth = 350),
dashboardSidebar(
sidebarUserPanel("Nishant Upadhyay",
image = "nishantcofyshop.jpg"
),
sidebarMenu(
menuItem("Data", tabName = "data", icon = icon("database")),
menuItem("Filters", tabName = "filter", icon = icon("filter")),
menuItem("Anomalies", tabName = "anomaly", icon = icon("check")),
#menuItem("Save Data", tabName = "save", icon = icon("save"))
menuItem("About The App", tabName = "Help", icon = icon("info-circle"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "data",
fluidRow(
box(
title = "Data scatter Chart",
status = "primary",
solidHeader = T,
collapsible = T,
width = 12,
shinycssloaders::withSpinner(htmlOutput("dataChart"),type = getOption("spinner.type", default = 8),color = "red")
)
),
fluidRow(
box(
radioButtons(
"data_input","",
choices = list("Load sample data" = 1,
"Upload csv file" = 2
)
),
conditionalPanel(
condition = "input.data_input=='1'",
h5("Sample dataset of Lebron James basketball shots over the years")
),
conditionalPanel(
condition = "input.data_input=='2'",
fileInput('file1', 'Choose file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),','),
radioButtons('quote', 'Quote',
c('None'='',
'Double Quote'='"',
'Single Quote'="'"),
'')
),
title = "Select Dataset",
status = "info",
solidHeader = T,
collapsible = T
),
box(
title = "Data",
status = "info",
solidHeader = T,
collapsible = T,
shinycssloaders::withSpinner(htmlOutput('contents'),type = getOption("spinner.type", default = 8),color = "red")
)# end of box
)## end of Fluid row
), ## end of tab item
tabItem(
tabName = "filter",
fluidRow(
box(
title = "Data Chart",
status = "primary",
solidHeader = T,
collapsible = T,
width = 12,
shinycssloaders::withSpinner(htmlOutput('dataChartFiltered'),type = getOption("spinner.type", default = 8),color = "red")
)
),
fluidRow(
box(
title = "Filters",
status = "info",
solidHeader = T,
collapsible = T,
width = 4,
radioButtons("filt", NULL,
c("None" = "none",
"Butterworth" = "butt",
"Type-II Chebyshev" = "cheby2")),
submitButton("Filter")
),
box(
title = "Butterworth",
status = "info",
solidHeader = T,
collapsible = T,
width = 4,
textInput("buttern", label = "Filter Order", value = "3"),
textInput("butterf", label = "Critical Frequencies", value = "0.1"),
radioButtons("buttert", "Type",
c("Low-Pass" = "low",
"High-Pass" = "high"))
),
box(
title = "Chebyshev",
status = "info",
solidHeader = T,
collapsible = T,
width = 4,
textInput("chebyn", label = "Filter Order", value = "5"),
textInput("chebyd", label = "dB of Pass Band", value = "20"),
textInput("chebyf", label = "Critical Frequencies", value = "0.2"),
radioButtons("chebyt", "Type",
c("Low-Pass" = "low",
"High-Pass" = "high"))
)
)
)
) ## end of tab items
) ## end of Dashboard
)
)
shinyServer(function(input, output){
dataframe<-reactive({
if (input$data_input == 1) {
tab <- read.csv("df.csv",header = T,stringsAsFactors = F)
} else if (input$data_input == 2) {
inFile <- input$file1
if (is.null(inFile))
return(data.frame(x = "Select your datafile"))
tab = read.csv(inFile$datapath, header = input$header,
sep = input$sep, quote = input$quote)
}
tt <- tryCatch(as.POSIXct(tab[,1]),error=function(e) e, warning=function(w) w)
if (is(tt,"warning") | is(tt,"error")) {
tab$Old = tab[,1]
tab[,1] = as.POSIXct(1:nrow(tab), origin = Sys.time())
} else {
tab[,1] = as.POSIXct(tab[,1])
}
tab
})
output$dataChart <- renderGvis({
if (!is.null(dataframe()))
gvisLineChart(dataframe()[,c(1,2)], xvar = colnames(dataframe())[1], yvar = colnames(dataframe())[2],
options = list(
crosshair.trigger = 'selection',
enableInteractivity = TRUE,
hAxis.maxTextLines = 10,
tooltip.trigger = 'none'
))
})
output$contents <- renderGvis({
if (!is.null(dataframe()))
gvisTable(dataframe(),
options = list(page='enable'))
})
output$dataChartFiltered <- renderGvis({
if (input$filt == "none") {
return(NULL)
} else if (input$filt == "butt") {
bf <- butter(as.numeric(input$buttern), as.numeric(input$butterf), type = input$buttert)
filtered = data.frame(timestamp = dataframe()[,1],
count = as.numeric(filter(bf, dataframe()[,2])))
gvisLineChart(filtered, xvar = colnames(filtered)[1], yvar = colnames(filtered)[2],
options = list(
crosshair.trigger = 'selection',
enableInteractivity = TRUE,
hAxis.maxTextLines = 10,
tooltip.trigger = 'none'
))
} else if (input$filt == "cheby2") {
ch <- cheby2(as.numeric(input$chebyn), as.numeric(input$chebyd),
as.numeric(input$chebyf), type = input$chebyt)
filtered = data.frame(timestamp = dataframe()[,1],
count = as.numeric(filter(ch, dataframe()[,2])))
gvisLineChart(filtered, xvar = colnames(filtered)[1], yvar = colnames(filtered)[2],
options = list(
crosshair.trigger = 'selection',
enableInteractivity = TRUE,
hAxis.maxTextLines = 10,
tooltip.trigger = 'none'
))
}
})
})
The problem i am facing is that once the shiny app is executed , the sample data is loaded properly as the this data is placed in the app folder in the directory (one can use R inbuilt data set or use the data i gave in the start) and subsequently all steps gets executed properly.
But if i want to upload some other csv file from local directory, the upload button selection does not get activated even after selecting it.But,in fact, if one goes to the second menu item in the sidebar panel i.e. filter tab and clicks on the filter button (under Filters box ) and then if i go back to Data menu in the sidebar panel again, i can see that now my upload csv file button has got activated and now i can browse the csv file in local directory and upload the same into the app and now everything works fine.
It seems somewhere the condition that makes the upload file button is not getting active initially when the app opens....
Need help to sort out the issue...Sorry for posting large chunk of code....
conditionalPanel and submitButton do not work well together. Replace your submitButton("Filter") with actionButton("Filter", "").
EDIT:
As per the comment, for the plot to be generated only after the actionButton is clicked you can put output$dataChartFiltered inside observeEvent of Filter with isolate for `input objects as follows:
observeEvent(input$Filter,{
output$dataChartFiltered <- renderGvis({
if (isolate(input$filt) == "none") {
return(NULL)
} else if (isolate(input$filt) == "butt") {
bf <- butter(as.numeric(isolate(input$buttern)), as.numeric(isolate(input$butterf)), type = isolate(input$buttert))
filtered = data.frame(timestamp = dataframe()[,1],
count = as.numeric(filter(bf, dataframe()[,2])))
gvisLineChart(filtered, xvar = colnames(filtered)[1], yvar = colnames(filtered)[2],
options = list(
crosshair.trigger = 'selection',
enableInteractivity = TRUE,
hAxis.maxTextLines = 10,
tooltip.trigger = 'none'
))
} else if (isolate(input$filt) == "cheby2") {
ch <- cheby2(as.numeric(isolate(input$chebyn)), as.numeric(isolate(input$chebyd)),
as.numeric(isolate(input$chebyf)), type = isolate(input$chebyt))
filtered = data.frame(timestamp = dataframe()[,1],
count = as.numeric(filter(ch, dataframe()[,2])))
gvisLineChart(filtered, xvar = colnames(filtered)[1], yvar = colnames(filtered)[2],
options = list(
crosshair.trigger = 'selection',
enableInteractivity = TRUE,
hAxis.maxTextLines = 10,
tooltip.trigger = 'none'
))
}
})
})

Resources