Skip to contents

Use esquisse as a module in a Shiny application.

Usage

esquisse_ui(
  id,
  header = esquisse_header(),
  container = esquisse_container(),
  controls = c("options", "labs", "axes", "geoms", "theme", "filters", "code"),
  insert_code = FALSE,
  play_pause = TRUE,
  layout_sidebar = FALSE,
  downloads = downloads_labels(),
  n_geoms = 8
)

esquisse_server(
  id,
  data_rv = NULL,
  name = "data",
  default_aes = c("fill", "color", "size", "group", "facet"),
  import_from = c("env", "file", "copypaste", "googlesheets", "url"),
  n_geoms = 8,
  drop_ids = TRUE,
  notify_warnings = NULL
)

esquisse_container(width = "100%", height = "700px", fixed = FALSE)

esquisse_header(
  import_data = TRUE,
  show_data = TRUE,
  update_variable = TRUE,
  create_column = TRUE,
  cut_variable = TRUE,
  settings = TRUE,
  close = TRUE
)

Arguments

id

Module ID.

header

Either TRUE or FALSE to display or not esquisse header, or a named list where names are : settings, close, import and show_data and values are TRUE or FALSE to display or not the corresponding button.

container

Container in which display the addin, default is to use esquisse_container(), see examples. Use NULL for no container (behavior in versions <= 0.2.1). Must be a function.

controls

Controls menu to be displayed. Use NULL to hide all menus.

insert_code

Logical, Display or not a button to insert the ggplot code in the current user script (work only in RStudio).

play_pause

Display or not the play / pause button.

layout_sidebar

Put controls in a sidebar on the left rather than below the chart in dropdowns.

downloads

Export options available or NULL for no export. See downloads_labels().

n_geoms

Number of geoms the user can use.

data_rv

Either:

  • A shiny::reactiveValues() with a slot data containing a data.frame to use in the module and a slot name corresponding to the name of the data.frame used for the generated code.

  • A shiny::reactive() function returning a data.frame. See argument name for the name used in generated code.

  • A data.frame object.

name

The default name to use in generated code. Can be a reactive function return a single character.

default_aes

Default aesthetics to be used, can be a character vector or reactive function returning one.

import_from

From where to import data, argument passed to datamods::import_server(), use NULL to prevent the modal to appear.

drop_ids

Argument passed to datamods::filter_data_server. Drop columns containing more than 90% of unique values, or than 50 distinct values.

notify_warnings

See safe_ggplot(). If NULL, the user can make his or her own choice via the settings menu, default is to show warnings once.

width, height

The width and height of the container, e.g. "400px", or "100%"; see validateCssUnit.

fixed

Use a fixed container, e.g. to use use esquisse full page. If TRUE, width and height are ignored. Default to FALSE. It's possible to use a vector of CSS unit of length 4 to specify the margins (top, right, bottom, left).

import_data

Show button to import data.

show_data

Show button to display data.

update_variable

Show button to update selected variables and convert them.

create_column

Show button to create a new column based on an expression.

cut_variable

Show button to allow to convert a numeric variable into factors.

settings

Show button to open settings modal (to select aesthetics to use).

close

Show button to stop the app and close addin.

Value

A reactiveValues with 3 slots :

  • code_plot : code to generate plot.

  • code_filters : a list of length two with code to reproduce filters.

  • data : data.frame used in plot (with filters applied).

Examples


### Part of a Shiny app ###

library(shiny)
library(esquisse)

ui <- fluidPage(

  theme = bs_theme_esquisse(),

  tags$h1("Use esquisse as a Shiny module"),

  radioButtons(
    inputId = "data",
    label = "Data to use:",
    choices = c("iris", "mtcars"),
    inline = TRUE
  ),
  checkboxGroupInput(
    inputId = "aes",
    label = "Aesthetics to use:",
    choices = c(
      "fill", "color", "size", "shape",
      "weight", "group", "facet", "facet_row", "facet_col"
    ),
    selected = c("fill", "color", "size", "facet"),
    inline = TRUE
  ),
  esquisse_ui(
    id = "esquisse",
    header = FALSE, # dont display gadget title
    container = esquisse_container(height = "700px")
  )
)
#> Loading required namespace: plotly

server <- function(input, output, session) {

  data_rv <- reactiveValues(data = iris, name = "iris")

  observeEvent(input$data, {
    if (input$data == "iris") {
      data_rv$data <- iris
      data_rv$name <- "iris"
    } else {
      data_rv$data <- mtcars
      data_rv$name <- "mtcars"
    }
  })

  esquisse_server(
    id = "esquisse",
    data_rv = data_rv,
    default_aes = reactive(input$aes)
  )

}

if (interactive())
  shinyApp(ui, server)


### Whole Shiny app ###

library(shiny)
library(esquisse)


# Load some datasets in app environment
my_data <- data.frame(
  var1 = rnorm(100),
  var2 = sample(letters[1:5], 100, TRUE)
)


ui <- fluidPage(
  theme = bs_theme_esquisse(),
  esquisse_ui(
    id = "esquisse",
    header = esquisse_header(close = FALSE), # hide the close button
    container = esquisse_container(fixed = TRUE),
    play_pause = FALSE,
    controls = c("settings", "labs", "axes", "geoms", "theme", "filters", "code", "export"),
    layout_sidebar = TRUE
  )
)

server <- function(input, output, session) {

  esquisse_server(id = "esquisse")

}

if (interactive())
  shinyApp(ui, server)



## You can also use a vector of margins for the fixed argument,
# useful if you have a navbar for example

library(shiny)
library(esquisse)
library(datamods)

ui <- navbarPage(
  title = "My navbar app",
  theme = bs_theme_esquisse(),
  tabPanel(
    title = "esquisse",
    esquisse_ui(
      id = "esquisse", 
      header = FALSE,
      container = esquisse_container(
        fixed = c(55, 0, 0, 0)
      )
    )
  )
)

server <- function(input, output, session) {
  
  # lauch import data modal
  import_modal(
    id = "import-data",
    from = c("env", "file", "copypaste"),
    title = "Import data"
  )
  data_imported_r <- datamods::import_server("import-data")

  data_rv <- reactiveValues(data = data.frame())
  observeEvent(data_imported_r$data(), {
    data_rv$data <- data_imported_r$data()
    data_rv$name <- data_imported_r$name()
  })
  
  esquisse_server(id = "esquisse", data_rv = data_rv)
  
}

if (interactive())
  shinyApp(ui, server)