Skip to content

Instantly share code, notes, and snippets.

@vfulco
Forked from m-Py/wordCountRmdFile.R
Created February 2, 2018 11:46
Show Gist options
  • Save vfulco/54a80c20e86f008d9341e80790958eb0 to your computer and use it in GitHub Desktop.
Save vfulco/54a80c20e86f008d9341e80790958eb0 to your computer and use it in GitHub Desktop.

Revisions

  1. @m-Py m-Py revised this gist Feb 9, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions wordCountRmdFile.R
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # this function returns a list, better to process the word count stats
    # 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)
    @@ -11,8 +12,7 @@ text_stats_file <- function(rmdFile) {
    return(wordcountaddin:::text_stats_fn_(conText))
    }

    # this function renders the Rmd file and prints the word count
    # (don't use this if your manuscript consists of several Rmd files)
    # 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
  2. @m-Py m-Py revised this gist Feb 9, 2017. 1 changed file with 8 additions and 18 deletions.
    26 changes: 8 additions & 18 deletions wordCountRmdFile.R
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,20 @@
    text_stats_file <- function(rmdFile) {
    text <- file(rmdFile, "rt")
    text <- readLines(text)
    conText <- ""
    for (i in text) {
    conText <- paste(conText, i)
    }
    # count words - uses an internal function of the wordcountaddin package
    # alternatively use the function text_stats_fn_, which returns a list
    return(wordcountaddin:::text_stats_fn(conText))
    }

    # this function returns a list, better to process the word count stats
    text_stats_file_ <- function(rmdFile) {
    text <- file(rmdFile, "rt")
    text <- readLines(text)
    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 the Rmd file and prints the word count
    # this function renders the Rmd file and prints the word count
    # (don't use this if your manuscript consists of several Rmd files)
    render_and_count <- function(rmdFile) {
    rmarkdown::render(rmdFile)
    n_words <- text_stats_file_(rmdFile)$n_words_korp
    n_words <- text_stats_file(rmdFile)$n_words_korp
    cat("\n\nword count: ", n_words, "\n\n")
    }
    }
  3. @m-Py m-Py revised this gist May 13, 2016. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions wordCountRmdFile.R
    Original file line number Diff line number Diff line change
    @@ -9,3 +9,22 @@ text_stats_file <- function(rmdFile) {
    # alternatively use the function text_stats_fn_, which returns a list
    return(wordcountaddin:::text_stats_fn(conText))
    }

    # this function returns a list, better to process the word count stats
    text_stats_file_ <- function(rmdFile) {
    text <- file(rmdFile, "rt")
    text <- readLines(text)
    conText <- ""
    for (i in text) {
    conText <- paste(conText, i)
    }
    # count words - uses an internal function of the wordcountaddin package
    return(wordcountaddin:::text_stats_fn_(conText))
    }

    # this function renders the 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")
    }
  4. @m-Py m-Py revised this gist May 12, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wordCountRmdFile.R
    Original file line number Diff line number Diff line change
    @@ -6,5 +6,6 @@ text_stats_file <- function(rmdFile) {
    conText <- paste(conText, i)
    }
    # count words - uses an internal function of the wordcountaddin package
    wordcountaddin:::text_stats_fn(conText)
    # alternatively use the function text_stats_fn_, which returns a list
    return(wordcountaddin:::text_stats_fn(conText))
    }
  5. @m-Py m-Py revised this gist May 12, 2016. No changes.
  6. @m-Py m-Py created this gist May 12, 2016.
    10 changes: 10 additions & 0 deletions wordCountRmdFile.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    text_stats_file <- function(rmdFile) {
    text <- file(rmdFile, "rt")
    text <- readLines(text)
    conText <- ""
    for (i in text) {
    conText <- paste(conText, i)
    }
    # count words - uses an internal function of the wordcountaddin package
    wordcountaddin:::text_stats_fn(conText)
    }