【问题标题】:shinyapps.io does not draw plotsshinyapps.io 不绘制图
【发布时间】:2017-11-16 22:22:48
【问题描述】:

我使用 FactorMineR 包构建了一个简单的应用程序,根据所选变量进行 MCA 分析和聚类。

该应用程序在我的本地设备上运行良好,但它没有在 shinyapps.io 服务器上显示任何图(基本图和 ggplots)。我检查了包裹,本地和远程它们是相同的。我还检查了 FactoMineR pcg 中的 MCA() 函数是否可以通过提取一些结果并将它们呈现为表格来产生积极的结果。所以只有绘图的问题。我一直在尝试解决它两天,但没有任何帮助,所以我向您寻求任何建议。

这是它在本地的样子:

这是应用程序的链接:https://mikolajm.shinyapps.io/MCA_test/

还有一个可重现的例子

library(shiny)
library(FactoMineR)
library(cluster)
library(ggplot2)
data(tea)

ui <- fluidPage(

  # Application title
  titlePanel("MCA"),
  textOutput("packages"),br(),
  tableOutput("table"),br(),

  fluidRow(
    column(4, checkboxGroupInput("Variables", "Select variables:", 
                                 names(tea), selected=c("breakfast", "tea.time"))),
    column(4, plotOutput("plot")), column(4, plotOutput("plot1"))),
  fluidRow(column(12, plotOutput("dendro", height = "700px", width="1200px"))
  )
)

server <- function(input, output) {

  ## packages checking
  output$packages <- renderText({.packages()})
  tea_selected <- reactive({
    tea[, input$Variables]
  })

  ## table with some results from MCA() fun
  output$table <- renderTable({
    tea.mca <- MCA(tea_selected(), ncp=9)
    tea.mca$eig[1:5,]

  })

  ##  mca1
  output$plot <- renderPlot({
    library(FactoMineR)
    par(mfrow=c(2,2))
   tea.mca <- MCA(tea_selected(), ncp=9)
  })


  ## mca with ggplot
  output$plot1 <- renderPlot({

    tea.mca <- MCA(tea_selected(), ncp=9)
    tea_vars_df <- data.frame(tea.mca$var$eta2, Variable =names(tea_selected())) 

    library(ggplot2)

    pp <- ggplot(data=tea_vars_df, aes(x=Dim.1, y=Dim.2, label=Variable))+
      geom_hline(yintercept = 0, colour = "gray70") +
      geom_vline(xintercept = 0, colour = "gray70") +
      geom_point()+
      geom_text() +
      ggtitle("MCA plot of variables ")+
      theme_bw()

    pp
    })
  ### dendro 

  output$dendro <- renderPlot({
    library(FactoMineR)
    library(cluster)

    tea.mca <- MCA(tea_selected(), ncp=9)
    classif <- agnes(tea.mca$ind$coord,method="ward")
    plot(classif,main="Dendrogram",ask=F,which.plots=2)
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 您是否使用 R 代码上传数据?您能否显示用于将 app.R 和数据上传到 shinyapps.io 的命令?如果它在本地工作,最简单的解释是您丢失了 shinyapps.io 上的数据。
  • 我使用 rstudio 发布按钮上传了我的代码(我的帖子中的那个)。数据(茶)包含在 factorominer 包中。复选框从该数据中导入变量,它们显示在网页上,但不显示在图表上。所以我认为这不是数据问题。
  • 每次闪亮在 apps.io 上运行时,R 会话都必须运行它。你确定 R 闪亮服务器上的 R 环境有 FactoMineR 吗?将textOutput("packages") 添加到您的ui 并将output$packages &lt;- renderText({.packages()}) 添加到您的server。在本地尝试,它应该打印在您的环境中加载的包。然后在apps.io上试试...
  • @MikolajM 我测试了部署并检查了 shinyapps.io 中的日志。有这些行: 2017-06-16T20:01:20.810740+00:00 shinyapps[189841]: dev.new(): using pdf(file="Rplots1.pdf") ... 所以看起来情节到 pdf 输出而不是渲染的 img。您的应用中有多个绘图,您只能启用其中一个并再次测试以定位问题。我注意到的一件事是output$plot 中的p,那是什么?错别字?
  • 当你使用 ggplot 时,你不应该在 ggplot 对象上使用plot()。只需返回将被渲染的 ggplot 对象。这可能是问题的根本原因。

标签: r plot shiny shinyapps


【解决方案1】:

编辑:你可以清楚地看到情节,但是

原创
当我运行你的代码时,我在你闪亮的应用程序中看不到绘图。

经过一番挖掘,我的猜测是:

您使用了FactoMineR 软件包附带的许多功能。例如,您在 output$plot1 代码块中使用函数 MCA。在您的 R 命令行中键入 MCA,它应该会打印该函数。你可以看到MCA 做了很多事情并最终调用了plot.MCA。现在在 R 命令行中输入 plot.MCA。你可以看到plot.MCA 有很多plot 命令,我很确定当你调用MCA 时它会执行所有的绘图。我认为您的问题是函数plot.MCA 中的plot 被发送到图形设备,并且这些图没有保存,即它们不是return()parent 环境。 这只是猜测。

【讨论】:

  • 我在帖子中添加了应用程序的屏幕截图。我会尝试使用 return() 函数来解决这个问题
  • 对不起,我不知道...但是,当我运行您的代码时,我没有看到这些图。我将更新我的答案以表明您可以看到图,但我不能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2012-10-31
  • 2015-08-09
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多