```{r message=FALSE, echo = FALSE} library(AnalysisPageServer) setup.APS.knitr() ``` # Embedding APS datasets in other documents AnalysisPageServer datasets can be embedded within other (HTML) documents. ```{r results = "hide", echo = FALSE} ## Since the filenames have some randomness, this is just ## a trick to keep the output directory clean if the file ## is knitted more than once. plotdir <- "embed-example" if(file.exists(plotdir)) unlink(dir(plotdir, full = TRUE)) ``` ## Embedding within a knitr document AnalysisPageServer datasets can be embedded within knitr documents such as the one you are reading. To do so, first call `setup.APS.knitr()` to set things up. The main purpose of this call is to include some javascript and CSS that makes the interactivity work. See the documentation of that function for more details: ```{r eval = FALSE} library(AnalysisPageServer) setup.APS.knitr() ``` A few notes about `setup.APS.knitr()`: 1. By default this function makes a table of contents based on your header elements. Turn this off with `include.toc=FALSE`. 1. It also includes some custom styling. This puts the table of contents in the margin on the left and the body of your document to the right of that. For example, this document was created with this default styling. You can control this behavior with the `include.css` argument. 1. It is recommended to use `message=FALSE, echo=FALSE` for this setup block. Whether you use default or custom styling, after your call to `setup.APS.knitr()` at the top of your document you can proceed as you normally would. At the point you want to insert an AnalysisPageServer dataset, make a call to `embed.APS.dataset()`. In typical usage the first argument is a code block wrapped in `{`curly brackets`}` that contains your plotting code. You also have to pass the data. This can either be the return value of the code block, or you can provide it explicity as the second argument: ```{r eval = FALSE} x <- seq(0, 2*pi, length = 100) y <- sin(x) + rnorm(100)/10 col <- adjustcolor(heat.colors(100), alpha.f = 0.6) embed.APS.dataset({ plot(x, y, col = col, pch = 19) }, df = data.frame(x=x, y=y, X=x, Y=y), title = "A shaky sine curve", data.subdir = "embed-example") ``` ```{r echo = FALSE} ## Remember this to show later x <- seq(0, 2*pi, length = 100) y <- sin(x) + rnorm(100)/10 col <- adjustcolor(heat.colors(100), alpha.f = 0.6) div.html <- embed.APS.dataset({ plot(x, y, col = col, pch = 19) }, df = data.frame(x=x, y=y, X=x, Y=y), title = "A shaky sine curve", data.subdir = "embed-example") div.html ``` You can have two datasets on the same page. In the next example we also hide the sidebar and data table with `show.sidebar = FALSE` and `show.table = FALSE`, and pass in a little extra styling to get some margins on the sides. ```{r} x <- rep(1:nrow(volcano), each = ncol(volcano)) y <- rep(1:ncol(volcano), nrow(volcano)) volcano.cells <- data.frame(x = x, y = y, Height = as.vector(t(volcano))) embed.APS.dataset({ image(volcano, xaxt = "n", yaxt = "n", main = "Maunga Whau Volcano") }, df = volcano.cells, title = "", data.subdir = "embed-example", show.table = FALSE, show.sidebar = FALSE, show.xy = TRUE, extra.div.attr = c(style = "width:60%; margin:0 auto")) ``` The `