R 美学中的未知问题在一个闪亮的应用程序中以生动的情节分解
我有一个k-means闪亮的应用程序,在该应用程序中,我对R 美学中的未知问题在一个闪亮的应用程序中以生动的情节分解,r,ggplot2,shiny,plotly,R,Ggplot2,Shiny,Plotly,我有一个k-means闪亮的应用程序,在该应用程序中,我对iris数据集的选定列执行kmeans分析,当我将鼠标悬停在某个点上以查看该点的名称、值和簇。我在:未知输入:uneval中收到一个错误,这很奇怪。另外,当我将鼠标悬停在所选点上时,为了显示我想要的文本,我应该更改什么 #ui.r # k-means only works with numerical variables, # so don't give the user the option to select # a categori
iris
数据集的选定列执行kmeans分析,当我将鼠标悬停在某个点上以查看该点的名称、值和簇。我在:未知输入: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))
})
}
#ui.r
#k-均值仅适用于数值变量,
#因此,不要给用户选择的选项
#分类变量
vars问题是您在aes\u字符串中使用了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)
library(plotly)
图书馆(闪亮)
图书馆(GG2)
vars如何修改悬停文本以显示变量名而不是iris[,1]等?我接受了这一点并创建了一个新的Q