I am going to build a web page that clusters iris data based on the number of clusters the user enters. It uses K means algorithm to cluster the data and shows a plot of clustered data. It does not work and I do not know why. I started from this link: http://rstudio.github.io/shiny/tutorial/#sending-images
我将构建一个web页面,根据用户输入的集群数量对iris数据进行集群。它使用K表示算法对数据进行聚类,并显示聚类数据的图。它不管用,我也不知道为什么。我从这个链接开始:http://rstudio.github.io/shiny/tutorial/#sending-images
Here are my files: ui.R
这是我的档案
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Clustering iris Data"),
sidebarPanel(
sliderInput("k", "Number of clusters:",
min = 1, max = 5, value = 3)
),
mainPanel(
# Use imageOutput to place the image on the page
imageOutput("myImage")
)
))
and server.R
和server.R
library(shiny)
library(caret)
library(ggplot2)
data(iris)
inTrain <- createDataPartition(y=iris$Species, p=0.7, list=FALSE)
training <- iris[inTrain,]
testing <- iris[-inTrain,]
shinyServer(function(input, output, session) {
output$myImage <- renderImage({
# A temp file to save the output.
# This file will be removed later by renderImage
outfile <- tempfile(fileext='.png')
kMeans1 <- kmeans(subset(training,select=-c(Species)),centers=input$k)
training$clusters <- as.factor(kMeans1$cluster)
# Generate the PNG
png(outfile, width=400, height=600)
qplot(Petal.Width,Petal.Length,colour=clusters,data=training,main="iris Data Clusters")
print(qplot)
#plot(training$Petal.Width,training$Petal.Length,colour=clusters,data=training,main="iris Data Clusters")
#hist(rnorm(input$k), main="Generated in renderImage()")
#myImage
dev.off()
# Return a list containing the filename
list(src = outfile,
contentType = 'image/png',
width = 400,
height = 600,
alt = "This is alternate text")
}, deleteFile = TRUE)
})
1 个解决方案
#1
3
I think you just have to change
我认为你必须改变。
qplot(Petal.Width,Petal.Length,colour=clusters,data=training,main="iris Data Clusters")
print(qplot)
to something like this:
是这样的:
qP <- qplot(
Petal.Width,Petal.Length,
colour=clusters,data=training,
main="iris Data Clusters")
print(qP)
Because your call to qplot()
was not actually creating an object; which is why print(qplot)
was printing the function definition of qplot
in the console.
因为您对qplot()的调用实际上并没有创建对象;这就是为什么print(qplot)要在控制台中打印qplot的函数定义。
#1
3
I think you just have to change
我认为你必须改变。
qplot(Petal.Width,Petal.Length,colour=clusters,data=training,main="iris Data Clusters")
print(qplot)
to something like this:
是这样的:
qP <- qplot(
Petal.Width,Petal.Length,
colour=clusters,data=training,
main="iris Data Clusters")
print(qP)
Because your call to qplot()
was not actually creating an object; which is why print(qplot)
was printing the function definition of qplot
in the console.
因为您对qplot()的调用实际上并没有创建对象;这就是为什么print(qplot)要在控制台中打印qplot的函数定义。