I am using the plotly
package to produce two plots styled through their ggplotly
integration based on a user's inputs. the user accesses each plot by using the tabset panel choice. Unfortunately, in testing, I was not able to use the plotly
package to produce both graphs without causing my R
instance to crash.
UI with normal ggplot2
functionality
here's the data as suggested by the comments:
generationData = read.csv("data/statedata.csv", #"https://docs.google.com/spreadsheets/d/1ZbDI31sSKatBoEVKo70TV_A4VwCBHK4pIoCWXB7yfx0/pub?gid=192701245&single=true&output=csv",
header = TRUE) #read csv file
generationDataCleaned = generationData[!(is.null(generationData$Name) | generationData$Name==""), ]
statenames = as.character(generationDataCleaned$Name)
row.names(generationDataCleaned) = statenames
result()
is a reactive function to calculate the result data frame that the plot uses
result <- reactive({
state = input$stateInput
pctCoal = input$Coal / 100
if(state == "") {
#handle onload
print("it was blank!")
state = "Alabama"
pctCoal = 15 / 100
}
baseCoal_Energy = generationDataCleaned[state, "Coal.Steam.Electric.Generation..MWh."]
baseNGCC_Energy = generationDataCleaned[state, "NGCC.Electric.Generation..MWh."]
totalEnergy = sum(baseCoal_Energy,
baseNGCC_Energy
)
baseEnergy = totalEnergy
coalEnergy_Reduction = (pctCoal) * baseCoal_Energy
newCoal_Energy = (1 - pctCoal) * baseCoal_Energy
newNGCC_Energy = baseNGCC_Energy + coalEnergy_Reduction
newEnergy = newCoal_Energy + newNGCC_Energy
Energy_Frame <- c(baseEnergy, newEnergy)
#Emissions Rate
baseCoal_CO2_Rate = generationDataCleaned[state, "Coal.Steam.Emission.Rate..lb.MWh."]
baseNGCC_CO2_Rate = generationDataCleaned[state, "NGCC.Emission.Rate..lb.MWh."]
totalCO2_Rate = sum(baseCoal_CO2_Rate,
baseNGCC_CO2_Rate
)
baseCO2_Rate = totalCO2_Rate
coalCO2_Rate_Reduction = (pctCoal) * baseCoal_CO2_Rate
newCoal_CO2_Rate = (1 - pctCoal) * baseCoal_CO2_Rate
newNGCC_CO2_Rate = baseNGCC_CO2_Rate + coalEnergy_Reduction * baseNGCC_CO2_Rate / baseNGCC_Energy
newCO2_Rate = newCoal_CO2_Rate + newNGCC_CO2_Rate
CO2_Rate_Frame <- c(baseCO2_Rate, newCO2_Rate)
#Emissions Mass
baseCoal_CO2_Mass = generationDataCleaned[state, "Coal.Steam.Carbon.Dioxide.Emissions..tons."]
baseNGCC_CO2_Mass = generationDataCleaned[state, "NGCC.Carbon.Dioxide.Emissions..tons."]
totalCO2_Mass = sum(baseCoal_CO2_Mass,
baseNGCC_CO2_Mass
)
baseCO2_Mass = totalCO2_Mass
coalCO2_Mass_Reduction = (pctCoal) * baseCoal_CO2_Mass
newCoal_CO2_Mass = (1 - pctCoal) * baseCoal_CO2_Mass
newNGCC_CO2_Mass = baseNGCC_CO2_Mass + coalEnergy_Reduction * baseNGCC_CO2_Mass / baseNGCC_Energy
newCO2_Mass = newCoal_CO2_Mass + newNGCC_CO2_Mass
CO2_Mass_Frame <- c(baseCO2_Mass, newCO2_Mass)
name_Frame <- c("Base", "New")
result <- data.frame(name_Frame, Energy_Frame, CO2_Rate_Frame, CO2_Mass_Frame)
colnames(result) <- c("Name", "Energy", "Rate", "Mass")
result
})
ui.R
column(8,
tabsetPanel(type = "tabs",
id = "tabset1",
tabPanel("Rate", value = "Rate", plotlyOutput("ratePlot")),
tabPanel("Mass", value = "Mass", plotlyOutput("massPlot"))#, plotOutput("massPlot"))
)
server.R
output$ratePlot <- renderPlotly({
gg <- ggplot(result(), aes(x = Name, y = Rate, fill = Name)) +
theme_minimal() +
geom_bar(stat = "identity") +
scale_fill_brewer(type = "qual", palette = 1)
#gg
p <- ggplotly(gg)
p
})
output$massPlot <- renderPlotly({
gg2 <- ggplot(result(), aes(x = Name, y = Mass, fill = Name)) +
theme_minimal() +
geom_bar(stat = "identity") +
scale_fill_brewer(type = "qual", palette = 1)
#gg2
p2 <- ggplotly(gg2)
p2
})
But when I do just normal ggplot2, the tabPanel works fine:
ui.R
tabsetPanel(type = "tabs",
id = "tabset1",
tabPanel("Rate", value = "Rate", plotOutput("ratePlot")),
tabPanel("Mass", value = "Mass", plotOutput("massPlot"))#, plotlyOutput("massPlot"))
)
server.R
output$ratePlot <- renderPlot({ #ly
gg <- ggplot(result(), aes(x = Name, y = Rate, fill = Name)) +
theme_minimal() +
geom_bar(stat = "identity") +
scale_fill_brewer(type = "qual", palette = 1)
gg
#p <- ggplotly(gg)
#p
})
output$massPlot <- renderPlot({
gg2 <- ggplot(result(), aes(x = Name, y = Mass, fill = Name)) +
theme_minimal() +
geom_bar(stat = "identity") +
scale_fill_brewer(type = "qual", palette = 1)
gg2
})
Is there some functionality that I need to change?
For now, it seems as there is no solution to using tabbed panels with ggplot2
integration within plotly ( ggplotly()
). Worked around this issue by just using plotly()
for each graph between the tabs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.