简体   繁体   English

以闪亮的方式显示空白图而不是错误

[英]Display blank plot in shiny instead of error

When I have reactive data that gets passed to renderPlot (or other rendering functions), the data is often initially empty until some action happens.当我将反应数据传递给renderPlot (或其他渲染函数)时,数据通常最初是空的,直到发生某些操作。 The default rendering typically displays an error in the app before the action happens because the data is empty, ie默认渲染通常会在动作发生之前在应用程序中显示错误,因为数据为空,即

Error 'x' must be numeric错误“x”必须是数字

in this example.在这个例子中。 Is there some standard way to have the rendering functions act when they don't have data (maybe not rendering if there is an error or just being blank)?是否有一些标准方法可以让渲染函数在没有数据时运行(如果出现错误或只是空白,则可能不会渲染)? I know I could go to the trouble of structuring all the reactive values so the output would be blank, but it seems like unnecessary work.我知道我可以麻烦地构建所有反应值,因此输出将为空白,但这似乎是不必要的工作。

Example in rMarkdown shiny rMarkdown 中的示例闪亮

---
title: "Example"
runtime: shiny
output: html_document
---

```{r}
shinyApp(
    shinyUI(fluidPage(
        inputPanel( 
            numericInput("n", "n", 10),
            actionButton("update", "Update")
        ),
        plotOutput("plot")
    )),

    shinyServer(function(input, output) {
        values <- reactiveValues()
        values$data <- c()

        obs <- observe({
            input$update
            isolate({ values$data <- c(values$data, runif(as.numeric(input$n), -10, 10)) })
        }, suspended=TRUE)

        obs2 <- observe({
            if (input$update > 0) obs$resume()
        })

        output$plot <- renderPlot({
            dat <- values$data
            hist(dat)
        })
    }) 
)
```

You can use the exists function to see if a variable exists before trying to plot it, and change it as desired otherwise:您可以使用exists函数在尝试绘制变量之前查看变量是否存在,否则根据需要更改它:

renderPlot({
    if(exists("values$data")) {
      dat <- values$data 
    } else {
      dat <- 0
    }
    hist(dat)
})

Try surrounding the line that might error in a tryCatch .尝试围绕可能在tryCatch出错的tryCatch In your example, hist(dat) is what errors, so surround it like so:在你的例子中, hist(dat)是什么错误,所以像这样包围它:

tryCatch(
  { hist(dat) }, # Code that might error goes here, between the { }
  error = function(e) {""} # Leave this line as-is.
)

That's telling shiny:那是闪亮的:

  • try to run hist(dat) , and尝试运行hist(dat) ,和
  • if you can't run it without an error, simply display an empty string instead.如果你不能在没有错误的情况下运行它,只需显示一个空字符串。

Now there's no more error message and your histogram still works as expected.现在不再有错误消息,您的直方图仍按预期工作。

Working example工作示例

Here's your MRE with the tryCatch solution implemented - scroll down to where the comment says # CHANGES HERE ;这是实现了tryCatch解决方案的 MRE - 向下滚动到评论说# CHANGES HERE it simply replaces hist(dat) with the 4 tryCatch() lines - that's the only change.它只是用 4 个tryCatch()行替换hist(dat) - 这是唯一的变化。

---
title: "Example"
runtime: shiny
output: html_document
---

```{r}
shinyApp(
    shinyUI(fluidPage(
        inputPanel( 
            numericInput("n", "n", 10),
            actionButton("update", "Update")
        ),
        plotOutput("plot")
    )),

    shinyServer(function(input, output) {
      

        values <- reactiveValues()
        values$data <- c()

        obs <- observe({
            input$update
            isolate({ values$data <- c(values$data, runif(as.numeric(input$n), -10, 10)) })
        }, suspended=TRUE)

        obs2 <- observe({
            if (input$update > 0) obs$resume()
        })

        output$plot <- renderPlot({
            dat <- values$data
            # CHANGES HERE (NEXT 4 LINES)
            tryCatch(
              { hist(dat) }, 
              error = function(e) {""} 
              )
        })
    }) 
)
```

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

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