You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Shiny中根据用户输入显示三种反馈警告?代码问题求助

问题

需要在以下三种场景向用户显示反馈警告:

  • 上传的文件总数不等于23
  • 文件格式错误
  • 同时满足上述两种情况

尝试用if else语句实现,但只有第一种情况生效,代码如下:

library(shiny)
library(tools)
library(shinyFeedback)

ui <- fluidPage(
  useShinyFeedback(),
  fileInput("site",
            "Upload .json",
            accept = ".json",
            multiple = T),
  actionButton("submit", "Submit"),
  textOutput("validate")
)

server <- function(input, output, session){
  validateJSON <- reactive({
    json <- input$site
    json$filetype <- file_ext(json$datapath)
    totalFiles <- nrow(json)
    
    if(totalFiles != 23){
      shinyFeedback::feedbackWarning("site",
                                     show = totalFiles != 23,
                                     "Total .json files must equal 23.")
    } else if (json$filetype != "json"){
      shinyFeedback::feedbackWarning("site",
                                     show = json$filetype != "json",
                                     "Wrong file format! .json files only")
    } else if ((totalFiles !=23) & (json$filetype != "json")) {
      shinyFeedback::feedbackWarning("site",
                                     show = (totalFiles != 23) & (json$filetype != "json"),
                                     "Wrong file type - 23 total .json files needed.")
    }
  })
  validateWhenClick <- eventReactive(input$submit, validateJSON())
  output$validate <- renderPrint(validateWhenClick())
}

shinyApp(ui, server)

错误原因分析

  1. if-else分支逻辑顺序错误:
    当同时满足文件数量错误和格式错误时,第一个if(totalFiles !=23)会先触发,后续else分支不会执行,第三种情况的判断永远不会被触发——因为它的前置条件已经被前两个分支覆盖,且放在了最后。

  2. 文件格式判断逻辑错误:
    上传多个文件时,json$filetype是布尔向量,直接用json$filetype != "json"返回的是多个布尔值,而shinyFeedback的show参数需要单个布尔值。应该判断是否存在非json格式的文件,用any(json$filetype != "json")。

  3. 未处理空上传情况:
    用户未上传文件就点击提交时,input$site为NULL,执行nrow(json)会直接报错,需要先判断文件是否存在。

修复后的代码

library(shiny)
library(tools)
library(shinyFeedback)

ui <- fluidPage(
  useShinyFeedback(),
  fileInput("site",
            "Upload .json",
            accept = ".json",
            multiple = T),
  actionButton("submit", "Submit"),
  textOutput("validate")
)

server <- function(input, output, session){
  validateJSON <- reactive({
    # 每次验证前清除之前的反馈
    hideFeedback("site")
    
    json <- input$site
    if(is.null(json)){
      feedbackWarning("site", show = TRUE, "请先上传文件")
      return("请先上传文件")
    }
    
    json$filetype <- file_ext(json$datapath)
    totalFiles <- nrow(json)
    has_wrong_format <- any(json$filetype != "json")
    wrong_count <- totalFiles != 23
    
    # 优先判断同时满足两种错误的情况,再分别处理单个错误
    if(wrong_count && has_wrong_format){
      feedbackWarning("site", show = TRUE, "文件格式错误 - 需要上传23个.json文件")
      return("文件格式错误 - 需要上传23个.json文件")
    } else if(wrong_count){
      feedbackWarning("site", show = TRUE, "上传的.json文件总数必须为23")
      return("上传的.json文件总数必须为23")
    } else if(has_wrong_format){
      feedbackWarning("site", show = TRUE, "文件格式错误!仅支持.json文件")
      return("文件格式错误!仅支持.json文件")
    } else {
      feedbackSuccess("site", show = TRUE, "文件验证通过")
      return("文件验证通过")
    }
  })
  
  validateWhenClick <- eventReactive(input$submit, validateJSON())
  output$validate <- renderPrint(validateWhenClick())
}

shinyApp(ui, server)

内容的提问来源于stack exchange,提问作者ksinva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 09:25:57