Skip to content

Instantly share code, notes, and snippets.

@m-Py
Last active February 2, 2018 11:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m-Py/faf679a0a0be3dbafa2b43b390519923 to your computer and use it in GitHub Desktop.
Save m-Py/faf679a0a0be3dbafa2b43b390519923 to your computer and use it in GitHub Desktop.
Count words in Rmd file using the package wordcountaddin
# This function reads a Rmd file and returns the word count
# It uses the wordcountaddin and koRpus packages
text_stats_file <- function(rmdFile) {
rmd <- file(rmdFile, "rt")
text <- readLines(rmd)
conText <- ""
for (i in text) {
conText <- paste(conText, i)
}
close(rmd)
# count words - uses an internal function of the wordcountaddin package
return(wordcountaddin:::text_stats_fn_(conText))
}
# This function renders a Rmd file and prints the word count
render_and_count <- function(rmdFile) {
rmarkdown::render(rmdFile)
n_words <- text_stats_file(rmdFile)$n_words_korp
cat("\n\nword count: ", n_words, "\n\n")
}
@m-Py
Copy link
Author

m-Py commented May 12, 2016

The function text_stats_file takes an Rmd file as argument and counts the words as implemented in the wordcountaddin package. This works without using RStudio.

The function render_and_count both renders the Rmd file and prints the word count. The koRpus estimate is printed, which seems to yield more precise results.

@benmarwick
Copy link

This is a great idea, thanks for sharing it (I'm the author of wordcountaddin, and came here from crsh/papaja#63). Do you mind if I add these functions to my package, crediting you as the author?

@m-Py
Copy link
Author

m-Py commented May 24, 2016

Hi, of course, just use them! =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment