0

I've just discovered the powerful beauty of Shiny. I tried to make a simple app in which I can load an excel file and perform an PCA on it using the FactoMineR plug-in in rcmdr (for now, the code is just written for this specific file).

I manage to build an app where I can load my file, and display three different plot with the result of the PCA. Everything works fine in RStudio, but when I deploy the app, the graph are not shown. Everything else seems to be working fine !

Any idea on where that issue might come from ?

Best regars,

Léolo

library(shiny)
library(gdata) 
library(Rcmdr)
library(RcmdrMisc)
library(FactoMineR)

options(shiny.sanitize.errors = FALSE)

# Define UI ----
ui <- fluidPage(
  titlePanel("Introducing PCApp"), 
  p("Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Nam augue enim, vestibulum vitae nisi vel, placerat suscipit ipsum.
    Duis eu quam lobortis, tincidunt tellus eget, condimentum est.
    In nibh velit, tempor ac neque et, viverra pretium sapien. 
    Fusce massa sapien, varius eu rutrum at, pharetra sit amet leo. Orci varius.",
    br(), 
  sidebarLayout(position = "left",
    sidebarPanel(position = "left",
                 p("Principal Composant Analysis of PCR data from normal and tumor samples.",
                   br(), br(), "Add your data by importing a valid file.", br(), 
                   span(a("What's a valid file?", target="popup", onclick="window.open('validfile.html','popup','width=505,height=600'); return false;"))),

                 fileInput("means", "Choose a valid file",
                           accept = c(
                             "text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv",
                             ".xls",
                             ".xlsx")

                 ),
                 tags$hr(),

                 selectInput("var", 
                             label = "Choose a variable to display",
                             choices = c("Factor Map", "Variables Factor map (PCA)", 
                                          "Individuals Factor map (PCA)"),
                             selected = "Factor Map")
                 ),
    mainPanel(
      plotOutput("means"),
      textOutput("selected_var")

    )
  )
)

# Define server logic ----
server <- function(input, output) {


  output$means <- renderPlot({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$means

    if (is.null(inFile))
      return(NULL)

    means<-readXL(inFile$datapath, 
           rownames=TRUE, header=TRUE, na="",
           stringsAsFactors=TRUE)

    means.PCA<-means[, c("CTNNB1", "LIN28B", "MYC", "SMARCA4", "SOX9", "TERT", 
                         "TP53", "hsa.let.7b")]
    res<-PCA(means.PCA , scale.unit=TRUE, ncp=2, graph = FALSE)
    res.hcpc<-HCPC(res ,nb.clust=-1,consol=FALSE,min=3,max=10,graph=FALSE)

    if(input$var == "Factor Map")
    p<-res.hcpc<-HCPC(res ,nb.clust=-1,consol=FALSE,min=3,max=10,graph=TRUE)
    print(p)

    if(input$var == "Individuals Factor map (PCA)")
      p<-plot.PCA(res, axes=c(1, 2), choix="ind", habillage="none", col.ind="black", 
               col.ind.sup="blue", col.quali="magenta", label=c("ind", "ind.sup", "quali"),
               new.plot=TRUE)
    print(p)

    if(input$var == "Variables Factor map (PCA)")
    p<-plot.PCA(res, axes=c(1, 2), choix="var", new.plot=TRUE, col.var="black", 
             col.quanti.sup="blue", label=c("var", "quanti.sup"), lim.cos2.var=0)
    summary(res, nb.dec = 3, nbelements=10, nbind = 10, ncp = 3, file="")
    remove(means.PCA)
    print(p)

  })

  output$selected_var <- renderText({ 
    inFile <- input$means
    if (is.null(inFile))
      return(paste("Please load a valid file ..."))

    paste("You have selected : ", input$var)
  })

  }

# Run the app ----
shinyApp(ui = ui, server = server)
  • I would recommend you to check whether the table is loaded correctly. Currently, you only check whether the path is `NULL`. Maybe use `renderTable({means <- readXL(...); means})`. Another thing you could try is to use `plot.PCA(..)` rather than `p<-plot.PCA(...); print(p)`. Saving plots as variables does not always work in R. – Gregor de Cillia Sep 29 '17 at 11:36
  • 1
    Hi ! Thanks for your advices ! I'll check for the table (you're right!) ! Bur regarding the plot.PCA, the thing you suggest is already something I've tried... It works on local but not when deployed. – Léolo Gonay Sep 30 '17 at 15:11

0 Answers0