ERROR: cannot coerce type 'environment' to vector of type 'character' with ggplot

ERROR: cannot coerce type 'environment' to vector of type 'character' with ggplot

我正在尝试利用在 SO 上找到的 ggplot 代码。问题是我不断收到错误,我认为这与尝试通过 shinydashboard 传递函数有关。我已经尝试过各种各样的事情。我可以让它在控制台中正常呈现,但在 shinydashboard 中却不行。

 library(shinydashboard)
 library(shiny)
 library(ggplot2)
 library(dplyr)

 ui <- fluidPage(
   gggauge(52,breaks=c(0,35,70,100)
 )

 )
 server <- function(input, output) {
   gggauge <- function(pos,breaks=c(0,30,70,100)) {
     get.poly <- function(a,b,r1=0.5,r2=1.0) {
       th.start <- pi*(1-a/100)
       th.end <- pi*(1-b/100)
       th <- seq(th.start,th.end,length=100)
       x <- c(r1*cos(th),rev(r2*cos(th)))
       y <- c(r1*sin(th),rev(r2*sin(th)))
       return(data.frame(x,y))
     }
     ggplot()+ 
      geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y),fill="red")+
      geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y),fill="gold")+geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y),fill="forestgreen")+
      geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y))+
      geom_text(data=as.data.frame(breaks), size=5, fontface="bold", vjust=0,
        aes(x=1.1*cos(pi*(1-breaks/100)),y=1.1*sin(pi*(1-breaks/100)),label=paste0(breaks,"%")))+
      annotate("text",x=0,y=0,label=pos,vjust=0,size=8,fontface="bold")+
      coord_fixed()+
      theme_bw()+
      theme(axis.text=element_blank(),
        axis.title=element_blank(),
        axis.ticks=element_blank(),
        panel.grid=element_blank(),
        panel.border=element_blank()) 
   }
 }
 shinyApp(ui = ui, server = server)

使用 -

library(shinydashboard)
library(shiny)
library(ggplot2)
library(dplyr)

gggauge <- function(pos,breaks=c(0,30,70,100)) {
  get.poly <- function(a,b,r1=0.5,r2=1.0) {
    th.start <- pi*(1-a/100)
    th.end <- pi*(1-b/100)
    th <- seq(th.start,th.end,length=100)
    x <- c(r1*cos(th),rev(r2*cos(th)))
    y <- c(r1*sin(th),rev(r2*sin(th)))
    return(data.frame(x,y))
  }
  ggplot()+ 
    geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y),fill="red")+
    geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y),fill="gold")+geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y),fill="forestgreen")+
    geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y))+
    geom_text(data=as.data.frame(breaks), size=5, fontface="bold", vjust=0,
              aes(x=1.1*cos(pi*(1-breaks/100)),y=1.1*sin(pi*(1-breaks/100)),label=paste0(breaks,"%")))+
    annotate("text",x=0,y=0,label=pos,vjust=0,size=8,fontface="bold")+
    coord_fixed()+
    theme_bw()+
    theme(axis.text=element_blank(),
          axis.title=element_blank(),
          axis.ticks=element_blank(),
          panel.grid=element_blank(),
          panel.border=element_blank()) 
}

ui <- fluidPage(

  plotOutput("plot1", click = "plot_click"),
  verbatimTextOutput("info")

)

server <- function(input, output) {

  output$plot1 <- renderPlot({
    #plot(mtcars$wt, mtcars$mpg)
    gggauge(52,breaks=c(0,35,70,100))
  })
}
shinyApp(ui = ui, server = server)

输出

说明

你混淆了一些东西,但你几乎就在那里。您需要的一切都在这里。你只需要组织。让我们来看看组件 -

  1. gggauge是渲染图的函数,所以需要单独定义一个函数,从服务器组件调用。仅仅在 server 对象中定义它是不够的。在你的例子中,函数是被定义的,而不是被调用的!
  2. ui 组件需要一个占位符来渲染绘图。直接尝试在 ui 中渲染情节不会削减它。你只需要一个 plotOutput 对象作为占位符(一个 UI 组件)
  3. 现在 (1) 和 (2) 已经处理完毕,是时候通过调用 gggauge 函数在 server 对象中渲染绘图了。

希望有帮助!