# Want to embed data or source files in HTML output? No problem!

### Yihui Xie / 2018-07-11

Sometimes it is even hard for myself to believe how long it would take for me to implement a simple feature. Last year, when I saw Steven Fazzio’s blogdown feature request on Github (to embed the Rmd source file in the HTML output so readers can download it), I had an idea how to implement it. I knew it wouldn’t be hard.

Actually two years ago, Noam Ross sent a pull request to the rmarkdown repo, which was related to Steven’s request. I felt the implementation could be easier.

## xfun::embed_file()

While deciding the issues/features for the next release of rmarkdown, I revisited the rmarkdown PR and the blogdown issue. At one late night, I finally typed out the two lines of code to implement it in the xfun package, so now you can embed an arbitrary file, or multiple files, or a whole directory, in the HTML output of an R Markdown document. For now you need the development version of xfun:

remotes::install_github('yihui/xfun')
# embed_file() requires a few more packages

Basically, you can use one of the functions xfun::embed_file(), xfun::embed_dir(), or xfun::embed_files() in an R code chunk to embed one or multiple files or an entire directory in the HTML output, e.g.,

{r echo=FALSE}
# a single file
xfun::embed_file('source.Rmd')

# multiple files
xfun::embed_files(c('source.Rmd', 'data.csv'))

# a directory


You can also provide the list of files programmatically, e.g.,

# embed all Rmd and csv files
xfun::embed_files(list.files('.', '[.](Rmd|csv)\$'))

For multiple files, they are first compressed to a zip file, and the zip file will be embedded. These functions return a link, which a reader can click on the HTML page to download the embedded file.