2

I would like to save the output result (TRUE/FALSE) of a shinyalert warning (https://github.com/daattali/shinyalert). This post is helpful for printing the value to console (How to capture shinyalert input field as variable), but I am unable to save the value itself as a variable.

library(shiny)
library(shinyalert)

ui <- fluidPage(
  useShinyalert()
)


server <- function(input, output) {

  shinyalert(
    title = "Warning",
    text = "Some warning",
    closeOnEsc = FALSE,
    closeOnClickOutside = FALSE,
    type = "warning",
    showConfirmButton = TRUE,
    showCancelButton = TRUE,
    confirmButtonText = "OK",
    cancelButtonText = "Cancel",
    animation = TRUE,
    callbackR = mycallback
  )


}


shinyApp(ui, server)

This will automatically print to console the value of the shinyalert. Setting the shinyalert as a variable or the value in a function does not seem to do anything:

  mycallback <- function(value) {
    test_var=value
  }
ben
  • 207
  • 3
  • 9

1 Answers1

1

Forget the function in this case, simply observe the alert with observeEvent, as the callback values can be acessed via input$shinyalert.

alert<-shinyalert(
title = "Warning",
text = "Some warning",
closeOnEsc = FALSE,
closeOnClickOutside = FALSE,
type = "warning",
showConfirmButton = TRUE,
showCancelButton = TRUE,
confirmButtonText = "OK",
cancelButtonText = "Cancel",
animation = TRUE,
callbackR = NULL
)

observeEvent(input$shinyalert,
value<<-input$shinyalert
)

Note: I have assigned the value variable as global, make sure you truly assign it.

Edit: This seems to only assign the value when the app is closes, since nothing else is happening whitin the server. To make up for this I added a button for testing purposes, as once another command goes through on the server, the value is assigned.

observeEvent(input$test,
print(value)
)

Edit: If we want to use the value in subsequent statements w/o doing anything else in the server first we will have to use the callbackR funciton.

mycallback<-function(value){
if(value==T){
  print(value) #commands
} else if (value==F){
  print(value) #commands
}
}

Note: This package is still relatively new and has a lot of bugs, for example if you try to chain these modals together as the author of the package claims this package can do, it does not work.

shinyalert(
title = "What is your name?", type = "input",
callbackR = function(value) { shinyalert(paste("Welcome", value)) }
)

Here is the link to the authors description. https://deanattali.com/blog/shinyalert-package/

As noted here...https://github.com/daattali/shinyalert/issues/14 If you install the newest version from github, many of these bugs will be fixed. Looking at the reported bugs associated with this package, we are not the only ones having this issue.

Chabo
  • 2,842
  • 3
  • 17
  • 32
  • apologies if i am just completely missing something, but I am unable to assess the value of "value". observeEvent(input$shinyalert, value<<-input$shinyalert ) observe({ if(value==TRUE){ print("Yes") } }) this also does nothing: observeEvent(input$shinyalert, value<<-input$shinyalert print(value) ) – ben May 24 '18 at 19:13
  • @ben you are not wrong, please see the edits made. It seems that the next step in the program needs to be taken in order for the value to be assigned, may be a bug, or may be the nature of the shinyalert – Chabo May 24 '18 at 19:52
  • Thanks @Chabo, one other issue is the missing { } the observeEvent . Ideally i would like the output of the shinyalert to be within the same obsereveEvent to determine a criteria for a subsequent if statement – ben May 24 '18 at 19:53
  • @ben, If you are not going to do anything in the server before the next statement, then I think we need to go back to the function, since we cannot push 'value' out. See edits – Chabo May 24 '18 at 20:01
  • @ben I think this all may just be due to bugs in a brand new package. Using the If/else within the callbackR function, or using other code within the server (e.g a button) to push the input$value through, will work. Maybe if you made your own buttons on the alert you could in turn monitor those with and observe event and that should also do the trick. – Chabo May 24 '18 at 20:25