R闪亮处理 - 处理来自空白数据帧的错误 [英] R Shiny Handling - handling error from empty data frames

查看:127
本文介绍了R闪亮处理 - 处理来自空白数据帧的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写我的第一个Shiny应用程序,到目前为止我都很享受它。我的应用程序适用于包含许多测量膳食方面变量的数据框。它允许用户使用滑块选择六个连续变量的范围。这些输入用于数据框的子集,然后根据数据子集创建一个 ggplot



我的问题是这样的 - 当选择的范围导致在子集数据框中没有剩余数据时,我在主面板中得到这个红色的错误信息,其中情节通常是:

 错误:参数长度为零(来自:错误if(nrow(layer_data)== 0)return():参数长度为零)。 

我明白为什么会出现这种错误,而且如果我在典型的过程中得到静态图数据分析会议。然而,我试图找出正确的方式来处理这个闪亮的web应用程序的情况。



由于此消息对用户没有意义,我会如:

<1>能够用合理的信息代替

2)返回空白图表 OR

3)不显示任何内容(例如,当数据框为空时,不会显示错误消息或图表)

<问题是,如果我检查一个空的数据框并返回一个不同的(空白)图或消息,那么当用户将滑块设置更改为确实有数据的东西时,正确的图不会出现(因为反应对象不再是相同的)。如果我只是让错误消息像现在这样出现,并且用户更改了设置,那么图表会适当更新。



有人可以推荐一种方法来在Shiny中轻松应对? 以帮助您描述需要验证的情况。给出的例子要求用户选择一些和一个队列。如果用户没有选择所需的对象,那么需要
函数显示相关输出。也可以通过validate函数指定一个定制的CSS类,然后可以用于样式:

  library(shiny) 
library(ggplot2)
myData < - data.frame(group = sample(letters [1:4],100,TRUE)
,cohort = sample(0: TRUE)
,value = runif(100))
runApp(
list(ui = fluidPage(
column(4,
checkboxGroupInput('group','Pick
selectizeInput('cohort','Pick a cohort',choices = 0:4)),
column(8,plotOutput('myplot '))

,server = function(输入,输出,会话){
appData< - 反应({
myData [myData $ group%in%input $ group & myData $ cohort == input $ cohort,]
})
output $ myplot< - renderPlot({
validate(
need(input $ group,'Please select至少有一个组'),
需要(输入$队列> 0,'请ch o)大于0的队列')

g <-ggplot(appData(),aes(x = group,y = value))+
stat_summary(fun.y = sum, ('cohort',输入$ cohort))
g
})
}



I am writing my first Shiny app and so far am enjoying it. My app works on a data frame that includes many variables measuring aspects of meals. It allows a user to select ranges of half a dozen continuous variables using sliders. These inputs are used to subset the data frame, then a ggplot is created based on the subset of data.

My problem is this -- when the ranges chosen result in no data left in the subsetted data frame, I get this red error message printed in the main panel, where the plot would normally be:

Error: argument is of length zero (from: Error in if (nrow(layer_data) == 0) return() : argument is of length zero).  

I understand why this error happens, and it makes sense if I'm getting static plots during a typical data analysis session. However, I'm trying to figure out the right way to handle this in the shiny web app situation.

Since this message makes no sense to the user, I'd like to:

1) Be able to replace it with a sensible message instead OR
2) Return a blank graph OR
3) Display nothing (i.e. no error message or graph to the user when the data frame is empty)

The problem is that if I check for an empty data frame and return a different (blank) plot or message, then when the user changes the slider settings to something that does have data, the correct plot does not appear (since the reactive object is no longer the same). If I just let the error message appear as it does now and the user changes the settings, the graph updates appropriately.

Can someone recommend a way to handle this gracefully in Shiny?

解决方案

As of shiny 0.10.0 there are two nice functions to help with the situation you describe namely need and validate. The example given requires the user to pick a number of groups and a cohort. If the user hasn't selected the required objects then the need functions display relevant output. It is also possible to assign a bespoke CSS class via the validate function which can then be used for styling:

library(shiny)
library(ggplot2)
myData <- data.frame(group = sample(letters[1:4], 100, TRUE)
                     , cohort = sample(0:4, 100, TRUE)
                     , value = runif(100))
runApp(
  list(ui = fluidPage(
    column(4,
    checkboxGroupInput('group', 'Pick a group', choices = letters[1:4]),
    selectizeInput('cohort', 'Pick a cohort', choices = 0:4)),
    column(8, plotOutput('myplot'))
  )
  , server = function(input, output, session) {
    appData <- reactive({
      myData[myData$group %in% input$group & myData$cohort == input$cohort,]
    })
    output$myplot <- renderPlot({
      validate(
        need(input$group, 'Please select at least one group'),
        need(input$cohort > 0, 'Please choose a cohort greater than zero')
      )
      g <-ggplot(appData(),aes(x=group, y=value)) +
        stat_summary(fun.y=sum, geom="bar") +
        ggtitle(paste('cohort', input$cohort))
      g
    })
  }
  )
)

这篇关于R闪亮处理 - 处理来自空白数据帧的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆