Skip to main content
Version: Next

Shiny

Vapour works with Shiny, as it does with any other R package. This example builds a small app that lets the user pick a dataset and a numeric column. It then draws a histogram and a summary table for that column.

Create a directory and place an app.vp file in it.

Types​

We start by defining the types used throughout the app.

type dataset: any

type stats: dataframe {
stat: char,
value: num
}
  • dataset is an alias for any, because the built-in datasets are not all the same shape. The alias still documents what the functions expect.
  • stats is a typed dataframe. It describes the summary table we render.

We also declare the datasets the user can choose from as a constant.

const datasets: char = c("mtcars", "iris", "faithful")

Helpers​

Next we write the logic as small, typed functions. Keeping this logic out of the server function means Vapour can check it.

func get_data(name: char): dataset {
stopifnot(!missing(name))
return get(name, envir = as.environment("package:datasets"))
}

func numeric_columns(df: dataset): char {
stopifnot(!missing(df))
let all: char = names(df)
let keep: bool = vapply(df, is.numeric, logical(1))
return all[keep]
}

func plot_hist(df: dataset, column: char, bins: int): null {
stopifnot(!missing(df), !missing(column), !missing(bins))
hist(
df[[column]],
breaks = bins,
main = column,
xlab = column,
col = "steelblue",
border = "white"
)
return NULL
}

func column_summary(df: dataset, column: char): stats {
stopifnot(!missing(df), !missing(column))
let s: any = summary(df[[column]])
return stats(stat = names(s), value = as.numeric(s))
}

func has_column(df: dataset, column: any): bool {
stopifnot(!missing(df), !missing(column))
return !is.null(column) && column %in% names(df)
}
  • column_summary uses the stats type as a constructor, so the returned data frame has the stat and value columns we declared.
  • has_column types column as any because input$column is NULL until the dynamic input has rendered.
  • Each function uses stopifnot(!missing(...)), so Vapour does not warn that the arguments might be missing.

UI​

The UI is plain Shiny code. We call it with the shiny:: namespace.

let ui: any = shiny::fluidPage(
shiny::titlePanel("Vapour + Shiny"),
shiny::sidebarLayout(
shiny::sidebarPanel(
shiny::selectInput("dataset", "Dataset", choices = datasets),
shiny::uiOutput("column_ui"),
shiny::sliderInput("bins", "Bins", min = 5, max = 50, value = 20)
),
shiny::mainPanel(
shiny::plotOutput("hist"),
shiny::tableOutput("summary")
)
)
)

Server​

The server is a regular Vapour function. It only wires inputs to the typed helpers defined above.

func server(input: any, output: any, session: any): null {
stopifnot(!missing(input), !missing(output), !missing(session))

output$column_ui = shiny::renderUI(
shiny::selectInput("column", "Column", choices = numeric_columns(get_data(input$dataset)))
)

output$hist = shiny::renderPlot(
if (has_column(get_data(input$dataset), input$column)) {
plot_hist(get_data(input$dataset), input$column, input$bins)
}
)

output$summary = shiny::renderTable(
if (has_column(get_data(input$dataset), input$column)) {
column_summary(get_data(input$dataset), input$column)
}
)

return NULL
}

shiny::shinyApp(ui, server)

The column selector depends on the chosen dataset, so it is rendered with renderUI. When the dataset changes, the outputs can briefly see a column from the previous dataset. The has_column check skips rendering until the inputs agree.

Run​

Transpile the app to R.

vapour -infile=app.vp -outfile=app.R

Then run the generated file with Shiny.

shiny::runApp("app.R")