Saturday 14 October 2017

r - shinyapps.io does not draw plots

I built a simple app using FactorMineR package to do MCA
analysis and clustering depending on selected
variables.



The app works fine on my local
device, however it does not show any plots (either base plots and ggplots) on
shinyapps.io server. I checked the packages and locally and remotley they are the same.
I also checked if the MCA() function from FactoMineR pcg even works by extracking some
results and rendering them as a table what gave positive results. So there is only the
problem with plots drawing. I have been trying to solve it for two days but nothing
helps so I am asking you for any advice.



Here
is how it looks locally: alt="mca_locally">



Here is the link
to the app: https://mikolajm.shinyapps.io/MCA_test/




And
a reproducible
example



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)

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...