Rmarkdown中代码块折叠

在写文章的某些时候,并不希望读者一下子就看到接下来的内容,先藏起来。比如,我们提了一个问题,希望读者脑海里先闪过思考的瞬间。这时候可以考虑把答案部分先折叠起来。真好,Rmarkdown是有这个功能的,虽然是借助HTML下实现的。

很简单,借助details 属性就可以了:

<details>
  <summary>Toggle answer</summary>
# ```{r mes}
  message("You can  try sUmmary : ")
  summary(cars)
# ```
</details>

结果如下

可以看到折叠区于不折叠的markdown语法并无二致,均是可以执行的。需要主要的是折叠区不要放标题##,默认标题是不折叠的。


<script>
document.addEventListener("click", function (event) {
    if (event.target.classList.contains("aaron-collapse")) {
        event.target.classList.toggle("active");
        var content = event.target.nextElementSibling;
        if (content.style.display === "block") {
            content.style.display = "none";
        } else {
            content.style.display = "block";
        }
    }
})
</script>

<style>
.aaron-collapse {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
}

.aaron-content {
  padding: 0 18px;
  display: none;
  overflow: hidden;
  background-color: #f1f1f1;
}
</style>


<button class="aaron-collapse">View session info</button>
<div class="aaron-content">
\\\```
R version 4.0.0 Patched (2020-05-01 r78341)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS:   /home/luna/Software/R/R-4-0-branch-dev/lib/libRblas.so
LAPACK: /home/luna/Software/R/R-4-0-branch-dev/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] scater_1.17.3               ggplot2_3.3.2              
 [3] AnnotationHub_2.21.1        BiocFileCache_1.13.0       
 [5] dbplyr_1.4.4                SingleR_1.3.6              
 [7] celldex_0.99.1              ensembldb_2.13.1           
 [9] AnnotationFilter_1.13.0     GenomicFeatures_1.41.0     
[11] AnnotationDbi_1.51.1        scRNAseq_2.3.6             
[13] SingleCellExperiment_1.11.6 SummarizedExperiment_1.19.5
[15] DelayedArray_0.15.6         matrixStats_0.56.0         
[17] Matrix_1.2-18               Biobase_2.49.0             
[19] GenomicRanges_1.41.5        GenomeInfoDb_1.25.5        
[21] IRanges_2.23.10             S4Vectors_0.27.12          
[23] BiocGenerics_0.35.4         BiocStyle_2.17.0           
[25] rebook_0.99.0              

loaded via a namespace (and not attached):
 [1] ggbeeswarm_0.6.0              colorspace_1.4-1             
 [3] ellipsis_0.3.1                scuttle_0.99.10              
 [5] XVector_0.29.3                BiocNeighbors_1.7.0          
 [7] bit64_0.9-7                   interactiveDisplayBase_1.27.5
 [9] codetools_0.2-16              knitr_1.29                   
[11] Rsamtools_2.5.3               graph_1.67.1                 
[13] shiny_1.5.0                   BiocManager_1.30.10          
[15] compiler_4.0.0                httr_1.4.1                   
[17] assertthat_0.2.1              fastmap_1.0.1                
[19] lazyeval_0.2.2                later_1.1.0.1                
[21] BiocSingular_1.5.0            htmltools_0.5.0              
[23] prettyunits_1.1.1             tools_4.0.0                  
[25] rsvd_1.0.3                    gtable_0.3.0                 
[27] glue_1.4.1                    GenomeInfoDbData_1.2.3       
[29] dplyr_1.0.0                   rappdirs_0.3.1               
[31] Rcpp_1.0.4.6                  vctrs_0.3.1                  
[33] Biostrings_2.57.2             ExperimentHub_1.15.0         
[35] rtracklayer_1.49.3            DelayedMatrixStats_1.11.1    
[37] xfun_0.15                     stringr_1.4.0                
[39] ps_1.3.3                      mime_0.9                     
[41] lifecycle_0.2.0               irlba_2.3.3                  
[43] XML_3.99-0.3                  zlibbioc_1.35.0              
[45] scales_1.1.1                  hms_0.5.3                    
[47] promises_1.1.1                ProtGenerics_1.21.0          
[49] yaml_2.2.1                    curl_4.3                     
[51] memoise_1.1.0                 gridExtra_2.3                
[53] biomaRt_2.45.1                stringi_1.4.6                
[55] RSQLite_2.2.0                 BiocVersion_3.12.0           
[57] BiocParallel_1.23.0           rlang_0.4.6                  
[59] pkgconfig_2.0.3               bitops_1.0-6                 
[61] evaluate_0.14                 lattice_0.20-41              
[63] purrr_0.3.4                   GenomicAlignments_1.25.3     
[65] CodeDepends_0.6.5             bit_1.1-15.2                 
[67] processx_3.4.2                tidyselect_1.1.0             
[69] magrittr_1.5                  bookdown_0.20                
[71] R6_2.4.1                      generics_0.0.2               
[73] DBI_1.1.0                     pillar_1.4.4                 
[75] withr_2.2.0                   RCurl_1.98-1.2               
[77] tibble_3.0.1                  crayon_1.3.4                 
[79] rmarkdown_2.3                 viridis_0.5.1                
[81] progress_1.2.2                grid_4.0.0                   
[83] blob_1.2.1                    callr_3.4.3                  
[85] digest_0.6.25                 xtable_1.8-4                 
[87] httpuv_1.5.4                  openssl_1.4.2                
[89] munsell_0.5.0                 viridisLite_0.3.0            
[91] beeswarm_0.2.3                vipor_0.4.5                  
[93] askpass_1.1                  
\\\```
</div>




参考:

R Markdown中单个块的代码折叠?
第12节 Markdown自定义折叠区

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天晚上,我说:我要做饭。于是到了晚上爸爸让我做疙瘩汤。一开始我是一个人做疙瘩汤,我先在厨房捎了一个土豆,这时爸爸...
    李鹤轩1阅读 360评论 1 9
  • 你都明白,离开水的鱼最可怜。 可是那一刻,你相信,你是她眼中最特别的鱼,和那群不一样。 “就要那只”听着声,身子一...
    冬迪阅读 123评论 0 0
  • #自控力School打卡作业5.16# 选择一位采访对象,用我们提到的两种方法之一,写一份采访提纲,不少于9个问题...
    洋葱哥张乔阅读 317评论 1 6
  • 说过要做的事不多,但也有那么几件是想做并且认为非做不可的。摄影,游泳,写稿……还有学好专业课,做做兼职,多交几...
    久醉阅读 240评论 0 1