RObservations #47 : Knitting .Rmd Files to Google Docs with knit2docs

[This article was first published on r – bensstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

RMarkdown is a powerful framework for writing a documents that contain a mixture of text, code, and the output of the code. Popular output formats for RMarkdown Documents (.Rmd) include HTML, PDF and Word Documents. It is also possible to output RMarkdown documents as part of a static website using blogdown package and is (still!) possible to publish RMarkdown documents to WordPress sites as well (like this one)!

Recently, I started to look into the possibility of outputting an .Rmd file as a Google Doc, but I was unable to locate any out-of-box solutions. After looking into the issue I developed a small function that makes it possible!

In this blog I share a function that enables users to write an .Rmd file as a Google Doc with the rmarkdown and googledrive packages.

The Code (so far)

The code below essentially takes a the .Rmd file, renders it to a word document and uploads the word document to Google Drive as a Google Doc:

library(googledrive)
library(rmarkdown)


knit2docs<- function(rmd_file, doc_name=".Rmd to Docs"){
  # Temporary File
  temp_file<- tempfile(fileext = ".docx")
  rmarkdown::render(rmd_file, "word_document", output_file = temp_file)
  # Write to docs
  drive_upload(temp_file, 
               name = doc_name,
               type = "application/vnd.google-apps.document")
}

Now lets try this code out with an example .Rmd file:

knit2docs("Test.Rmd" ,doc_name = ".Rmd to Docs")

If we check the output on Google Docs, we can see the output:

Next steps

In the present form, the the knit2docs function does what its supposed to, but it does have its limitations:

  1. In its present form, the code is creates a new Google Doc each time the the function is called. It would be helpful to link the .Rmd file to a Google Doc so that each time it would be knit2docs() would be called, it would update a linked Doc (should one be specified).
  2. With the above code, the rendered Google Doc is sent to the drive at the top level. It would be helpful to specify the drive location for the file being uploaded.
  3. The exported formatting for fonts is default to Microsoft fonts. It would be nice to be able to update these fonts to Google fonts. Its could be that this is something that needs to be specified in the YAML header of the .Rmd file – I’ll have to look into it to see what exactly needs to be done.

To make this function readily available and develop it further, I created a Github Repository of the packaged code here. Add this code to your workflow by installing the knit2docs package! If you notice anything else or want to get involved in development, feel free to reach out!

Conclusion

Knitting .Rmd files to Google Docs is something which is definitely intriguing. I’m surprised that I wasn’t able to find any readily developed solutions that were already made, but I’m happy that I have something basic put together so far! Its my hope that I can get this code and the next steps put together in a fully developed package and hopefully make knitting .Rmd files to Google Docs a part of rmarkdown users’ regular workflow!

Feel free to get involved by checking out the knit2docs repository here and be sure to give it a star for exposure!

Thank you for reading!

Want to see more of my content?

Be sure to subscribe and never miss an update!

To leave a comment for the author, please follow the link and comment on their blog: r – bensstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)