ropensci / iheatmapr

Complex, interactive heatmaps in R
https://docs.ropensci.org/iheatmapr
Other
267 stars 36 forks source link

Resize width and height of html file using saveWidget or save_iheatmap #95

Open gsgarlata opened 1 year ago

gsgarlata commented 1 year ago

I would like to save in html a plot that I create using the iheatmap function. I convert the heatmap plot into widget using the to_widget function and save a *.html file with a dynamic plot using the saveWidget R function from the htmlwidgets R package. Below there is a reproducible example. The problem that I encounter is that the size of the figure in the html file is not what I prefer. Thus, how I do specify the width and height of a html file? I tried to define the list of kintr chunck options using (see at the bottom of the question), but it did not work.

#define Classes groups
        names_n = c('Apple','Banana','Orange','Kiwi','Ananas','Avocado','Lemon','Strawberry')
#define number of samples per Class
list_n = c(5,10,20,3,6,7,10,22)

#define Samples types and IDs  
annot_file <- data.frame(Sample=paste0('P',1:4),SampleType=c('Water','Water','No_Water','No_Water'))

#define function to perform random sampling of values
GetRanValues<-function(sample_n, mean_n){
      res = rnorm(n=sample_n,mean=mean_n,sd = 1)
      return(res)
}

#create datframe used for creating the heatmap plot
final_data = NULL

for(u in 1:length(list_n)){

  rep_n=list_n[u]

  tmp_data = data.frame(ID=paste0(names_n[u],1:rep_n),Class = rep(names_n[u],rep_n),
                        P1=GetRanValues(rep_n,mean_n=-12),P2=GetRanValues(rep_n,mean_n=-13),
                        P3=GetRanValues(rep_n,mean_n=-15),P4=GetRanValues(rep_n,mean_n=-14))

  final_data=rbind(final_data,tmp_data)

}

dataExpr = as.matrix(final_data[,-c(1,2)])
rownames(dataExpr) = 1:nrow(dataExpr)

#row annotation# 
row_annotation = as.data.frame(final_data$Class)
names(row_annotation) = 'Class'
#row annotation#

#col annotation##
rownames(annot_file) = annot_file$Sample
annot_file$Sample = NULL
col_annotation = annot_file
names(col_annotation) = " "
#col annotation#

heat_plot = iheatmapr::iheatmap(dataExpr, y=final_data$ID,
                                 row_title = "ID", col_title = "Sample",
                                 cluster_cols = 'none', cluster_rows = 'none',
                                 row_annotation = row_annotation, col_annotation = col_annotation,
                                 scale = "rows")

myknitr_options = knitr::opts_chunk$get()
myknitr_options$out.width = '500px'
myknitr_options$out.height = '1000px'

saveWidget(widget=to_widget(heat_plot), file = 'test_heatmap.html'), knitrOptions=myknitr_options)