r 闪亮的条件行为

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 闪亮的条件行为相关的知识,希望对你有一定的参考价值。

# UI ==========================================================================
toolUI <- function(id, label) {
  ns <- NS(id)
  tagList(
    actionButton(ns("tool"), label)
  )
}

# Server ======================================================================
toolServer <- function(input, output, session) {
}
library(shiny)
library(shinyjs)
source("module.R")

# UI ==========================================================================
ui <- fluidPage(
  useShinyjs(),
  radioButtons("mode", "Mode",
               choices = c("Entry" = "entry",
                           "Validation" = "validation"),
               selected = "entry", inline = TRUE),
  selectInput("status", "Status", 
              choices = c("NA" = "NA",
                          "Entered" = "entered",
                          "Skipped" = "skipped",
                          "Issue" = "issue",
                          "Validated" = "validated",
                          "Removed" = "removed"),
              selected = "NA"
  ),
  hr(),
  textInput("form", "Form Object"),
  div(strong("Actions")),
  div(
    toolUI("submit", "Submit"),
    toolUI("skip", "Skip"),
    toolUI("issue", "Report Issue"),
    toolUI("validate", "Validate"),
    toolUI("remove", "Confirm Skip")
  ),
  br(),
  div(strong("Comment")),
  textOutput("comment")
)

# Server ======================================================================
server <- function(input, output, session) {
  values <- reactiveValues(
    status = "NA",
    mode = "entry",
    comment = NULL
  )
  
  observe({ values$status <- input$status })
  observe({ values$mode <- input$mode })
  
  fill_form <- function() { updateTextInput(session, "form", value = "User entered values") }
  empty_form <- function() { updateTextInput(session, "form", value = "") }
  
  update_comment <- function(comment) { values$comment <- comment }
  
  observe({
    if (values$mode == "entry") {
      if (values$status == "NA" | values$status == "issue") {
        empty_form()
        sapply(list("submit-tool", "skip-tool"), show)
        sapply(list("issue-tool", "validate-tool", "remove-tool"), hide)
        update_comment(NULL)
      } else if (values$status == "entered") {
        fill_form()
        sapply(list("submit-tool", "skip-tool", "issue-tool", "validate-tool", "remove-tool"), hide)
        update_comment(NULL)
      } else if (values$status == "skipped") {
        empty_form()
        sapply(list("submit-tool", "skip-tool", "issue-tool", "validate-tool", "remove-tool"), hide)
        update_comment("User's reason to skip")
      } else {
        empty_form()
        sapply(list("submit-tool", "skip-tool", "issue-tool", "validate-tool", "remove-tool"), hide)
        update_comment(NULL)
      }
    } else if (values$mode == "validation") {
      if (values$status == "entered") {
        fill_form()
        sapply(list("issue-tool", "validate-tool"), show)
        sapply(list("submit-tool", "skip-tool", "remove-tool"), hide)
        update_comment(NULL)
      } else if (values$status == "skipped") {
        empty_form()
        sapply(list("issue-tool", "remove-tool"), show)
        sapply(list("submit-tool", "skip-tool", "validate-tool"), hide)
        update_comment("User's reason to skip")
      } else if (values$status == "validated") {
        fill_form()
        sapply(list("submit-tool", "skip-tool", "issue-tool", "validate-tool", "remove-tool"), hide)
        update_comment(NULL)
      } else if (values$status == "removed") {
        empty_form()
        sapply(list("submit-tool", "skip-tool", "issue-tool", "validate-tool", "remove-tool"), hide)
        update_comment("User's reason to remove")
      } else {
        empty_form()
        sapply(list("submit-tool", "skip-tool", "issue-tool", "validate-tool", "remove-tool"), hide)
        update_comment(NULL)
      }
    }
  })
  
  output$comment <- renderText({ values$comment })
  
  callModule(toolServer, "submit")
  callModule(toolServer, "skip")
  callModule(toolServer, "issue")
  callModule(toolServer, "validate")
  callModule(toolServer, "remove")
}

shinyApp(ui, server)

以上是关于r 闪亮的条件行为的主要内容,如果未能解决你的问题,请参考以下文章

R闪亮:observeEvent和eventReactive的不同行为

来自 selectInput 的具有多个条件的闪亮 R 观察事件

如何根据带有闪亮元素的 R Markdown 文档中的条件禁用按钮?

在R闪亮中,如何在不使用renderUI的情况下首次调用应用程序时消除侧边栏中所有条件面板的闪烁?

在R中使用条件面板和PickerInput

闪亮的响应式 UI 在同一条件变量上挂起多个 uiOutput 调用