
---
output:
  html_document

params:
  data: NA
  settings: NA
  charts: NA
  
---

## Customized Interactive Safety Graphics {.tabset .tabset-fade} 

```{r results='asis', echo = FALSE, message=FALSE, warning = FALSE}
library(safetyGraphics)
library(knitr)

subchunkify_title <- function(chart) {
  sub_chunk <- paste0("### ",chart,"\n")
  cat(sub_chunk)
}

subchunkify_chart <- function(chart,settings, fig_height=15, fig_width=12) {
  g_deparsed <- paste0(deparse(     
    function() {chartRenderer(data = params$data,
      settings = settings, chart=chart)}
  ), collapse = '')

  sub_chunk <- paste0("
  `","``{r sub_chunk_", floor(runif(1) * 10000), ", fig.height=",
   fig_height, ", fig.width=", fig_width, ", echo=FALSE, message=FALSE, warning=FALSE}",
  "\n(", 
    g_deparsed
    , ")()",
  "\n`","``
  ",'\n')

  cat(knitr::knit(text = knitr::knit_expand(text = sub_chunk), quiet = TRUE))
}

for (chart in params$charts) {
  chart_settings <- trimSettings(settings=params$settings, charts=chart)
  subchunkify_title(chart)
  subchunkify_chart(chart, chart_settings)
}

```



### Info

#### Background
The safetyGraphics package provides a framework for evaluation of clinical trial safety in R. Examples and additional documentation are available [here](https://github.com/ASA-DIA-InteractiveSafetyGraphics/safetyGraphics).

safetyGraphics is an open source project built using standard web technology and will run in any modern web browser. The displays created are all dynamically linked to raw safety data which allows the tool to work with data from any safety system. The tool was originally created using Javascript/D3, but has been extended to an R tool as well using HTML Widgets.  
  
<br>

#### Code to Reproduce Charts

Use the script below to load the safetyGraphics package and your data. Make sure to update the second line to point at your data. 

```{r, comment=NA, echo=FALSE}
lib_code <- quote(library(safetyGraphics))
lib_code

writeLines("path <- 'path_to_data'") ### <-- Update this!
writeLines("my_data <- read.csv(file.path(path, 'data.csv'))")

```

```{r results='asis', echo = FALSE, message=FALSE, warning = FALSE}

subchunkify_header <- function(chart){
  g_deparsed <- paste0("**Code to render ",chart," chart**\n")
      
  cat(g_deparsed)
}

subchunkify_code <- function(chart,settings){
  g_deparsed <- paste0(deparse(         
    function() {
      bquote(chartRenderer(data = my_data,
      settings = .(settings), chart=.(chart)))
    }
  ), collapse = '')

  sub_chunk <- paste0("
  `","``{r sub_chunk_", floor(runif(1) * 10000),", comment = NA",", echo=FALSE}",
  "\n(", 
    g_deparsed
    , ")()",
  "\n`","``
  ",'\n')

  cat(knitr::knit(text = knitr::knit_expand(text = sub_chunk), quiet = TRUE))
}

for (chart in params$charts) {
  chart_settings <- trimSettings(settings=params$settings, charts=chart)
  subchunkify_header(chart)
  subchunkify_code(chart, chart_settings)
}

```

