将 ggplot 气泡图导入 Shiny 时出错

Error when importing ggplot bubble chart into Shiny

我在使用 Shiny 和 ggplot 时遇到了一些问题

当我 运行 使用以下代码在 Shiny 中生成气泡图时,我收到此错误消息:

"Error in $<-.data.frame(*tmp*, "value", value = numeric(0)) : 替换有 0 行,数据有 3"

但是当我运行它在外面的时候就很好

有什么想法吗?

谢谢

library(shiny)
library(ggplot2)

gtest = data.frame(cbind(sort(rep(c(1:3),5)),rep(c(1:5),3),sample(-8:8, 15)/10))


# Define a server for the Shiny app
server <- function(input, output) {

  g3 <- reactive({
    g2 <- gtest[gtest[2]==input$article,]
    g2 <-g2[order(g2[3],decreasing = TRUE), ]
    g2 <- rbind(g2[g2[1]==input$article,], g2[!g2[1]==input$article,])
    angl <-seq(0, 360, length.out =(nrow(g2)))
    angl <-angl[1:(length(angl)-1)]
    g2[3] <-(max(g2[3])-min(g2[3]))*.3+(max(g2[3])-g2[3])
    x<-c(0,g2[2:nrow(g2),3]*cos(angl%*%(pi/180)))
    y<-c(0,g2[2:nrow(g2),3]*round(sin((pi/180)*angl),2))
    g2<-cbind(g2,x,y)
    colnames(g2) <- c("col1","col2","value","x","y")
    g2
  })

  # Fill in the spot we created for a plot

  output$g3plot = renderPlot({
    p<-ggplot(g3(),aes(x,y,label= col1))+
      geom_point(colour="white", fill="red", shape=21, size = 20)+
      geom_text(size=5)+
      theme_bw()
    print(p)
  })
}

#runApp("R/Test_shiny")

# Define the overall UI
ui <- fluidPage(  
  titlePanel("Articles by similarities"),
  sidebarLayout(      
    sidebarPanel(
      selectInput("article", "Article:", choice=unique(gtest[2]))), hr()),
  mainPanel(
    plotOutput("g3plot")
  )   
)


shinyApp(ui = ui, server = server)

我这边的代码似乎运行:

因此我修改了以下行:

x<-c(0,g2[2:nrow(g2),3]*cos(angl*(pi/180)))

此致