benmarwick / wordcountaddin

Word counts and readability statistics in R markdown documents
Other
337 stars 33 forks source link

word_count() won't work inline in quarto documents #54

Closed silverfoxdoc closed 6 months ago

silverfoxdoc commented 8 months ago

word_count() doesn't currently work with quarto documents when it is used inline, see reprex below. It does however give a word count if the same qmd document is passed to it directly though word_count("reprex.qmd")

Gives this error:

Quitting from lines 11-16 (reprex.qmd)
Error in `text_to_count()`:
! The supplied file has an extension which is not associated with markdown.
  This function only works with markdown or R markdown files.
Backtrace:
 1. wordcountaddin:::word_count()
 2. wordcountaddin:::text_to_count(filename)

Execution halted

Is this fixable?

---
title: "reprex"
format: pdf
---

```{r}
#| label: libraries

library(wordcountaddin)

Word Count: r wordcountaddin:::word_count()

Quarto

Quarto enables you to weave together content and executable code into a finished document.

benmarwick commented 6 months ago

Right, that usage is currently not implemented (it also wont work like that for R Markdown documents). An immediate fix to get that working is to include the path to the file in the function, as you note, like this:

---
title: "reprex"
format: pdf
---

```{r}
#| label: libraries

library(wordcountaddin)

Word Count: r wordcountaddin:::word_count('my_reprex.qmd')

Quarto

Quarto enables you to weave together content and executable code into a finished document.



Which gives this output: 

<img width="461" alt="image" src="https://github.com/benmarwick/wordcountaddin/assets/1262179/d583199c-49ee-47ba-8ad2-d622ae755b76">