2

I would love a convenient and easy way to print my word count automatically in quarto and stumbled across this nice add-in from Ben Marwick:

https://github.com/benmarwick/wordcountaddin

It is sound for rmarkdown and I presumed it should be no issue with quarto too. However, when I use the add-in, though it can count out the number of words within my RStudio session, it doesn't print it in my final pdf format and just returns [1] NA.

{r, #wordcountdev, message = FALSE, warning = FALSE, echo = FALSE}

wordstats <- wordcountaddin:::text_stats('CMI Write Up.qmd')  
words <- substr(wordstats[3], start=19, stop=30)
print(words)

I don't understand what is going on here, it is seemingly simple, would anyone know of a better way to achieve what I'm trying?

Richard
  • 21
  • 1

1 Answers1

4

You could take a look at the wordcounts pandoc filter, e.g. as a starting point it prints the number of words in the body to the console while rendering:

---
format: html
filters: [wordcounts.lua]
---

Hello there, how many words are in the body?

enter image description here

Or: You can use the development version (devtools::install_github("benmarwick/wordcountaddin", type = "source", dependencies = TRUE)) of the above mentioned package:

---
format: html
---

```{r}
#| echo: false
#| label: wordstats
#| warning: false
#| message: false
wordcount <- wordcountaddin::text_stats('wordcount.qmd')
words <- substr(wordcount[3], start=19, stop=30)
```

Hello there, how many words are in the body? 
There are `r words` words in the whole document. 

enter image description here

Update: There exist a new quarto filter for wordcounts: https://github.com/andrewheiss/quarto-wordcount

Julian
  • 6,586
  • 2
  • 9
  • 33