首页 > 解决方案 > 我无法让我的 Shiny (in R) 应用程序散点图在窗口中呈现

问题描述

不断出现的错误是Error in output$barchart <- renderPlotly({ : object 'output' not found

我不太确定这里出了什么问题。我对这个项目有点不知所措。

我正在使用 dplyr 中预加载的 Midwest 数据框做这个项目

我愿意接受任何人对此进行的任何编辑。Aghghhhgghgh helpppp 哈哈

到目前为止,这是我的两个文件(server.R 和 ui.R)

服务器:

library(dplyr)
library(tidyr)
library(stringr)
library(lintr)
library(shiny)
library(ggplot2)
library(knitr)
library(tidyverse)
library(operators)
library(magrittr)

server <- function(input, output) {
  
output$barchart <- renderPlotly({

scatter <- ggplot(data = midwest, 
         mapping = aes(x = percollege, y = percpovertyknown)) + 
    geom_point(aes(fill = state, size = popdensity), shape = 21, color = "white") + 
  #  geom_smooth(aes(x = percollege, y = percpovertyknown)) +
    labs(
      x = "Percent with college education", 
      y = "Poverty rate", 
      title = "Midwest Data",
      subtitle = "States with higher college education rates tend to have lower poverty rates") + 
    scale_color_brewer(palette = "Set1") + 
    scale_size(range = c(0, 12)) +
    guides(size = guide_legend(override.aes = list(col = "black")), 
           fill = guide_legend(override.aes = list(size = 5))) +
    theme_bw()
  chartly <- ggplotly(scatter)
  return(chartly)

})
  }

用户界面:

library(shiny)
library(ggplot2)
library(dplyr)
library(plotly)
library(lintr)

page_one <- tabPanel(
  "Introduction",
ui <-  fluidPage(

# START OF PAGE 2 / CHART---------------------------------
# Inputting Panel
page_two_sidepanel <- sidebarPanel(
  checkboxGroupInput("checkGroup",
                     label = h3("Select State"), 
                     choices = list("Illinois" = "IL",
                                    "Indiana" = "IN",
                                    "Minnesota" = "MN",
                                    "Ohio" = "OH",
                                    "Wisconsin" = "WN"),
                     selected = "IL"
  )
),

page_two_mainpanel <- mainPanel(
  h2("Barchart Comparison by Income Classification"), 
  plotOutput(
    outputId = "barchart"
  )
)

标签: rggplot2shiny

解决方案


我无法重现您遇到的错误,代码运行得很好。为简单起见,我将代码制作成单个 app.R 文件。我发现的一个问题是它应该是 plotlyOutput 而不是 plotOutput。

library(dplyr)
library(tidyr)
library(stringr)
library(lintr)
library(shiny)
library(ggplot2)
library(knitr)
library(tidyverse)
library(operators)
library(magrittr)
library(plotly)

    ui <-  fluidPage(
        

            checkboxGroupInput("checkGroup",
                               label = h3("Select State"), 
                               choices = list("Illinois" = "IL",
                                              "Indiana" = "IN",
                                              "Minnesota" = "MN",
                                              "Ohio" = "OH",
                                              "Wisconsin" = "WN"),
                               selected = "IL"
            ),
            plotOutput(
                outputId = "barchart"
            )
        )
    
    
        ui <- fluidPage(
            sidebarLayout(
                sidebarPanel(
                    checkboxGroupInput("checkGroup",
                                       label = h3("Select State"), 
                                       choices = list("Illinois" = "IL",
                                                      "Indiana" = "IN",
                                                      "Minnesota" = "MN",
                                                      "Ohio" = "OH",
                                                      "Wisconsin" = "WN"),
                                       selected = "IL"
                    )
                ),
                mainPanel(
                    plotlyOutput("barchart")
                )
            )
        )


# Define server logic required to draw a histogram
server <- function(input, output) {
    output$barchart <- renderPlotly({
        
        scatter <- ggplot(data = midwest, 
                          mapping = aes(x = percollege, y = percpovertyknown)) + 
            geom_point(aes(fill = state, size = popdensity), shape = 21, color = "white") + 
            #  geom_smooth(aes(x = percollege, y = percpovertyknown)) +
            labs(
                x = "Percent with college education", 
                y = "Poverty rate", 
                title = "Midwest Data",
                subtitle = "States with higher college education rates tend to have lower poverty rates") + 
            scale_color_brewer(palette = "Set1") + 
            scale_size(range = c(0, 12)) +
            guides(size = guide_legend(override.aes = list(col = "black")), 
                   fill = guide_legend(override.aes = list(size = 5))) +
            theme_bw()
        chartly <- ggplotly(scatter)
        return(chartly)
        
    })
}

# Run the application 
shinyApp(ui = ui, server = server)


推荐阅读