---
title: "flexdashboard: Shiny Embedding"
output: 
  flexdashboard::flex_dashboard:
    social: menu
    source_code: embed 
runtime: shiny
---

```{r global, include=FALSE}
# load data in 'global' chunk so it can be shared by all users of the dashboard
library(datasets)
data(WorldPhones)
```

Module
=======================================================================

### Embedding a Shiny Module

```{r, include=FALSE}
# Shiny module definition (would typically be defined in a separate R script)

# UI function
worldPhonesUI <- function(id) {
  ns <- NS(id)
  fillCol(height = 600, flex = c(NA, 1), 
    inputPanel(
      selectInput(ns("region"), "Region:", choices = colnames(WorldPhones))
    ),
    plotOutput(ns("phonePlot"), height = "100%")
  )
}

# Server function
worldPhones <- function(input, output, session) {
  output$phonePlot <- renderPlot({
    barplot(WorldPhones[,input$region]*1000, 
            ylab = "Number of Telephones", xlab = "Year")
  })
}
```

```{r}
# Include the module
worldPhonesUI("phones")
callModule(worldPhones, "phones")
```

Inline App
=======================================================================

### Inline Shiny App via shinyApp

```{r}
shinyApp(
  ui = fillPage(
    fillCol(flex = c(NA, 1), 
      inputPanel(
        selectInput("region", "Region:", choices = colnames(WorldPhones))
      ),
      plotOutput("phonePlot", height = "100%")
    )
  ),
  server = function(input, output) {
    output$phonePlot <- renderPlot({
      barplot(WorldPhones[,input$region]*1000, 
              ylab = "Number of Telephones", xlab = "Year")
    })
  },
  options = list(height = 600)
)
```

External App
=======================================================================

### External Shiny App via shinyAppDir

```{r}
shinyAppDir(
  system.file("examples/06_tabsets", package="shiny"),
  options = list(height=850)
)
```

