-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using sweetalertR inside Shiny apps? #1
Comments
Hey, I wasn't aware of this library, it does look awesome! So much nicer than real alerts indeed. Really good to know. I'm a bit embarrassed to admit that I haven't actually looked into htmlwidgets yet myself so I'm very unfamiliar with how to run these things... but after playing around with it for a bit it seems to me that something like this will work in a shiny app:
Which works really nicely :) Although since I was summoned here, I'll say that I don't love the idea of defining the alert box in the input -- to me, that should be part of the logic rather than part of the UI. It's very possible that my previous code block is not the correct way of using this in a shiny app though. Is it possible to define the alert in the server? Anyway, just as a fun exercise, I wanted to see if this could be incorporated into shinyjs and it does seem like it, but I won't do it unless there's demand for it. Here's a quick simple example of how to use shinyjs as an interface to sweetalert
I hope my presence here is ok @timelyportfolio |
@daattali I have been watching In my mind,
One other benefit to library(shiny)
library(sweetalertR)
jscode <- "shinyjs.swal = function(params) { swal.apply(this, params); }"
runApp(shinyApp(
ui = fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = jscode),
textInput("title", "Title"),
textInput("text", "Text"),
selectInput("type", "Type", c("warning", "error", "success")),
actionButton("swal", "Action!"),
sweetalert()
),
server = function(input, output, session) {
observeEvent(input$swal, {
shinyjs::js$swal(input$title, input$text, input$type)
})
} This doesn't really apply to I'll stop for now, but happy to continue the discussion. |
@Thercast did the discussion above help? Off topic, but I enjoyed the couple of episodes of R podcast. Sounds like a new R podcast has started. |
Thank you @timelyportfolio and @daattali! I ran the updated example on my personal server and it works great. Unfortunately the Shiny servers I use at work don't have the necessary libraries required by the v8 package (hence I can't use Thanks for your kind feedback on the previous episodes! I'm getting much closer to re-launching the R-Podcast and I'd like to have a lot more interviews than in the previous run. I plan on doing a series of episodes dedicated to Shiny and htmlwidgets, and it would be excellent to have both of you as future guests on the show. If you're interested please send a message to my podcast email account (on my GitHub profile). |
@timelyportfolio you're right, the dependency management is actually a really nice feature. I'd just like to raise another reason whyI like being able to call JS functions from the server and not have them completely coupled to a click on a specific tag. In my past life as a webdev I often ran into situations where I want to trigger an action (such as an alert box) based on some logic that is NOT a button press. For example, if the user presses a "Submit" button and there's an error, I'd like to show an error alert box. Or if the user presses on a button but another input field is not valid, you might want to trigger the alert. I'm not sure if you agree that it's a useful use of alert boxes -- maybe for 99% of shiny users having an alert box on click is all they need. @Thercast it looks like you started these podcasts before I even knew what R is, long time! I did see your last one with Yihui a few months ago, it's a good idea, I would very much support having more episodes. |
Thank you both (timelyportfolio and daattali) for publishing workable examples for Shiny. SWAL is something I was looking for quite a long time! |
@daattali is there any way to pass named arguments to function declared through shinyjs::extendShinyjs()? |
Yes, you can pass named arguments. What you cannot do with shinyjs is use both named and unnamed arguments in the same function call. See this section of the README for more info. The error you were seeing was an error on sweetalert's part, not coming from shinyjs, but it's happening because in my previous code I called sweetalert using
|
Thank you daattali, you are a star! It works. |
Hello,
|
It doesn't look like you have any code here that defines what to do when the confirm button vs cancel button is clicked. I wrote the code above after looking at the very basic SWAL documentation and just wrote it quickly to make it show a popup. If you want to have functionality happen when you click the buttons, you'd have to read their documentation to see how to do that. |
@daattali. Thank you for your answer.
it returns the following error:
|
@csese It doesn't look like you fully understand either how my sample code above works or how I think this is closer to the code you want (notice that after clicking the confirm button I'm just showing an alert message because your javascript code to switch tabs doesn't seem to work, but that's a purely javascript question)
Also, the file resetInputFile.js seems to be unrelated but because it's in the code you include it keeps resulting in errors for us. |
@daattali Thank you for your explanations.
|
Is there a way to close the shinyjs::js$swal messages with a line of code after finishing a long calculation? trying to figure it out but no luck so far: see also: SO question
|
Hello, I just came across your blog entry on
sweetalertR
and it looks great! I'd like to incorporate this functionality in some of my Shiny applications I'm developing at work. What would be the best way to use this in a Shiny app? Ideally it would be triggered after the user hits an action button much like what you did in your R markdown example. This example from the Shiny gallery uses a javascript file called message-handler.js to call the basicalert
function, but I'm wondering if there's an easy way to swap that with the functions offered in this package? I know @daattali has created the excellent shinyjs package which contains a function calledinfo
to display normal alerts, so I guess another approach might be to use itsextendShinyjs
paradigm but I'm pretty new to using customized javascript solutions in Shiny Apps. Sorry for the long text!The text was updated successfully, but these errors were encountered: