0

i want to create a heatmap from user csv file in shiny, so far i have succeeded in reading the table in Shiny and also got the heatmap. however, the row names from the input csv file are not shown in the heatmap. the column names seems to appear fine.

server function

plotdata <- eventReactive(input$getHmap, {
data <- as.matrix(data()[-1], )
row.names(data) <- data()$Name

data[is.na(data)] <- 0
data
})

output$theheatmap = renderPlot({ 
pheatmap(plotdata())

})

Pretty sure the UI script is fine..

#sliderbar for heatmap
conditionalPanel(condition = "$('li.active a').first().html()==='Heatmap'",
                             h2 ("Click the button to produce heatmap"),
                             actionButton('getHmap', 'get heatmap')),
mainpanel (
tabsetpanel ( 
tabPanel('Heatmap',
          fluidRow(column(8, offset = 1,
                          h2("Heatmap"),
                          plotOutput("theheatmap"),
                          ))  

something is missing in the server function and i cant seem to get my head around it

  • Can you provide a full reproducible example? Are you bringing in the row names from the CSV into the server using row.names = TRUE? – Kevin Jan 02 '20 at 18:00
  • i have changed the output in server as below, but i still get the same output. output$theheatmap = renderPlot({ pheatmap(plotdata(), cluster_rows = T, cluster_cols = T, show_rownames = T, show_colnames = T) }) – Najeha Mohamed Jan 02 '20 at 18:58
  • Are the row names shown when you bring in the csv into R (not the heatmap yet)? Again, it's hard to help without a full reproducible example. – Kevin Jan 02 '20 at 19:41

1 Answers1

0

I tried to remake your app with the mtcars dataset and it works fine so I suppose the problem comes from the way you import your data. But I agree with @Kevin, you should provide a reproducible example because it is hard to see what your problem is without the full code.

Here's what I did, hope that it helps:

library(shiny)
library(tibble)
library(pheatmap)

ui <- basicPage(
  #sliderbar for heatmap
  conditionalPanel(condition = "$('li.active a').first().html()==='Heatmap'",
                   h2 ("Click the button to produce heatmap"),
                   actionButton('getHmap', 'get heatmap')),
  mainPanel (
    tabsetPanel ( 
      tabPanel('Heatmap',
               fluidRow(column(8, offset = 1,
                               h2("Heatmap"),
                               plotOutput("theheatmap"),
               ))  )
    )
  )
)

server <- function(input, output){

  data <- reactive({
    x <- head(mtcars)
    x <- rownames_to_column(x, "Name")
  })

  plotdata <- eventReactive(input$getHmap, {
    data <- as.matrix(data()[-1], )
    row.names(data) <- data()$Name

    data[is.na(data)] <- 0
    data
  })

  output$theheatmap = renderPlot({ 
    pheatmap(plotdata())

  })

}

shinyApp(ui, server)
bretauv
  • 7,756
  • 2
  • 20
  • 57
  • 1
    Nice example! I think the OP's issue is when reading the CSV into R, they are not using row.names = TRUE to bring the row names in with it. But again, hard to tell without the code. – Kevin Jan 03 '20 at 13:41