gpt4 book ai didi

r - 美学中的未知问题破坏了 Shiny 应用程序中的 ggplotly plotly

转载 作者:行者123 更新时间:2023-12-04 08:50:46 25 4
gpt4 key购买 nike

我有一个 k-means Shiny 应用程序,我在其中对 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))

})

}

最佳答案

问题是你用了 aesaes_string .我删除了 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)

关于r - 美学中的未知问题破坏了 Shiny 应用程序中的 ggplotly plotly ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64107426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com