在 R 中的 saveWidget 中调整 html

问题描述 投票:0回答:0

我想在 html 中保存我使用

iheatmapr
R 包和
iheatmap
函数创建的图。我使用
to_widget
R 包的
iheatmapr
将热图图转换为小部件,并使用
saveWidget
R 包中的
htmlwidgets
R 函数保存一个带有动态图的 *.html 文件。下面是一个可重现的例子。 我遇到的问题是 html 文件中图形的大小不是我喜欢的。因此,我如何指定 html 文件的宽度和高度?我尝试使用(请参阅问题底部)定义 kintr chunck 选项列表,但它没有用。

#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)
r knitr htmlwidgets
© www.soinside.com 2019 - 2024. All rights reserved.