[英]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.