r - 错误:无法使用 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)
解决方案
采用 -
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)
输出
解释
你混淆了一些东西,但你几乎就在那里。您需要的一切都在这里。你只需要组织起来。让我们来看看组件 -
gggauge
是一个渲染绘图的函数,所以它需要是一个单独定义并从服务器组件调用的函数。仅仅在server
对象中定义它是不够的。在您的情况下,该函数被定义,而不是被调用!- 该
ui
组件需要一个占位符来渲染绘图。直接尝试渲染情节ui
不会削减它。您只需要一个plotOutput
充当占位符的对象(UI 组件) - 现在已经处理了 (1) 和 (2),是时候
server
通过调用gggauge
函数在对象中渲染绘图了。
跳,有帮助!
推荐阅读
- sql - 未来三个月的滚动销售额
- c++ - Linux 中的单例与共享模块和静态内存访问
- ios - Shaka Player 嵌入式 Widevine 配置 JSON 缺失
- python - 执行 Python subprocess.run 继续调用外部 MPIRUN 命令
- r - 返回选定的 tklistbox 值
- ruby-on-rails - 将活动 TCP 会话负载平衡到 AWS Aurora RDS
- typescript - 在打字稿中的对象中插入键值对
- karate - 空手道 UI 对 afterScenario 钩子中的错误进行截图
- json - 如果在使用 JOLT 转换时值具有特殊字符,则 JSON 解析会出错(无效的 UTF-8 起始字节 0xb7)
- php - 如何在 PHP 中使用 xPath 显示有关所选工程工具的信息