【发布时间】: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 <- 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 对象。这可能是问题的根本原因。