0

I am new to shiny and trying to make a shiny app. I need to plot a heatmap using pheatmap based on the numeric input from user. The data is too big, but here is a reproducable data:

a<-structure(list(gene_name = c("NAT2", "ACADS", "ACAT1", "ADA", 
                             "ADRB2", "ADRB3"), tree = c(1L, 2L, 3L, 3L, 4L, 4L)), row.names = c(NA, 
                                                                                                 6L), class = "data.frame")


b<-structure(list(Phaeodactylum_tricornutum = c(0, 1, 1, 1, 0, 0
), Coccomyxa_subellipsoidea = c(0, 1, 1, 1, 0, 0), Acanthamoeba_castellanii = c(1, 
                                                                                1, 1, 1, 0, 0), Fonticula_alba = c(0, 1, 1, 1, 0, 0), Rhizophagus_irregularis = c(0, 
                                                                                                                                                                  1, 1, 1, 0, 0), Sphaeroforma_arctica = c(0, 1, 1, 1, 0, 0), Capsaspora_owczarzaki = c(0, 
                                                                                                                                                                                                                                                        1, 1, 1, 0, 0), Cryptosporidium_parvum = c(0, 0, 0, 0, 0, 0), 
Enterobacter_cloacae = c(1, 1, 1, 1, 0, 0), gene_name = c("NAT2", 
                                                          "ACADS", "ACAT1", "ADA", "ADRB2", "ADRB3"), human_np = c("NP_000006.2", 
                                                                                                                   "NP_000008.1", "NP_000010.1", "NP_000013.2", "NP_000015.1", 
                                                                                                                   "NP_000016.1")), row.names = c(NA, 6L), class = "data.frame")

Here is the shiny code:

library(shiny)
library(DT)
library(pheatmap)
library(dplyr)

# Define UI ----
ui <- fluidPage(

  titlePanel("title panel"),

  sidebarLayout(
    sidebarPanel("sidebar panel"
                 ),
    mainPanel("main panel", 
              numericInput(inputId = "cl", 
                           h3("Cluster number"), 
                           value = 1, min = 1, max = 2),
              plotOutput("cls_num"),

    )
  )
)

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

  clinput<-reactive({
    b[which(a$tree == input$cl),1:9]
  })

  output$cls_num<-renderPlot({

    clinput()%>%
      pheatmap(cluster_rows = FALSE, cluster_cols = FALSE)
  })
}

# Run the app ----
shinyApp(ui = ui, server = server)

The problem is, when I run the app, it only shows the plot when I resize the window, and after I change the input, I need to resize the window again to see the heatmap. This happens in both rstudio and browser.

Thanks in advance.

phago29
  • 142
  • 1
  • 9

1 Answers1

0

If there are no open graphics devices, dev.off() throws an error. It's safer to use graphics.off().

ecotopic
  • 21
  • 1