繁体   English   中英

美学中的未知问题破坏了闪亮应用程序中的 ggplotly 情节

[英]Unknown issue in aesthetics breaks down ggplotly plot in a shiny app

我有一个 k-means 闪亮应用程序,我在其中对iris数据集的选定列执行 kmeans 分析,当我将鼠标悬停在一个点上时,可以查看该点的名称、值和集群。 我收到一个Error in : Unknown input: uneval ,这很奇怪。 另外,当我将鼠标悬停在所选点上时,我应该更改什么才能显示我想要的文本?

#ui.r
# k-means only works with numerical variables,
# so don't give the user the option to select
# a categorical variable
vars <- setdiff(names(iris), "Species")
library(plotly)
pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', vars),
    selectInput('ycol', 'Y Variable', vars, selected = vars[[2]]),
    numericInput('clusters', 'Cluster count', 3, min = 1, max = 9)
  ),
  mainPanel(
    plotlyOutput('plot1')
  )
)
#server.r
function(input, output, session) {
  
  
  
  output$plot1 <- renderPlotly({
    

      # Combine the selected variables into a new data frame
      iris<-iris[, c(input$xcol, input$ycol)]
     
      cls <- kmeans(x = iris, centers = input$clusters)
      iris$cluster <- as.character(cls$cluster)
      ggplotly(ggplot() +
                 geom_point(data = iris, 
                            mapping = aes(x = iris[,1], 
                                          y = iris[,2], 
                                          colour = cluster))+
                 scale_x_discrete(name =as.character(input$xcol))+
                 scale_y_discrete(name =as.character(input$ycol))+
                 theme_light()+
                 geom_text(mapping = aes_string(x = cls$centers[, input$xcol], 
                                                y = cls$centers[, input$ycol],
                                                aes(label = 1:input$clusters)),
                           color = "black", size = 4))
    
  })
  
}

问题是您在aes_string使用了aes 我删除了aes ,然后它起作用了:

library(plotly)
library(shiny)
library(ggplot2)

vars <- setdiff(names(iris), "Species")

ui <- pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', vars),
    selectInput('ycol', 'Y Variable', vars, selected = vars[[2]]),
    numericInput('clusters', 'Cluster count', 3, min = 1, max = 9)
  ),
  mainPanel(
    plotlyOutput('plot1')
  )
)
#server.r
server <- function(input, output, session) {
  
  
  
  output$plot1 <- renderPlotly({
    
    
    # Combine the selected variables into a new data frame
    iris<-iris[, c(input$xcol, input$ycol)]
    
    cls <- kmeans(x = iris, centers = input$clusters)
    iris$cluster <- as.character(cls$cluster)
    
    ggplotly(ggplot() +
               geom_point(data = iris, 
                          mapping = aes(x = iris[,1], 
                                        y = iris[,2], 
                                        colour = cluster))+
               scale_x_discrete(name =as.character(input$xcol))+
               scale_y_discrete(name =as.character(input$ycol))+
               theme_light()+
               geom_text(mapping = aes_string(x = cls$centers[, input$xcol], 
                                              y = cls$centers[, input$ycol],
                                              label = 1:input$clusters),
                         color = "black", size = 4))
    
  })
  
}

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM