Include external js file when Shiny server renders .Rmd file

736 views
Skip to first unread message

Felix Schönbrodt

unread,
Jan 9, 2015, 8:32:31 AM1/9/15
to shiny-...@googlegroups.com
In a Shiny app, I can include an external in ui.R:

includeScript("/includes/iframeResizer.contentWindow.min.js")


Is something similar possible in .Rmd files?
My goal is: When the Shiny server renders a .Rmd file, the js-file should be included.

Thanks,
Felix

Yihui Xie

unread,
Jan 9, 2015, 11:54:48 AM1/9/15
to Felix Schönbrodt, shiny-discuss
Yes, please see the section "Includes" in the rmarkdown documentation:
http://rmarkdown.rstudio.com/html_document_format.html

Regards,
Yihui

Richard Morey

unread,
Sep 27, 2016, 12:55:45 PM9/27/16
to Shiny - Web Framework for R, nice...@gmx.net
I have a whole load of HTML, JS, and CSS collated from various sources that I'd like to include in the document header, so the YAML approach is too constraining. knitr::opts_knit$set( header=X) doesn't work; what's the best approach? 

Richard

Yihui Xie

unread,
Sep 27, 2016, 1:02:29 PM9/27/16
to Richard Morey, Shiny - Web Framework for R, Felix Schönbrodt
What is too constraining about the YAML approach? You can include
arbitrary content in the HTML header via the includes option if you
save the content in a file.

knitr::opts_knit$set( header=X) only works for Rnw and Rhtml
documents. It does not apply to R Markdown.

Regards,
Yihui

Richard Morey

unread,
Sep 27, 2016, 1:09:00 PM9/27/16
to Yihui Xie, Shiny - Web Framework for R, Felix Schönbrodt
Thanks for your email. It's generated at runtime, so there is no file to include until the file is run. Perhaps I'm missing some way of generating the file at run time, then including it, e.g. includes: `r source('functions.R'); saveHeader(); print('filename')`?

I actually tried `r foo()` syntax in the YAML header, didn't seem to work.

Best,
Richard

--
Richard D. Morey
Senior Lecturer
School of Psychology, Cardiff University
http://psych.cf.ac.uk/morey

Yihui Xie

unread,
Sep 27, 2016, 1:50:54 PM9/27/16
to Richard Morey, Shiny - Web Framework for R, Felix Schönbrodt
I think you can use header-includes in YAML, e.g.

output: html_document
header-includes: "`r generate_your_header()`"

Regards,
Yihui

Richard Morey

unread,
Sep 27, 2016, 2:08:04 PM9/27/16
to Yihui Xie, Shiny - Web Framework for R, Felix Schönbrodt
Well, it seems like this works:


####################
---
output: 
  html_document:
    includes:
      in_header: "tmp.html_"
---

`r 'tmp.html_'`
####################

But this does not:

#####################
---
output: 
  html_document:
    includes:
      in_header: "`r 'tmp.html_'`"
---

`r 'tmp.html_'`
#####################

and I get error:

pandoc: `r 'tmp.html_'`: openFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 1
Execution halted

So it seems like it is not interpreting the inline R code as R.

Best,
Richard


Yihui Xie

unread,
Sep 27, 2016, 2:24:36 PM9/27/16
to Richard Morey, Shiny - Web Framework for R, Felix Schönbrodt
The whole output field is read and used before knitr comes in, so `r `
won't work in this field. It works in other fields such as
title/author/...

Regards,
Yihui

Richard Morey

unread,
Sep 27, 2016, 2:33:24 PM9/27/16
to Yihui Xie, Shiny - Web Framework for R, Felix Schönbrodt
ok, so there's no way to do what I want?

Yihui Xie

unread,
Sep 27, 2016, 2:47:10 PM9/27/16
to Richard Morey, Shiny - Web Framework for R
Did you see my reply about header-includes? That was one way.

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages