在一个闪亮的app中的tabsetPanel中使用2个单独的plotly(或ggplotly)图

时间:2015-12-04 18:22:27

标签: r ggplot2 shiny plotly

我正在使用plotly包根据用户的输入生成两个通过ggplotly集成设计的图表。用户使用tabset面板选项访问每个​​绘图。不幸的是,在测试中,我无法使用plotly包来生成两个图,而不会导致R实例崩溃。

UI with normal ggplot2 functionality

这是评论建议的数据:

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()是一个反应函数,用于计算绘图使用的结果数据框

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
})

但是当我做普通的ggplot2时,tabPanel工作正常:

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
})

是否需要更改某些功能?

1 个答案:

答案 0 :(得分:0)

目前,似乎没有解决方案在plotly(ggplot2)中使用带有ggplotly()集成的选项卡式面板。通过在标签页之间为每个图表使用plotly()来解决此问题。