5

I'm trying to use the relatively new shinyAlert package to see if it offers better results than the sweetalert package but I'm unable to figure out how to get this:

Myvar <-  shinyalert input text 

from this minimal example.

library(shiny)
library(shinyjs)
library(shinyalert)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  useShinyalert(),
     actionButton("run", "Run", class = "btn-success")
)
server <- function(input, output, session) {
  shinyEnv <- environment()

  observeEvent(input$run, {

    shinyalert('hello', type='input')
  })
}
shinyApp(ui = ui, server = server)

My thanks for any help from you geniouses out there.

user5029763
  • 1,903
  • 1
  • 15
  • 23
Mark
  • 2,789
  • 1
  • 26
  • 66

2 Answers2

5

Here is how you do it:

library(shiny)
library(shinyalert)

ui <- fluidPage(
  useShinyalert(),
  actionButton("run", "Run", class = "btn-success")
)
server <- function(input, output, session) {

  observeEvent(input$run, {
    shinyalert('hello', type='input', callbackR = mycallback)
  })

  mycallback <- function(value) {
    cat(value)
  }
}
shinyApp(ui = ui, server = server)

It's done using callbacks. You can assign the value to a reactive variable if you'd like.

I had fully documented the package last month and was about to release it, and then my computer crashed before I had a chance to push to github, and lost all progress. I haven't had a chance to work on it again. So sorry that the documentation isn't great yet, the package is still un-released so use at your own risk for now :)

(Notice that you don't need shinyjs for this)

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • Fair enough, but this does not save the value to variable... What if you need to condition on the output of the shinyalert? For instance, I would need it to return a boolean variable - how do I store it to a variable? – Charl Francois Marais Sep 08 '18 at 15:41
  • 1
    The documentation for shinyalert shows one way to do this, shinyalert automatically saves the response inside `input$shinyalert`. Or you can use the code in my answer together with a `reactiveVal()` variable. – DeanAttali Sep 21 '18 at 23:55
  • 1
    Just want to say thanks for all your hard work on this, Dean. It's really appreciated. – awwsmm Jul 09 '19 at 17:17
2

I have no experience with the package shinyalert, but you can achieve what you want with the widely used and well documented modal dialogs from shiny. Maybe you there is a reason for you to stick with shinyalert that I am unaware off, but if not, example code for achieving what you want with modal dialogs:

ui <- fluidPage(
  shinyjs::useShinyjs(),
  actionButton("run", "Run", class = "btn-success"),
  textOutput("output1")
)
server <- function(input, output, session) {

  dataModal <- function(failed = FALSE) {
    modalDialog(
      textInput("input1", "Enter text:",
                placeholder = 'Enter text here'
      )
    )
  }

  # Show modal when button is clicked.
  observeEvent(input$run, {
    showModal(dataModal())
  })

  output$output1 <- renderText({
    input$input1
  })
}
shinyApp(ui = ui, server = server)

Let me know if this helps!

Florian
  • 24,425
  • 4
  • 49
  • 80
  • Without testing yes this might work but won't be what I'm looking for. For one reason because my app already uses dozens of sweet/shiny alerts and I would like to stick to the same style – Mark Jul 16 '17 at 15:38
  • Ok, sounds reasonable. I hope someone else with more experience with the shinyalert package can help you. – Florian Jul 16 '17 at 15:43
  • I had a look at the github repository https://github.com/daattali/shinyalert/blob/master/inst/www/srcjs/shinyalert.js but I'm not sure how to figure out the callback functions – Mark Jul 16 '17 at 15:50