-
-
Save vfulco/54a80c20e86f008d9341e80790958eb0 to your computer and use it in GitHub Desktop.
Count words in Rmd file using the package wordcountaddin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment