简体   繁体   中英

Unknown issue in aesthetics breaks down ggplotly plot in a shiny app

I have a k-means shiny app in which I perform kmeans analysis on selected column of the iris dataset and when I hover over a point to see the names the values and the cluster of this point. Im getting an Error in : Unknown input: uneval which is weird. Also what should I change in order to display the text that I wish when I hover over the selected point?

#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))
    
  })
  
}

The problem was that you used aes within aes_string . I removed aes , then it works:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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