简体   繁体   English

将数据冲突错误 (409) 转换为客户端可以理解的消息

[英]Turning Data Conflict Error (409) into a message that clients can understand

I'm relatively new to using R and shiny.我对使用 R 和 shiny 比较陌生。 Currently, I'm getting the Error: Conflict (HTTP 409) when trying to access an html file from dropbox and this is fine, I know the reason.目前,我在尝试从保管箱访问 html 文件时遇到错误:冲突(HTTP 409),这很好,我知道原因。 What I do have a problem with is trying to find a way to change Error code message.我确实有一个问题是试图找到一种方法来更改错误代码消息。

I've tried a couple forms of validation and try-catches.我已经尝试了几个 forms 的验证和尝试捕获。

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)

Instead of simply "Error: Conflict (HTTP 409)", I would a message a client might be able to understand.而不是简单的“错误:冲突(HTTP 409)”,我会发送一条客户端可能能够理解的消息。 Any and all suggestions are welcome.欢迎任何和所有建议。 Thank you in advance for your help.预先感谢您的帮助。

In my current environment I cannot establish a connection to dropbox, but please try the approach below.在我当前的环境中,我无法建立与 Dropbox 的连接,但请尝试以下方法。 I first deleted the last line refering to filePutReport() in your filePutReport2() reactive, since they are the same and you want your call to drop_download to produce either a value (TRUE) or an invisible object of class "try-error".我首先删除了您的filePutReport2()反应式中引用filePutReport()的最后一行,因为它们是相同的,并且您希望对drop_download的调用产生一个值 (TRUE) 或 class “try-error”的不可见 object。 Therefore, you need to further wrap your call to drop_download in a try statement.因此,您需要在try语句中进一步包装对drop_download的调用。 This way filePutReport2() either contains the value TRUE or an invisible object of class "try-error".这样, filePutReport2()要么包含值 TRUE,要么包含 class “try-error”的不可见 object。 Then you should be able to use a need/validate function in your renderUI statement including a custom error message.然后,您应该能够在您的renderUI语句中使用需要/验证 function ,包括自定义错误消息。 I hope it's working, since I can't test it.我希望它有效,因为我无法测试它。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM