upendra
upendra

Reputation: 2189

Cannot display plot in Rshiny

I have a shiny app and what I am trying to do is to print the header or full data frame in one tab, summary in another tab and finally a plot in the third tab. For some reason, I was able to generate the table and summary in the first two tabs but not the plot. I don't know what I am doing wrong.

Here is my ui.R script

library(shiny)
library(shinyFiles)

shinyUI(pageWithSidebar(
  headerPanel(
    'Selections with shinyFiles',
    'shinyFiles example'
  ),
  sidebarPanel(
    tags$h3('Select your file'),

    # Shiny button   
    shinyFilesButton(id = 'file', label = 'File select', title = 'Please select a file', multiple = FALSE),

    tags$h3('Select your options for table'),

    # Horizontal line ----
    br(),

    # Input: Checkbox if file has header ----
    checkboxInput("header", "Header", TRUE),

    # Input: Select separator ----
    radioButtons("sep", "Separator",
                 choices = c(Comma = ",",
                             Semicolon = ";",
                             Tab = "\t"),
                 selected = ","),

    # Input: Select quotes ----
    radioButtons("quote", "Quote",
                 choices = c(None = "",
                             "Double Quote" = '"',
                             "Single Quote" = "'"),
                 selected = '"'),

    # Horizontal line ----
    tags$hr(),

    # Input: Select number of rows to display ----
    radioButtons("disp", "Display",
                 choices = c(Head = "head",
                             All = "all"),
                 selected = "head"),

    tags$h3('Select your options for plotting'),

    br(),

    # Input: 
    radioButtons("sho", "Show",
                 choices = c(Yes = "yes",
                             No = "no"),
                 selected = "yes")
    ),
  mainPanel(
    tabsetPanel(type = "tabs",
                tabPanel("Table", dataTableOutput("contents")),
                tabPanel("Summary", verbatimTextOutput("summary")),
                tabPanel("Plot", plotOutput("plt"))

  )
  ))
)

And here is my server.R script

library(shiny)
library(shinyFiles)

shinyServer(function(input, output, session) {
  shinyFileChoose(input, 'file', roots=c(wd='/Users/upendra_35/Documents/CyVerse/Images_apps/DE/VICE/'), filetypes=c('', 'csv'))
  shinySaveButton("save", "Save file", "Save file as ...", filetype=c('', "csv"))
  output$contents <- renderDataTable({
    inFile <- parseFilePaths(roots=c(wd='/Users/upendra_35/Documents/CyVerse/Images_apps/DE/VICE/'), input$file)
    if( NROW(inFile)) {
      df <- read.csv(as.character(inFile$datapath),
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote)
      if(input$disp == "head") {
        return(head(df))
      }
      else {
        return(df)
      }
    }
  })


  output$summary <- renderPrint({
    inFile <- parseFilePaths(roots=c(wd='/Users/upendra_35/Documents/CyVerse/Images_apps/DE/VICE/'), input$file)
    if( NROW(inFile)) {
      df <- read.csv(as.character(inFile$datapath),
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote)

      summary(df)
    }
  })

  output$plt <- renderPlot({
    inFile <- parseFilePaths(roots=c(wd='/Users/upendra_35/Documents/CyVerse/Images_apps/DE/VICE/'), input$file)
    if( NROW(inFile)) {
      df <- read.csv(as.character(inFile$datapath),
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote)
      if(input$sho == "yes") {
        manh <- manhattanr(df, chr = "chrom", bp = "pos", p = "P", snp = "marker")
        manhattanly(manh, col=c("#D2691E","#800080","#6495ED","#9ACD32"), point_size=7, showlegend = FALSE, 
                    xlab = "Chromosome", ylab = "-log10(p)", suggestiveline_color = "blue", suggestiveline_width = 2, 
                    genomewideline =FALSE, title = "")
      }
      else {
        print("No plot")
      }

    }
  })

})

The code for the plot definitely works when I run it manually in the R console.

Upvotes: 0

Views: 300

Answers (2)

upendra
upendra

Reputation: 2189

I have figured it out. I was using the wrong functions to print the plotly plot. Since manhattanly is a plotly library, I supposed to be using renderPlotly instead of renderPlot in server.R and plotlyOutput instead of plotOutput in ui.R

Upvotes: 0

msr_003
msr_003

Reputation: 1233

Your code looks nice. But i believe there are no required fields to get manhattanly plot in the file what you are choosing. I just tried with the default HapMap data which comes along with manhattanly package. Just import that data to your R environment and save it to your local machine as a csv file. Then select the same file for your testing purpose. Below is the renderPlot block from your code which worked for me.

  output$plt <- renderPlot({
    inFile <- parseFilePaths(roots = c(wd = 'Data/'), input$file)
    if (NROW(inFile)) {
      df <- read.csv(as.character(inFile$datapath)
                      header = input$header,
                      sep = input$sep,
                      quote = input$quote
                     )
    if (input$sho == "yes") {
        #manh <- manhattanr(df, chr = "CHR", bp = "pos", p = "P", snp = "SNP", gene = "GENE")
      # manhattanly(manh, col=c("#D2691E","#800080","#6495ED","#9ACD32"), point_size=7, showlegend = FALSE, 
      #             xlab = "Chromosome", ylab = "-log10(p)", suggestiveline_color = "blue", suggestiveline_width = 2, 
      #             genomewideline =FALSE, title = "")
        manhattanly(df, 
                    snp = "SNP", gene = "GENE", 
                    annotation1 = "ZSCORE", annotation2 = "EFFECTSIZE", 
                    highlight = significantSNP)
      }
      else {
      print("No plot")
      }

    }
  })

Note : It is plotting in a separate tab in my browser. Looks like a little workaround should fix it.

Upvotes: 1

Related Questions