首页 > 解决方案 > 错误:无法使用 ggplot 将类型“环境”强制转换为“字符”类型的向量

问题描述

我正在尝试利用在 SO 上找到的 ggplot 代码。问题是我不断收到错误,我认为这与尝试通过 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)

标签: rggplot2shinyshinydashboard

解决方案


采用 -

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),是时候server通过调用gggauge函数在对象中渲染绘图了。

跳,有帮助!


推荐阅读