R 在闪亮应用程序的tabsetPanel中使用两个单独的plotly(或ggplotly)绘图

R 在闪亮应用程序的tabsetPanel中使用两个单独的plotly(或ggplotly)绘图,r,ggplot2,shiny,plotly,R,Ggplot2,Shiny,Plotly,我正在使用plotly包根据用户的输入,通过ggplotly集成生成两个绘图样式。用户通过使用选项卡集面板选项访问每个绘图。不幸的是,在测试中,我无法使用plotly包生成两个图形,而不会导致我的R实例崩溃 以下是评论建议的数据: generationData = read.csv("data/statedata.csv", #"https://docs.google.com/spreadsheets/d/1ZbDI31sSKatBoEVKo70TV_A4VwCBHK4pIoCWXB7yfx0

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

以下是评论建议的数据:

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

output$ratePlot目前,似乎没有解决方案可以将选项卡式面板与plotly(
ggplotly()
)中的
ggplot2
集成。通过对选项卡之间的每个图形使用
plotly()
解决了此问题。

在第一次输出中,$massPlot p2未打印操作,抱歉,我忘了将其放在这一代码行上,我将进行编辑以反映该更改。我还没有将ggplotly与SHINK一起使用,但您不应该拥有用于plotly的usename或密码吗?@MLavoie Think changed now opensource@Alex有一些数据会有帮助。您是否尝试过只输出plotlyOutput(“ratePlot”)plotlyOutput(“massPlot”),而不输出任何选项卡集panel/panel/column。可以帮助确定问题区域