Best practice when using both R Markdown and Shiny?

I read this (very interesting) conversation and I think I will use a unique .Rmd file where I will put all my redaction and code (even to do the data cleaning). But that topic only focused on R Markdown.

If I want to write an article / a report and then create a Shiny app related to that report where users can modify data, graphs, correlation tables, etc., what would the best practice be? It could be a good idea to make three different files:

  • a .R document where all the code to tidy data, make linear regressions, etc. is made
  • a .Rmd document where you only have text and the necessary code to make plots or tables
  • another .R document containing the whole Shiny app.

What do you think about this?

I know that you can create a Markdown file that contains interactive Shiny items, but that is not what I want.


Interesting topic!
I don't think there is one perfect workflow for this, but here are some of my thoughts:

  • Markdown document: This is a great way to explain in plain text what you did and show the results in the form of plots and tables while providing the code that generated it all. The best thing about the Rmd is that the reader does not have to install R or run any scripts. It's also easy to distribute and suitable for all types of end-users (no skills required to open a PDF)
  • Shiny app: This is a great addition to a mainly static report (i.e. Markdown or other text document) where you can give your audience easy access to data and results and allow them to explore them in detail. You should consider running the app on an online server if your audience is not familiar with R (or coding in general) or if you like to keep your data / code more private. Providing the full app and data as files for download is handy when you have complex calculations or large files that are more suited for running locally, or when you want to give them the possibility of modifying the app for own use.
  • Plain R-files: These files will usually only be of use for experienced coders or people who need in-depth knowledge of the scrips or modify them (e.g. students in class). There are many caveats with these type of files like readability (need good comments), requirement of additional packages or data, or correct assignment of each variable (sometimes hard to find which ones should be set by the user and to what values).

I know it's general, but I hope it helps you in looking for the different uses of these files.



Here are few suggestions for you to use as you see fit.

  • Put all the data processing code in a R script. Create an "init" R script where you can add all the functions, variable you need. You can then source it from either the RMardown or Shiny app. Global.R is a good place for that in a shiny app.

  • If your article / report is HTML only, and not too long, you should have a look at the learnR package. It is quite handy in term of mixing Rmarkdown and Shiny component.

  • if you need PDF output, then bookdown is also a good extension. You can add link to live shiny app for the HTML version.



You're right, it is much easier for users if the shiny app is deployed online. I don't want to provide the code for the shiny app because I think that readers don't need it, but I would provide them the plain R-files and the markdown document. This segmentation is not as easy as having everything in a single markdown document but, to me, it is the simplest if you want to add a shiny app

thanks, I didn't know these packages but since I produce articles in PDF which are printed (most of the time), links to live shiny app are not useful. However, I am thinking about writing the link to a shiny app deployed online in my articles so that they know where to find it

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.