【问题标题】:R - shiny package- sending plot to shinyR - 闪亮的包 - 将情节发送到闪亮
【发布时间】:2014-09-12 04:37:35
【问题描述】:

我将构建一个网页,根据用户输入的聚类数量对虹膜数据进行聚类。它使用 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")
  )
))

和服务器.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)

})

【问题讨论】:

    标签: r plot shiny


    【解决方案1】:

    我认为你只需要改变

    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) 在控制台中打印qplot 的函数定义的原因。

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 2017-08-12
      • 2016-03-25
      • 2023-03-13
      • 2021-05-10
      • 2019-08-15
      • 1970-01-01
      • 2017-04-04
      • 2019-02-12
      相关资源
      最近更新 更多