文章目录
- [8 User feedback](#8 User feedback)
-
- [8.1 Validation](#8.1 Validation)
-
- [8.1.1 Validating input](#8.1.1 Validating input)
- [8.1.2 Cancelling execution with `req()`](#8.1.2 Cancelling execution with
req())
- [8.1.3 `req()` and validation](#8.1.3
req() and validation)
- [8.1.4 Validate output](#8.1.4 Validate output)
- [8.2 Notifications](#8.2 Notifications)
-
- [8.2.1 Transient notification](#8.2.1 Transient notification)
- [8.2.2 Removing on completion](#8.2.2 Removing on completion)
- [8.2.3 Progressive updates](#8.2.3 Progressive updates)
- [8.3 Progress bars](#8.3 Progress bars)
-
- [8.3.1 Shiny](#8.3.1 Shiny)
- [8.3.2 Waiter](#8.3.2 Waiter)
- [8.3.3 Spinners](#8.3.3 Spinners)
- [8.4 Confirming and undoing](#8.4 Confirming and undoing)
-
- [8.4.1 Explicit confirmation](#8.4.1 Explicit confirmation)
- [8.4.2 Undoing an action](#8.4.2 Undoing an action)
- [8.4.3 Trash](#8.4.3 Trash)
- [8.5 Summary](#8.5 Summary)
8 User feedback
8.1 Validation
复制代码
library(shiny);ui <- fluidPage(
shinyFeedback::useShinyFeedback(),
numericInput("n", "n", value = 10),
textOutput("half")
)
server <- function(input, output, session) {
half <- reactive({
even <- input$n %% 2 == 0
shinyFeedback::feedbackWarning("n", !even, "Please select an even number")
input$n / 2
})
output$half <- renderText(half())
};shinyApp(ui,server)
8.1.2 Cancelling execution with req()
复制代码
ui <- fluidPage(
selectInput("language", "Language", choices = c("", "English", "Maori")),
textInput("name", "Name"),
textOutput("greeting")
)
server <- function(input, output, session) {
greetings <- c(
English = "Hello",
Maori = "Kia ora"
)
output$greeting <- renderText({
req(input$language, input$name)
paste0(greetings[[input$language]], " ", input$name, "!")
})
}
}
8.1.3 req() and validation
复制代码
library(shiny);ui <- fluidPage(
shinyFeedback::useShinyFeedback(),
textInput("dataset", "Dataset name"),
tableOutput("data")
)
server <- function(input, output, session) {
data <- reactive({
req(input$dataset)
exists <- exists(input$dataset, "package:datasets")
shinyFeedback::feedbackDanger("dataset", !exists, "Unknown dataset")
req(exists, cancelOutput = T)
get(input$dataset, "package:datasets")
})
output$data <- renderTable({
head(data())
})
};shinyApp(ui,server)
8.1.4 Validate output
复制代码
ui <- fluidPage(
numericInput("x", "x", value = 0),
selectInput("trans", "transformation",
choices = c("square", "log", "square-root")
),
textOutput("out")
)
server <- function(input, output, session) {
output$out <- renderText({
if (input$x < 0 && input$trans %in% c("log", "square-root")) {
validate("x can not be negative for this transformation")
}
switch(input$trans,
square = input$x ^ 2,
"square-root" = sqrt(input$x),
log = log(input$x)
)
})
};shinyApp(ui,server)
8.2 Notifications
复制代码
ui <- fluidPage(
actionButton("goodnight", "Good night")
)
server <- function(input, output, session) {
observeEvent(input$goodnight, {
showNotification("So long")
Sys.sleep(1)
showNotification("Farewell")
Sys.sleep(1)
showNotification("Auf Wiedersehen")
Sys.sleep(1)
showNotification("Adieu")
})
};shinyApp(ui,server)
8.2.1 Transient notification
8.2.2 Removing on completion
8.2.3 Progressive updates
8.3 Progress bars
8.3.1 Shiny
8.3.2 Waiter
8.3.3 Spinners
8.4 Confirming and undoing
8.4.1 Explicit confirmation
8.4.2 Undoing an action
8.4.3 Trash
8.5 Summary