Separating dependencies from RMarkdown Flexdashboad HTML output

I have a script that creates nested html output (using flexdashboard) files and each file is ~ 8MB. I think it should be possible to reduce the file size by storing the dependencies/formatting in a separate folder but I am not succeeding. I am specifying the following inside the rmarkdown::render call but I'm not seeing the "lib" folder once the script runs. Is there another way to reduce the output file size?

output_options = list(html_document = 
                    list(self_contained = FALSE, 
                         lib_dir = "lib"))

I am not sure output_options take the format name. I think it should only be a list of options per the doc

output_options = list(self_contained = FALSE, lib_dir = "lib")

Did you try that already ?

Yes, I tried that. Though the script runs, the "lib" folder is not populated (even when I pre-create it in the working directory). I'm not very familiar with js/HTML, what types of files should I expect in the "lib" folder?

Ah, maybe the dependencies need to be put into the "lib" folder first and then when .Rmd file is rendered it reads the dependencies in from the "lib" folder and so the resulting html files can be smaller in size? I don't really know how html rendering/dependencies work so that's contributing to the issue here.

What exactly are you trying to do ? I think your use case makes it specific.

This small example works and show that a lib folder is created to contain all the js library required. It will be populated at rendering.

title: "Title"
    self_contained: false
    lib_dir: "lib"


So sorry for the delay. I am trying to reduce the size of the html output files (right now my files are ~ 8mb each). I thought that if I put the dependencies in a separate "lib" folder, the actual html files would be smaller? The example you provide above works fine and does generate the "lib" folder. So I'll continue troubleshooting my script to see if it's the render call that's not working or if something else is preventing the "lib" folder from populating with all the dependencies.

1 Like

Circling back to this: I did get it to work eventually (though it only seems to work when the logo/theme/orientation are defined in YAML, not through the rmarkdown::render call). I now have a separate "lib" folder with all the dependencies and the resulting HTML file is 0.5 mg instead of 8mb, which is a win.

However, for my purposes it is inconvenient to have to have a local "lib" folder for every output file (it's complex nested structure). Is it possible to store dependencies externally?

For example, in the resulting html file, there's a call:

<link href="lib/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="lib/bootstrap-3.3.5/js/bootstrap.min.js"></script>

I'd like for it to instead refer to the following, but without having to manually change it for every file:

<link rel="stylesheet" href="" integrity="sha384-pdapHxIh7EYuwy6K7iE41uXVxGCXY0sAjBzaElYGJUrzwodck3Lx6IE2lA0rFREo" crossorigin="anonymous">

<script src="" integrity="sha384-pPttEvTHTuUJ9L2kCoMnNqCRcaMPMVMsWVO+RLaaaYDmfSP5//dP6eKRusbPcqhZ" crossorigin="anonymous"></script>

Do you know how I can use external links for dependencies? Many, many thanks.

Those call are inserted for the them by the default template. It is part of html_document format.

I think if you want to really customize the output with such things, you would need to not use a theme built in rmarkdown, or even change the default html template provided by rmarkdown tp use your own. For the former, that would mean setting theme = NULL to pass yourself the CSS you want, and the latter use template to provide a pandoc template you would create with the call you want inside.

Some resources:

The first one may be just what you need.

You also may found interesting recipe in the cookbook:

Hope it helps.

Thank you, this is really helpful. Will take a look at the resources you listed. Thanks again!

1 Like

One other follow up, if I may: does it matter that I am creating not just html_document format but flexdashboard pages? I have a sample call below (with output_options commented out), are you saying I could specify the theme to null and just update the css to have all the proper formatting? And the css would be specifying links to external dependencies? Thank you again so much for your help.

(by the way, for defining output_options as self_contained = FALSE to work, I couldn't call flexdashboard from the render function, and instead had to define it in the YAML in the Rmd file. Not sure if that's a feature or a bug).

                                 theme= "cosmo",
                                 orientation = "rows",
                                 css= "styles.css"),
                  #output_options = list(self_contained = FALSE, lib_dir = "lib"),
                  output_file = html_district_1,
                  params = list(file_path = data, set_title = paste0(district_name, " District, ", version), 
                                student_data = student_data_var, includeT2 = includeT2, version = version))

With flexdashboard it won't be as easy because the format is doing a lot of stuff for you so if you deactivate you may loose some feature.

Another way to modify your resuting HTML would be to post process the output with R or JS to change the link you want

I don't understand: flex_dashboard as a self_contained argument that you could set to FALSE if you want.

Another way to modify your resuting HTML would be to post process the output with R or JS to change the link you want

Thank you, makes sense.

I don't understand: flex_dashboard as a self_contained argument that you could set to FALSE if you want.

Aha! I didn't realize that was an argument inside flex_dashboard. Got that to work -- thank you!!

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.