如果数据丢失,则以闪亮的-R-文本进行绘制会导致错误

如果数据丢失,则以闪亮的-R-文本进行绘制会导致错误,r,shiny,plotly,R,Shiny,Plotly,我正在制作一款闪亮的应用程序,可以过滤和总结大量数据。 它在很大程度上起作用。但我受法律约束,限制了图形中显示的内容,因此我过滤掉了5以下的所有内容(如代码中所示)。这会导致一些变量被呈现为零,因为我在添加跟踪中有文本,所以它会抛出一个错误(列text的长度必须为0,而不是1)。 我试图找到一种方法,如果绘图为空,则获取文本输出,或者防止出现错误 如果输出抛出错误,我可以呈现其他内容(自定义消息),这也可能会对我有所帮助 这是一个可复制的示例, 如果你把学校改成霍格沃茨,错误就会出现 我的数据:

我正在制作一款闪亮的应用程序,可以过滤和总结大量数据。 它在很大程度上起作用。但我受法律约束,限制了图形中显示的内容,因此我过滤掉了5以下的所有内容(如代码中所示)。这会导致一些变量被呈现为零,因为我在添加跟踪中有文本,所以它会抛出一个错误(列
text
的长度必须为0,而不是1)。 我试图找到一种方法,如果绘图为空,则获取文本输出,或者防止出现错误

如果输出抛出错误,我可以呈现其他内容(自定义消息),这也可能会对我有所帮助

这是一个可复制的示例,
如果你把学校改成霍格沃茨,错误就会出现

我的数据:

mydata <- structure(list(year = c("2001", "2002", "2001", "2002", "2001","2002", "2001", "2002", "2001", "2002", "2001", "2002", "2001", "2002", "2001", "2002", "2001", "2002", "2001", "2001", "2002"), school = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Beauxbatons", "Hogwarths"), class = "factor"), grade = c(67, 69, 72, 90, 75, 85, 13, 45, 12, 90, 75, 85, 13, 45, 12, 67, 69, 72, 67, 69, 72), magic = structure(c(1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("black", "white"), class = "factor")), row.names = c(NA, -21L), class = c("tbl_df", "tbl", "data.frame"))
mydata%
过滤器(n>5)%>%
绘图(type=“scatter”)%>%
添加跟踪(
x=~年,
y=~m,
text=~粘贴('编号:',n),
类型='bar'
)
}) 
}
#运行应用程序
shinyApp(用户界面=用户界面,服务器=服务器)

您可以使用
validate
执行此操作。它使用
need
检查条件,如果满足条件则继续,否则会为用户显示自定义消息

您可以首先根据
(n>5)
筛选数据。然后检查此数据是否有任何行。如果是,绘制它,如果不是,抛出一条消息

output$plot_school <- renderPlotly({

    filtered_data <- mydata %>%
        filter(school == input$school, magic == input$magic) %>% 
        group_by(year) %>% 
        summarise(m = mean(grade), n=n()) %>%
        filter(n > 5)

    # Add validate - checks if number of rows in filtered_data is > 0
    # If not, displays "Data insufficient for plot"
    validate(
      need( nrow(filtered_data) > 0, "Data insufficient for plot")
    )

    # Create plot
    filtered_data %>%
      plot_ly(type = "scatter") %>%
      add_trace(
        x = ~ year,
        y = ~ m,
        text = ~paste('number: ', n),
        type = 'bar'
      )
  })
输出$plot\u学校%
组别(年份)%>%
总结(m=平均值(等级),n=n())%>%
过滤器(n>5)
#添加验证-检查筛选的_数据中的行数是否大于0
#如果不是,则显示“数据不足以打印”
证实(
需要(nrow(过滤数据)>0,“数据不足以绘图”)
)
#创建绘图
过滤的\u数据%>%
绘图(type=“scatter”)%>%
添加跟踪(
x=~年,
y=~m,
text=~粘贴('编号:',n),
类型='bar'
)
})
阅读更多关于验证的信息

output$plot_school <- renderPlotly({

    filtered_data <- mydata %>%
        filter(school == input$school, magic == input$magic) %>% 
        group_by(year) %>% 
        summarise(m = mean(grade), n=n()) %>%
        filter(n > 5)

    # Add validate - checks if number of rows in filtered_data is > 0
    # If not, displays "Data insufficient for plot"
    validate(
      need( nrow(filtered_data) > 0, "Data insufficient for plot")
    )

    # Create plot
    filtered_data %>%
      plot_ly(type = "scatter") %>%
      add_trace(
        x = ~ year,
        y = ~ m,
        text = ~paste('number: ', n),
        type = 'bar'
      )
  })