使用一个 if() 而不是多个 observeEvent() 在闪亮的应用程序中显示消息
Use one if() instead of multiple observeEvent() to display message in a shiny app
我有下面这个闪亮的应用程序,我在其中使用了三个根据操作按钮激活的观察器。我想知道如何用包含所有 3 个条件的 if() 替换那些观察者。
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$h2("Sweet Alert examples"),
actionButton(
inputId = "success",
label = "Launch a success sweet alert",
icon = icon("check")
),
actionButton(
inputId = "error",
label = "Launch an error sweet alert",
icon = icon("remove")
),
actionButton(
inputId = "sw_html",
label = "Sweet alert with HTML",
icon = icon("thumbs-up")
)
)
server <- function(input, output, session) {
observeEvent(input$success, {
sendSweetAlert(
session = session,
title = "Success !!",
text = "All in order",
type = "success"
)
})
observeEvent(input$error, {
sendSweetAlert(
session = session,
title = "Error !!",
text = "It's broken...",
type = "error"
)
})
observeEvent(input$sw_html, {
sendSweetAlert(
session = session,
title = NULL,
text = tags$span(
tags$h3("With HTML tags",
style = "color: steelblue;"),
"In", tags$b("bold"), "and", tags$em("italic"),
tags$br(),
"and",
tags$br(),
"line",
tags$br(),
"breaks",
tags$br(),
"and an icon", icon("thumbs-up")
),
html = TRUE
)
})
}
shinyApp(ui, server)
您可以在获取事件后调用辅助函数来执行您想要的操作
server <- function(input, output, session) {
observeEvent(input$success, {sendMessage("success")})
observeEvent(input$error, {sendMessage("error")})
observeEvent(input$sw_html, {sendMessage("sw_html")})
sendMessage <- function(type) {
if (type=="success") {
sendSweetAlert(
session = session,
title = "Success !!",
text = "All in order",
type = "success"
)
} else if (type=="error") {
sendSweetAlert(
session = session,
title = "Error !!",
text = "It's broken...",
type = "error"
)
} else if (type="sw_html") {
sendSweetAlert(
session = session,
title = NULL,
text = tags$span(
tags$h3("With HTML tags",
style = "color: steelblue;"),
"In", tags$b("bold"), "and", tags$em("italic"),
tags$br(),
"and",
tags$br(),
"line",
tags$br(),
"breaks",
tags$br(),
"and an icon", icon("thumbs-up")
),
html = TRUE
)
}
}
}
我有下面这个闪亮的应用程序,我在其中使用了三个根据操作按钮激活的观察器。我想知道如何用包含所有 3 个条件的 if() 替换那些观察者。
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$h2("Sweet Alert examples"),
actionButton(
inputId = "success",
label = "Launch a success sweet alert",
icon = icon("check")
),
actionButton(
inputId = "error",
label = "Launch an error sweet alert",
icon = icon("remove")
),
actionButton(
inputId = "sw_html",
label = "Sweet alert with HTML",
icon = icon("thumbs-up")
)
)
server <- function(input, output, session) {
observeEvent(input$success, {
sendSweetAlert(
session = session,
title = "Success !!",
text = "All in order",
type = "success"
)
})
observeEvent(input$error, {
sendSweetAlert(
session = session,
title = "Error !!",
text = "It's broken...",
type = "error"
)
})
observeEvent(input$sw_html, {
sendSweetAlert(
session = session,
title = NULL,
text = tags$span(
tags$h3("With HTML tags",
style = "color: steelblue;"),
"In", tags$b("bold"), "and", tags$em("italic"),
tags$br(),
"and",
tags$br(),
"line",
tags$br(),
"breaks",
tags$br(),
"and an icon", icon("thumbs-up")
),
html = TRUE
)
})
}
shinyApp(ui, server)
您可以在获取事件后调用辅助函数来执行您想要的操作
server <- function(input, output, session) {
observeEvent(input$success, {sendMessage("success")})
observeEvent(input$error, {sendMessage("error")})
observeEvent(input$sw_html, {sendMessage("sw_html")})
sendMessage <- function(type) {
if (type=="success") {
sendSweetAlert(
session = session,
title = "Success !!",
text = "All in order",
type = "success"
)
} else if (type=="error") {
sendSweetAlert(
session = session,
title = "Error !!",
text = "It's broken...",
type = "error"
)
} else if (type="sw_html") {
sendSweetAlert(
session = session,
title = NULL,
text = tags$span(
tags$h3("With HTML tags",
style = "color: steelblue;"),
"In", tags$b("bold"), "and", tags$em("italic"),
tags$br(),
"and",
tags$br(),
"line",
tags$br(),
"breaks",
tags$br(),
"and an icon", icon("thumbs-up")
),
html = TRUE
)
}
}
}