Easily Create Pretty Popup Messages (Modals) in 'Shiny'

Easily create pretty popup messages (modals) in 'Shiny'. A modal can contain text, images, OK/Cancel buttons, an input to get a response from the user, and many more customizable options.


shinyalert

shinyalert

Easily create pretty popup messages (modals) in Shiny

Demo ยท Copyright 2018 Dean Attali

Donate Build Status CRAN version


shinyalert lets you easily create pretty popup messages (modals) in Shiny.

A modal can contain text, images, OK/Cancel buttons, an input to get a response from the user, and many more customizable options. The value of the modal can be retrieved in Shiny using an input or using callback functions. See the demo Shiny app online for examples.

If you need any Shiny help, I'm available for consulting. If you find shinyalert useful, please consider supporting my efforts developing open-source R packages!

Overview

shinyalert uses the sweetalert JavaScript library to create simple and elegant popups (modals) in Shiny.

Simply call shinyalert() with the desired arguments, such as a title and text, and a modal will show up. In order to be able to call shinyalert() in a Shiny app, you must first call useShinyalert() anywhere in the app's UI.

basic modal

Here is some minimal Shiny app code that creates the above modal:

library(shiny)
library(shinyalert)

ui <- fluidPage(
  useShinyalert(),  # Set up shinyalert
  actionButton("preview", "Preview")
)

server <- function(input, output, session) {
  observeEvent(input$preview, {
    # Show a modal when the button is pressed
    shinyalert("Oops!", "Something went wrong.", type = "error")
  })
}

shinyApp(ui, server)

It's also possible to ask the user to enter input in a modal.

input modal

The modal can also have a "Cancel" button, as well as many other customizable options.

Installation

To install the stable CRAN version:

install.packages("shinyalert")

To install the latest development version from GitHub:

install.packages("devtools")
devtools::install_github("daattali/shinyalert")

Input modals

Usually the purpose of a modal is simply informative, to show some information to the user. However, the modal can also be used to retrieve an input from the user by setting the type = "input" parameter.

Only a single input can be used inside a modal. By default, the input will be a text input, but you can use other HTML input types by specifying the inputType parameter. For example, inputType = "number" will provide the user with a numeric input in the modal.

See the Modal return value and Callbacks sections below for information on how to access the value entered by the user.

Modal return value

Modals created with shinyalert have a return value when they exit.

When there is an input field in the modal (type="input"), the value of the modal is the value the user entered. When there is no input field in the modal, the value of the modal is TRUE if the user clicked the "OK" button, and FALSE if the user clicked the "Cancel" button.

When the user exits the modal using the Escape key or by clicking outside of the modal, the return value is FALSE (as if the "Cancel" button was clicked). If the timer parameter is used and the modal closes automatically as a result of the timer, no value is returned from the modal.

The return value of the modal can be accessed via input$shinyalert in the Shiny server's code, as if it were a regular Shiny input. The return value can also be accessed using the modal callbacks.

Callbacks

The return value of the modal is passed as an argument to the callbackR and callbackJS functions (if a callbackR or callbackJS arguments are provided). These are functions that get called, either in R or in JavaScript, when the modal exits.

For example, using the following shinyalert code will result in a modal with an input field. After the user clicks "OK", a hello message will be printed to both the R console and in a native JavaScript alert box. You don't need to provide both callback functions, but in this example both are used for demonstration.

shinyalert(
  "Enter your name", type = "input",
  callbackR = function(x) { message("Hello ", x) },
  callbackJS = "function(x) { alert('Hello ' + x); }"
)

Notice that the callbackR function accepts R code, while the callbackJS function uses JavaScript code.

Since closing the modal with the Escape key results in a return value of FALSE, the callback functions can be modified to not print hello in that case.

shinyalert(
  "Enter your name", type = "input",
  callbackR = function(x) { if(x != FALSE) message("Hello ", x) },
  callbackJS = "function(x) { if (x !== false) { alert('Hello ' + x); } }"
)

Comparison with Shiny modals

Doesn't Shiny already have support for modals?

Yes, it does.

And Shiny's modals are more powerful in some ways than shinyalert modals: Shiny's native modals (showModal()+modalDialog()) can contain multiple input fields and even outputs.

I created shinyalert for two reasons: first of all, I started working on it well before Shiny had modals. But I decided to keep working on it and release it even afterwards because I find shinyalert to be easier to use and to result in much nicer modals. There are also some extra features in shinyalert, such as the callback functions and the timer. But ultimately it's a matter of convenience and aesthetics.

Contributions

If you have any suggestions or feedback, I would love to hear about it. You can either message me directly, open an issue if you want to request a feature/report a bug, or make a pull request if you can contribute.

Lastly, if you find shinyalert useful, please consider supporting me for the countless hours I've spent building, documenting, and supporting various open-source packages :)

News

shinyalert 1.0

2018-02-12

Initial release

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("shinyalert")

1.0 by Dean Attali, 2 years ago


https://github.com/daattali/shinyalert https://daattali.com/shiny/shinyalert-demo/


Report a bug at https://github.com/daattali/shinyalert/issues


Browse source code at https://github.com/cran/shinyalert


Authors: Dean Attali [aut, cre] (R interface) , Tristan Edwards [aut] (sweetalert library)


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports digest, shiny, stats

Suggests colourpicker, knitr, rmarkdown

System requirements: pandoc with https support


Imported by BDEsize, CamelUp, objectremover, wiseR.

Suggested by gravitas.


See at CRAN