尽管没有任何错误,但 Renderplotly 不起作用

标签 r ggplot2 shiny r-plotly

我正在使用 renderPlotly对于我 Shiny 的 ggplot 图。当我运行应用程序时,没有显示错误,但也没有渲染图。我的代码有问题吗?

这是我的数据集的示例

year region     mean_price
  <int> <fct>           <dbl>
1  2007 Central       360769.
2  2007 East          255519.
3  2007 North         218453.
4  2007 North_East    263780.
5  2007 West          233401.
6  2008 Central       429607.

这是我的代码
library("shiny")
library("shinythemes")
library('plotly')
ui <-fluidPage(
    theme=shinytheme("superhero"),
    titlePanel("Average Resale Prices "),
    sidebarLayout(
        sidebarPanel(
            selectInput("year","Year", choices = c("All","2007","2008",
                                                   "2009","2010","2011",
                                                   "2012","2013","2014",
                                                   "2015","2016","2017"), selected="All"),
            selectInput("region",'Region',choices=c("All",'North','Central','North-East','West','East'),selected="All"),

            selectInput("type","Flat-Type",choices = c("All","3 ROOM",'4 ROOM',"5 ROOM"),selected = "All"),width = 2
        ),
        mainPanel(
            tabsetPanel(
                tabPanel("Summary", plotOutput(outputId = "lineChart")),
                tabPanel("Breakdown", plotOutput(outputId = "lineChart1")), 
                type="tab"

            )

        )
    )
)



# Define server logic required to draw a line graph ----

server <- function(input, output, session){
    #df1<-reactive({
    #if(input$type =="All"){
    # plotdata1%>%
    #dplyr::filter(flat_type %in% c("3 ROOM","4 ROOM","5 ROOM"))
    #  }

    # else{
    # plotdata1%>%
    # dplyr::filter(flat_type %in% input$type)
    #   }
    # })


    plotdata1<-data1 %>% 
        group_by(year, region) %>% 
        summarize(mean_price=mean(resale_price))



    options(scipen = 100000)

    output$lineChart <- renderPlotly({
        ggplot(data=plotdata1,aes(x=year,y=mean_price))+
            geom_line(stat = 'identity',aes(colour=region,group=region))+
            geom_point()+
            xlim(c(2006,2018))+
            ylab("Average Price")+
            xlab('Year')

})

}


# Create Shiny object
shinyApp(ui = ui, server = server)

最佳答案

正如刚才提到的

您需要使用 ggplotly如果您打算使用 plotly渲染你的图表。这也意味着您需要使用 plotlyOutputui .

此外,请确保安装了您要使用的所有库。即使 shinythemes 我似乎也没有收到错误消息没有安装...虽然这不太可能。

下面的代码运行为我生成 plotly图形。

library("shiny")
library("shinythemes")
library('plotly')
library("dplyr")

year <- c(2007,2007,2007,2007,2007,2008)
region <- c("central", "East", "North", "North_East", "West", "Central")
resale_price <- c(360769, 255519, 218453, 263780, 233401, 429607)
data1 <- data.frame(year,region,resale_price)

ui <-fluidPage(
  theme=shinytheme("superhero"),
  titlePanel("Average Resale Prices "),
  sidebarLayout(
    sidebarPanel(
      selectInput("year","Year", choices = c("All","2007","2008",
                                             "2009","2010","2011",
                                             "2012","2013","2014",
                                             "2015","2016","2017"), selected="All"),
      selectInput("region",'Region',choices=c("All",'North','Central','North-East','West','East'),selected="All"),

      selectInput("type","Flat-Type",choices = c("All","3 ROOM",'4 ROOM',"5 ROOM"),selected = "All"),width = 2
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Summary", plotlyOutput("lineChart")),
        type="tab"
      ))
  )
)

# Define server logic required to draw a line graph ----

server <- function(input, output, session){

  plotdata1<-data1 %>% 
    group_by(year, region) %>% 
    summarize(mean_price=mean(resale_price))

  options(scipen = 100000)

  output$lineChart <- renderPlotly({
   p <- ggplot(data=plotdata1,aes(x=year,y=mean_price))+
      geom_line(stat = 'identity',aes(colour=region,group=region))+
      geom_point()+
      xlim(c(2006,2018))+
      ylab("Average Price")+
      xlab('Year')
   p <- ggplotly(p)
   p
  })

}

# Create Shiny object
shinyApp(ui = ui, server = server)

输出

snippet

关于尽管没有任何错误,但 Renderplotly 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57085342/

相关文章:

r - 在 R 中的轴标题中同时使用下标和变量值

r - 在Shiny中可以强制执行隐藏面板吗?

R - 替换有NA值的特定位置的值

r - ggplot2 多图,共享图例,一种背景颜色,1 个主标题和 3 个副标题和非标准布局

r - GGplot 为点添加数据标签

r - 用 alpha channel 覆盖两个 ggplot2 stat_density2d 图

r - 清除多个 selectizeInput 时如何在 shiny 中触发 observeEvent

r - 如何根据 Shiny 应用程序中复选框的输入对数据框进行子集化?

r - 使用 ggplot2::borders() 绘制太平洋和大陆

r - 如何更改ggplot中的默认美感?