簡體   English   中英

R - 閃亮的包裹 - 發送情節閃亮

[英]R - shiny package- sending plot to shiny

我將構建一個網頁,根據用戶輸入的集群數量對虹膜數據進行聚類。 它使用K均值算法對數據進行聚類,並顯示聚類數據的圖。 它不起作用,我不知道為什么。 我從這個鏈接開始: http//rstudio.github.io/shiny/tutorial/#sending-images

這是我的文件: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")
  )
))

和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)

})

我想你只需要改變

qplot(Petal.Width,Petal.Length,colour=clusters,data=training,main="iris Data Clusters")
print(qplot)

這樣的事情:

qP <- qplot(
      Petal.Width,Petal.Length,
      colour=clusters,data=training,
      main="iris Data Clusters")
    print(qP)

因為你對qplot()調用實際上並沒有創建一個對象; 這就是print(qplot)在控制台中print(qplot)的函數定義的qplot 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM