【问题标题】:r - tryCatch error handling within Shinyr - Shiny 中的 tryCatch 错误处理
【发布时间】:2015-09-30 16:45:23
【问题描述】:

背景

我正在使用闪亮的应用程序来处理cuttable 一些数据。

数据集包含在下面的shiny代码中,但头部是:

> head(df_in)
  Report_Year Position   Target
1        2014      CEO 29.27644
2        2014      CEO 29.27644
3        2014      CFO 17.56586
4        2014       CE 17.56586
5        2014      COO 17.56586
6        2014      CEO 46.84231

我正在使用下面的语句来cuttable的数据

  df <- df_in %>%
    filter(Report_Year == input$v_year,
           Position == "CEO") %>%
    select(Target) %>%
    filter(!is.na(Target)) %>%
    mutate(bins = cut(Target, breaks=seq(0, (max(Target)+25), 25))) %>%
    select(bins) %>%
    table %>%
    as.data.frame

>
         . Freq
1   (0,25]    0
2  (25,50]    6
3  (50,75]    2
4 (75,100]    1

在数据中,Report_Year == 2012no 条目,因此当用户选择 2012 时,我希望它要么显示“无数据”之类的消息,要么此时我会对空数据框感到满意。

我尝试了tryCatch() 语句,但显然我没有正确执行,因为当用户选择2012 时应用程序崩溃。

问题

我应该如何写tryCatch

闪亮的应用程序

library(shiny)
library(dplyr)

ui <- shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("v_year", label = "select year", choices = c(2012, 2014), selected = 2014)
    ),
    mainPanel(
      dataTableOutput("dt")
    )
  )
))

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


df <- reactive({

  ## data
  df_in <- structure(list(Report_Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 
  2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
  2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
  2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Position = c("CEO", 
  "CEO", "CFO", "CE", "COO", "CEO", "CFO", "BUE", "CE", "CFO", 
  "CEO", "COO", "CE", "BUE", "COO", "CFO", "CE", "GC", "CEO", "CEO", 
  "CE", "BUE", "CEO", "CFO", "CE", "GC", "CFO", "CEO", "CEO", "CE"
  ), Target = c(29.2764408921928, 29.2764408921928, 17.5658645353157, 
  17.5658645353157, 17.5658645353157, 46.8423054275084, 38.6449019776945, 
  38.6449019776945, 38.6449019776945, 35.1317290706313, 35.1317290706313, 
  46.8423054275084, 35.1317290706313, 35.1317290706313, 43.9146613382892, 
  43.9146613382892, 35.1317290706313, 35.1317290706313, 29.2764408921928, 
  87.8293226765783, 11.7105763568771, 11.7105763568771, 29.2764408921928, 
  17.5658645353157, 35.1317290706313, 40.9870172490699, 40.9870172490699, 
  73.1911022304819, 70.2634581412627, 46.8423054275084)), class = "data.frame", .Names = c("Report_Year", 
  "Position", "Target"), row.names = c(NA, -30L))

    tryCatch({
      df <- df_in %>%
        filter(Report_Year == input$v_year,
               Position == "CEO") %>%
        select(Target) %>%
        filter(!is.na(Target)) %>%
        mutate(bins = cut(Target, breaks=seq(0, (max(Target)+25), 25))) %>%
        select(bins) %>%
        table %>%
        as.data.frame
    }, 
    warning=function(w) { 
      print("Warning")
      df <- data.frame()
      return(NA)
    }, 
    error=function(e) {
      print("Error")
      df <- data.frame()
      return(NULL)
    }
    )
})

output$dt <- renderDataTable({
  df  <- df()
})

})

shinyApp(ui = ui, server = server)

会话信息

> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.2 LTS

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C               LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
 [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8    LC_PAPER=en_AU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] knitr_1.10.5   rmarkdown_0.7  stringr_1.0.0  extrafont_0.17 scales_0.2.5   tidyr_0.2.0    ggplot2_1.0.1  reshape2_1.4.1
 [9] dplyr_0.4.2    shiny_0.12.1 

错误信息

【问题讨论】:

  • 你的应用对我来说很好用。当我选择 2012 时它不会崩溃..
  • @Lyzander 哦。我正在通过 RStudio 运行它,你呢?
  • 我也在使用 Rstudio。
  • @LyzanderR 这很奇怪。我也添加了我的会话信息,以防万一
  • 你得到什么错误?

标签: r shiny


【解决方案1】:

这样的事情怎么样?

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

    df_in <- structure(...) # As before

    # Filter data according to input$v_year
    df <- reactive({
        df_in %>%
            filter(Report_Year == input$v_year, Position == "CEO") %>%
            select(Target) %>%
            filter(!is.na(Target))
    })

    # Prepare bins or return if df is empty
    bins <- reactive({
        if(nrow(df()) == 0) return()
        df() %>%
            mutate(bins = cut(Target, breaks=seq(0, (max(Target)+25), 25))) %>%
            select(bins) %>%
            table %>%
            as.data.frame
    })

    output$dt <- renderDataTable({
        bins()
    })        
})

由于我们检查df() 是否为空,只要您的输入正确,seq 将永远不会收到空参数。

【讨论】:

  • 是的,这可能是我必须采取的方法。使用我的原始代码,当用户选择 2012 时,您的应用会崩溃吗?
  • 一般来说,我认为检查输入是您在 Shiny 中应该做的事情,不仅要包含可能的错误来源,还要避免不必要的更改传播。
  • 感谢“已知错误”链接。我可能会更改问题以使其与崩溃/已知错误有关。
猜你喜欢
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 2013-10-26
  • 1970-01-01
相关资源
最近更新 更多