Created
October 26, 2019 23:35
-
-
Save JosiahParry/1f76cb7f2aab934b96359110a687d334 to your computer and use it in GitHub Desktop.
Convert a Google Doc to an R Markdown Doc
This file contains 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
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