Skip to content

Instantly share code, notes, and snippets.

@ijlyttle
Created July 21, 2014 00:03
Show Gist options
  • Select an option

  • Save ijlyttle/f1886c3f1a4f62ccc756 to your computer and use it in GitHub Desktop.

Select an option

Save ijlyttle/f1886c3f1a4f62ccc756 to your computer and use it in GitHub Desktop.
Reproducible example (perhaps not minimal) on shiny and rmarkdown
---
title: Nested Widgets
runtime: shiny
output: html_document
---
## Introduction
I seem to be able to something really cool using shiny, interactively, on my computer, but I cannot get it to work using shiny within an rmarkdown document run from shiny.
I don't know if I am able to do something I shouldn't in shiny, or if there is something that could be added to rmarkdown or shiny.
```{r load_libraries}
library(shiny)
```
## Widgets
Consider a couple of widgets that each take a dataframe as an arguement.
### Contents widget
```{r wgt_head}
wgt_head <- function(dataset){
shinyApp(
ui = pageWithSidebar(
headerPanel("Contents"),
sidebarPanel(
checkboxInput(
"print_bool",
label = "Display head of dataset",
value = TRUE
)
),
mainPanel(
conditionalPanel(
condition = "input.print_bool",
verbatimTextOutput("contents")
)
)
),
server = function(input, output, session){
output$contents <- renderPrint(head(dataset))
}
)
}
```
```{r run_wiget_head}
wgt_head(mtcars)
```
### Summary widget
```{r wgt_summary}
wgt_summary <- function(dataset){
shinyApp(
ui = pageWithSidebar(
headerPanel("Summary"),
sidebarPanel(
checkboxInput(
"print_bool",
label = "Display summary of dataset",
value = TRUE
)
),
mainPanel(
conditionalPanel(
condition = "input.print_bool",
verbatimTextOutput("contents")
)
)
),
server = function(input, output, session){
output$contents <- renderPrint(summary(dataset))
}
)
}
```
```{r run_wiget_summary}
wgt_summary(mtcars)
```
## Combining widgets
This widget seems to work if I invoke `wgt_combine(mtcars)` from the command line, but it does not work if I invoke `rmarkdown::run("shiny_embedded_widgets.Rmd")`.
Again, I don't know if I am able to do something from the command line that I shouldn't or if there is something not-yet implemented in `rmarkdown::run`.
```{r wgt_combine}
wgt_combine <- function(dataset){
shinyApp(
ui = navbarPage(
title = "combined app",
tabPanel("Head", wgt_head(dataset)),
tabPanel("Summary", wgt_summary(dataset))
),
server = function(input, output, session){}
)
}
```
```{r run_widget_combine}
wgt_combine(mtcars)
```
## Further Work
At some point, I need to sit down with colleagues and coffee to see how this can be done by having arguments to widget functions be reactives (returning dataframes), rather than dataframes. ggvis pulls off this trick.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment