Hello! I'm a beginner in R and Shiny. I have to create an interactive graph, which gives the opportunity to select a "ZONE" and returns back the corresponding graph.
I have created a function that does this job. By using as an argument the ZONE you are interested in, it creates the corresponding graph.
To make it interactive, I'm trying to build a shiny app. I have put inside the server the function and inside UI the names of the ZONEs as inputs.
The problem is that the app doesn't display any plot. I think it should be due to the fact that shiny app is not uploading the dataframe, which should be stored in R memory because of the previous chunks.
Below my code. I attach both the function and the shiny app code.
Thanks in advance.
Function (well functioning alone)
demand_supply = function(zone = "ZONENAME"){
zone_sup = dataframe %>% filter(ZONE_CD == zone ) %>% filter(PURPOSE_CD == "OFF") %>% filter(ENERGY_PRICE_NO != "0.00") %>% select(QUANTITY_NO, ENERGY_PRICE_NO)
zone_sup$QUANTITY_NO = as.numeric(zone_sup$QUANTITY_NO)
zone_sup$ENERGY_PRICE_NO = as.numeric(zone_sup$ENERGY_PRICE_NO)
plot_sup = zone_sup %>%
ggplot(aes(QUANTITY_NO,ENERGY_PRICE_NO))+
geom_point()+
geom_smooth(method = lm)+
labs(x = "Quantity", y = "Price")+
theme_minimal()
zone_dem = dataframe %>% filter(ZONE_CD == zone) %>% filter(PURPOSE_CD == "BID") %>%
select(QUANTITY_NO, ENERGY_PRICE_NO)%>% filter(ENERGY_PRICE_NO != "0.00")
zone_dem$QUANTITY_NO = as.numeric(zone_dem$QUANTITY_NO)
zone_dem$ENERGY_PRICE_NO = as.numeric(zone_dem$ENERGY_PRICE_NO)
#zone_dem = zone_dem %>% filter(QUANTITY_NO < 110) #I remove some outliers
plot_dem = zone_dem %>%
ggplot(aes(QUANTITY_NO,ENERGY_PRICE_NO))+
geom_point()+
geom_smooth(method = lm)+
labs(x = "Quantity", y = "Price")+
theme_minimal()
return(ggarrange(plot_sup, plot_dem, labels = c("Supply", "Demand"), font.label= list(size = 10), hjust = -1))
}
Shiny app (just displaying the SideBar):
ui = function(request){
pageWithSidebar(
headerPanel("Interactive Supply and Demand"),
sidebarPanel(
selectInput("ZONE", "Please Select the Zone",
choices = c("NORD", "CNOR", "CSUD", "SUD", "CALA", "SICI", "SARD"))
),
mainPanel(
plotOutput("myPlot")
)
)
}
server = function(input, output, session){
renderPlot(demand_supply(zone = input$ZONE))
}
shinyApp(ui = ui, server = server)