估计R Markdown文档的读取时间并将其打印在标题下方

时间:2017-10-07 19:06:44

标签: r r-markdown

系统:Windows 10,RStudio 1.0.153,R 3.4.1

我在R Markdown写博文。我想使用Medium的方法,在标题下方的博客文章中包含估计的阅读时间,因为研究表明including reading time in your blog post increases the likelihood that people will read it all。为了估计阅读时间,我想使用简化版the algorithm used on Medium

  1. 计算文档中单词的数量N,最好不包括YAML前言和带有echo=FALSE的R代码块,即我的访问者看不到的所有文本)
  2. 计算导入图像的数量m和R生成的图:如果计数图使问题过于复杂,则只计算导入的图像是可以的。我使用include_graphics函数在自己的R块中单独导入每个图像,因此即使只计算include_graphics关键字的出现次数也可以。
  3. 估计的分钟时间是

    seconds <- N/200*60+12*m minutes <- round(seconds/60) fin <- ifelse(minutes>1, " minutes", " minute") estimation <- paste0("Estimated reading time: ", minutes, fin)

  4. 每次编写R Markdown文档时,都应执行估算代码,理想情况下,应在R Markdown文档本身的R块内部执行,或者在外部函数内部执行,然后在R块中获取。这样,当我编辑R Markdown文档时,估计将自动更新。我怎样才能做到这一点?例如,给定下面的示例R文档,其中包含N=106个单词和m=2图像,文本

    "Estimated reading time: 1 minute"
    

    应该包含在HTML输出中,就在标题,作者和日期的下方。

    示例R Markdown文档:

    ---
    title: "Testy test"
    author: "anonymous"
    date: "`r Sys.Date()`"
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    library(knitr)
    opts_chunk$set(echo = TRUE)
    ```
    
    ## R Markdown
    
    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    
    When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
    
    ```{r load_data, echo=FALSE}
    include_graphics("doge.jpg")
    ```
    
    ## Including Plots
    
    You can also embed plots, for example:
    
    ```{r pressure, echo=FALSE}
    plot(pressure)
    ```
    
    Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
    

0 个答案:

没有答案