Search code examples
rcsvshinypheatmap

How to plot heatmap with R shiny


I am working with R shiny for pheatmap, I want to read files and draw heatmaps, but it did not work. The csv file could be read, however, the content could not be seen from the web, and the heatmap could not be drawn.

library(shiny)
library(pheatmap)
ui = fluidPage("Test",
               sidebarPanel(
               fileInput("file1", "Choose CSV File",
                   accept = c(
                   "text/csv",
                   "text/comma-separated-values,text/plain",
                   ".csv")
                     ),
               tags$hr(),
               checkboxInput("header", "Header", TRUE)
               ),
                tabPanel('map', 
                         sidebarLayout(
                           sidebarPanel('side',
                                        actionButton('getHmap', 'get heatmap')
                           ),
                           mainPanel('main',
                                     plotOutput("themap")
                           )
                         ))
)

server = function(input, output, session) {
       a <- reactive({
       inFile <- input$file1
       if (is.null(inFile))
       return(NULL)
       tbl <- read.csv(inFile$datapath, header=input$header, sep=input$sep,  dec = input$dec)
       return(tbl)
   })
       output$table.output <- renderTable({
       a()
   })
    observeEvent(input$getHmap, {
    row.names(a) <- a$Name
    a <- a[,-1]
    a[is.na(a)] <- 0
    output$themap = renderPlot({        
    pheatmap(a)
  })
  })
}

shinyApp(ui, server)
```[![The original data I used][1]][1]


  [1]: https://i.sstatic.net/S83cH.png

Solution

  • This could be a full working example. This seems to work at my end. The following changes were made:

    • Added tableOutput("table.output") to ui
    • Simplified read.csv as inputs for sep and dec were missing
    • Created plotData function as eventReactive to plot heatmap with action button
    • Converted data to matrix before adding rownames for plot
    • The output$themap calls the plotData function
    library(shiny)
    library(pheatmap)
    ui = fluidPage("Test",
                   sidebarPanel(
                     fileInput("file1", "Choose CSV File",
                               accept = c(
                                 "text/csv",
                                 "text/comma-separated-values,text/plain",
                                 ".csv")
                     ),
                     tags$hr(),
                     checkboxInput("header", "Header", TRUE)
                   ),
                   tabPanel('map', 
                            sidebarLayout(
                              sidebarPanel('side',
                                           actionButton('getHmap', 'get heatmap')
                              ),
                              mainPanel('main',
                                        plotOutput("themap"),
                                        tableOutput("table.output")
                              )
                            ))
    )
    
    server = function(input, output, session) {
      a <- reactive({
        inFile <- input$file1
        if (is.null(inFile))
          return(NULL)
        tbl <- read.csv(inFile$datapath, header=input$header) #, sep=input$sep,  dec = input$dec)
        return(tbl)
      })
    
      output$table.output <- renderTable({
        a()
      })
    
      plotdata <- eventReactive(input$getHmap, {
        a <- as.matrix(a()[-1])
        row.names(a) <- a()$Name
        a[is.na(a)] <- 0
        a
      })
    
      output$themap = renderPlot({ 
        pheatmap(plotdata())
      })
    }
    
    shinyApp(ui, server)