【问题标题】:Turning Data Conflict Error (409) into a message that clients can understand将数据冲突错误 (409) 转换为客户可以理解的消息
【发布时间】:2020-02-08 07:09:36
【问题描述】:

我对使用 R 和 Shiny 比较陌生。目前,我在尝试从 Dropbox 访问 html 文件时遇到错误:冲突(HTTP 409),这很好,我知道原因。我确实有一个问题是试图找到一种方法来更改错误代码消息。

我尝试了几种形式的验证和尝试捕获。

library(shiny)
library(rdrop2)
library(httr)

ui <- # Define UI for dataset viewer application

shinyUI(pageWithSidebar(

headerPanel("Test DropBox html Docs to Shiny"),  

sidebarPanel(

  selectInput("Cat", "Choose a Category:",

              choices = c("A", "B", "C")),

  selectInput("Year", "Choose a Year:",

              choices = c("2012", "2011")),

  downloadButton("downFile", "Download File"),

  width = 2), 

mainPanel(

  tabsetPanel(type = "tabs",

              tabPanel("Html Pages", htmlOutput("viewReport"))), 
 width   = 10)
)
)

#IMPORTANT: The two lines below needs to be run just one time unless the token is deleted

# Create Token
token <- drop_auth()

# Save token
saveRDS(token, "droptoken.rds")

token <- readRDS("droptoken.rds")

server <- shinyServer(function(input, output) { 

# ---------------------------------------------------  
  filePutReport <- reactive(

    paste(input$Cat, "_", input$Year, "_Doc.html", sep = "")   
  )

  filePutReport2 <- reactive({
    # Search if the file exists in DropBox

    drop_download(path = paste("shiny_docs/shinydbtest/", filePutReport(), sep = ""),
                  overwrite = TRUE, local_path = "./www",
             dtoken = token)

      filePutReport()
  })

  # Show Html Pages

  output$viewReport <- renderUI({

    tags$iframe(seamless = "seamless", width = "1400", height = "1000",

                src = filePutReport2()
                )
  })

  ###
  output$downFile <- downloadHandler(
    # generate bins based on input$bins from ui.R
    filename = function() {

      paste0(filePutReport() )
    },

    content = function(file){

      file.copy(from = paste0("./www/", filePutReport2() ), to = file, overwrite = TRUE)
    }   
  )
})

shinyApp(ui = ui, server = server)

而不是简单的“错误:冲突(HTTP 409)”,我希望客户端可能能够理解的消息。欢迎任何和所有建议。提前感谢您的帮助。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    在我当前的环境中,我无法建立与 Dropbox 的连接,但请尝试以下方法。我首先删除了您的filePutReport2() 反应式中引用filePutReport() 的最后一行,因为它们是相同的,并且您希望对drop_download 的调用产生一个值(TRUE)或类“try-error”的不可见对象”。因此,您需要在try 语句中进一步包装对drop_download 的调用。这样filePutReport2() 要么包含值 TRUE,要么包含“try-error”类的不可见对象。然后您应该能够在您的renderUI 语句中使用need/validate function,包括自定义错误消息。我希望它能正常工作,因为我无法测试它。

    library(shiny)
    library(rdrop2)
    library(httr)
    
    ui <- # Define UI for dataset viewer application
    
      shinyUI(pageWithSidebar(
    
        headerPanel("Test DropBox html Docs to Shiny"),  
    
        sidebarPanel(
    
          selectInput("Cat", "Choose a Category:",
    
                      choices = c("A", "B", "C")),
    
          selectInput("Year", "Choose a Year:",
    
                      choices = c("2012", "2011")),
    
          downloadButton("downFile", "Download File"),
    
          width = 2), 
    
        mainPanel(
    
          tabsetPanel(type = "tabs",
    
                      tabPanel("Html Pages", htmlOutput("viewReport"))), 
          width   = 10)
      )
      )
    
    #IMPORTANT: The two lines below needs to be run just one time unless the token is deleted
    
    # Create Token
    token <- drop_auth()
    
    # Save token
    saveRDS(token, "droptoken.rds")
    
    token <- readRDS("droptoken.rds")
    
    server <- shinyServer(function(input, output) { 
    
      # ---------------------------------------------------  
      filePutReport <- reactive(
    
        paste(input$Cat, "_", input$Year, "_Doc.html", sep = "")   
      )
    
      filePutReport2 <- reactive({
        # Search if the file exists in DropBox
        try({
        drop_download(path = paste("shiny_docs/shinydbtest/", filePutReport(), sep = ""),
                      overwrite = TRUE, local_path = "./www",
                      dtoken = token)
        }, silent = TRUE)
    
      })
    
      # Show Html Pages
    
      output$viewReport <- renderUI({
    
        validate(
          need(filePutReport2(), 'Custom error message!'))
    
        tags$iframe(seamless = "seamless", width = "1400", height = "1000",
    
                    src = filePutReport()
        )
      })
    
      ###
      output$downFile <- downloadHandler(
        # generate bins based on input$bins from ui.R
        filename = function() {
    
          paste0(filePutReport() )
        },
    
        content = function(file){
    
          file.copy(from = paste0("./www/", filePutReport2() ), to = file, overwrite = TRUE)
        }   
      )
    })
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 我测试了它,到目前为止它工作正常!非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多