Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Created October 26, 2019 23:35
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosiahParry/1f76cb7f2aab934b96359110a687d334 to your computer and use it in GitHub Desktop.
Save JosiahParry/1f76cb7f2aab934b96359110a687d334 to your computer and use it in GitHub Desktop.
Convert a Google Doc to an R Markdown Doc
library(googledrive)
# authenticate yourself
drive_auth()
gdoc_to_rmd <- function(drive_id, output, verbose = TRUE, overwrite = FALSE) {
# get the doc
doc <- drive_get(as_id(drive_id))
# specify tempdir location for writing
fp <- glue::glue("{tempdir()}/{doc[[1]]}.docx")
# create var with non-conflicting name
out <- output
# download to tempdir
drive_download(doc, fp, overwrite = overwrite)
# convert to markdown
rmarkdown::pandoc_convert(
fp,
"markdown",
output = out
)
# cat to console if verbose
if (verbose) {
cat(readLines(out), sep = "\n")
}
# return lines as character string if so desired
invisible(readLines(out))
}
drive_id <- "1tqJCbEhU38iSl1QFaO3jLliw8ulbgdYN15peNXkOt6Q"
example_doc <- gdoc_to_rmd(drive_id,
verbose = TRUE,
overwrite = TRUE,
output = "~/desktop/x.Rmd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment