闪亮:dplyr返回错误消息

3
使用 diamonds 数据集和下面的代码:
  library(dplyr)
  library(ggplot2)

  diam <- diamonds %>%
    dplyr::select(cut, color, carat, price) %>%
    dplyr::arrange(cut, color) %>%
    dplyr::group_by(cut, color)

  diam[c(10:30, 100:140, 300:500, 765:963,1476:1987,2469:3786,5000:6000,
             8654:9876, 11000:12670, 21678:23456,35648:37896,
             45469:46789,49876:51346), c(3, 4)]  <- NA 

  write.csv(diam, "diam.csv")

我创建了一个名为diam.csv的文件,用于下面的shiny应用程序。
library(dplyr)
library(ggplot2)
library(shiny)

ui <- fluidPage(
  titlePanel(
    sidebarLayout(
      sidebarPanel(
        tags$h1(tags$strong("Shiny app")),
        fileInput("file", "Upload your file"), 
        width =2),
      mainPanel(width =10,
                uiOutput("tb")))))

#server

server <- function(input,output){
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.csv(file1$datapath, header=TRUE, sep=',')
  })
  output$sum <- renderTable({
    if(is.null(data())){return ()}

    df_summary <- data()

    df_summary1 <- df_summary %>%
      dplyr::select(cut, color, carat, price)
    summary(df_summary1)
  })

  output$table <- renderDataTable({
    if(is.null(data())){return ()}
    data()
  }) 

  output$stats <- renderDataTable({
    if(is.null(data())){return ()}

    diam1 <- data()
    print(
      diam_stats <- diam1 %>%
        dplyr::select(cut, color, carat) %>%
        dplyr::arrange(cut, color) %>%
        dplyr::group_by(cut, color) %>%
        dplyr::filter(!is.na(carat)) %>%
        dplyr::summarise_each(funs(
          mean(., na.rm=T), 
          sd(., na.rm=T),
          n()))
    )
  }) 

  output$tb <- renderUI({
    if(is.null(data()))
      h5()             
    else
      tabsetPanel(type="tab",
                  tabPanel(h3("Summary", align="center"), 
                           tableOutput("sum")), 
                  tabPanel(h3("Data"), 
                           dataTableOutput("table")),
                  tabPanel(h3("Stats"), 
                           dataTableOutput("stats")))
  })
}
shinyApp(ui = ui, server = server)

我在汇总标签页中遇到了以下错误:

"c('standardGeneric', 'genericFunction', 'function', 'OptionalFunction', 'PossibleMethod', 'optionalMethod')"类对象应用'select_'方法未被识别。

在统计标签页中也出现了以下错误:

"funs"参数缺失,无默认值。

我使用的是 dplyr_0.4.3 版本。有什么解决这些问题的建议吗?

更新

以下是错误的打印屏幕:

enter image description here

这是控制台中显示的内容:

Listening on http://127.0.0.1:3606
Warning: Error in UseMethod: no applicable method for 'select_' applied to an object of class "c('standardGeneric', 'genericFunction', 'function', 'OptionalFunction', 'PossibleMethod', 'optionalMethod')"
Stack trace (innermost first):
    73: select_
    72: dplyr::select
    71: ..redirect
    70: %>%
    69: renderTable [C:\R\Shiny\diam_app/app.R#28]
    68: func
    67: output$sum
     1: runApp
Warning: Error in lazyeval::as.lazy_dots: argument "funs" is missing, with no default
Stack trace (innermost first):
    83: lazyeval::as.lazy_dots
    82: funs_
    81: inherits
    80: is.fun_list
    79: stopifnot
    78: colwise_
    77: summarise_each_
    76: dplyr::summarise_each
    75: ..redirect
    74: %>%
    73: print
    72: exprFunc [C:\R\Shiny\diam_app/app.R#42]
    71: widgetFunc
    70: func
    69: renderFunc
    68: output$stats
     1: runApp

这里是sessioninfo()函数。

    R version 3.2.5 (2016-04-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_New Zealand.1252  LC_CTYPE=English_New Zealand.1252    LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C                        
[5] LC_TIME=English_New Zealand.1252    

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

other attached packages:
 [1] lazyeval_0.1.10     EcoHydRology_0.4.12 DEoptim_2.2-3       topmodel_0.7.2-2    operators_0.1-8     plotly_3.4.13       googleVis_0.5.10   
 [8] tidyr_0.4.1         raster_2.5-2        leaflet_1.0.1       chron_2.3-47        date_1.2-34         scales_0.4.0        dplyr_0.4.3        
[15] DT_0.1              ggplot2_2.1.0       pixmap_0.4-11       RgoogleMaps_1.2.0.7 sp_1.2-3            XML_3.98-1.4        shiny_0.13.2       

loaded via a namespace (and not attached):
 [1] reshape2_1.4.1   lattice_0.20-33  colorspace_1.2-6 htmltools_0.3.5  yaml_2.1.13      base64enc_0.1-3  withr_1.0.1      DBI_0.3.1       
 [9] plyr_1.8.3       stringr_1.0.0    munsell_0.4.3    gtable_0.2.0     htmlwidgets_0.6  devtools_1.11.1  memoise_1.0.0    labeling_0.3    
[17] httpuv_1.3.3     curl_0.9.7       parallel_3.2.5   Rcpp_0.12.4      xtable_1.8-2     jsonlite_0.9.19  mime_0.4         gridExtra_2.2.1 
[25] png_0.1-7        digest_0.6.9     stringi_1.0-1    RJSONIO_1.3-0    grid_3.2.5       tools_3.2.5      magrittr_1.5     assertthat_0.1  
[33] httr_1.1.0       viridis_0.3.4    R6_2.1.2         git2r_0.14.0  

更新 5月4日

我没有意识到EcoHydRology_0.4.12软件包是这个错误消息的源头。这就是为什么它没有包含在上面的示例中。

感谢@enpitsu指出问题的来源以及他通过先加载EcoHydRology软件包然后再加载dplyr的好解决方案。

以下是一个示例,可以重现相同的错误,包括EcoHydRology_0.4.12。 如果按照以下顺序加载软件包,则示例将正常工作

library(EcoHydRology) 
library(dplyr)

然而,如果您交换顺序,它会返回错误消息。
set.seed(123) 
date <-  rep (as.Date(seq(as.Date("2003-01-01"), as.Date("2008-05-31"), by = 1), format="%Y-%m-%d"), 2)
siteID <- c(rep("site1", 1978), rep("site2", 1978))
flow   <- runif(3956, 48530, 1250365) 
df   <- data.frame(date, siteID, flow)

library(dplyr)
library(EcoHydRology)

df1 <- df %>% 
  dplyr::select(siteID, flow) %>% 
  dplyr::group_by(siteID) %>% 
  dplyr::do(cbind(., BaseflowSeparation(.$flow, filter_parameter = 0.925, passes = 3))) 

summary(df1)

df2 <- df1 %>% 
  dplyr::select(siteID, flow, bt, qft) %>% 
  dplyr::group_by(siteID) %>%
  dplyr::summarise_each(funs(
    mean(., na.rm=T), 
    sd(., na.rm=T),
    n()))

df2

@AllanT 感谢您的时间和帮助。我在修复了拼写错误后运行了代码,但仍然收到相同的错误消息。 - shiny
也许更新一下你的软件包?R 3.2.5? - AllanT
1
你可以尝试重新启动R并再次运行吗?确保除了dplyr、shiny和ggplot2之外没有加载其他软件包。 - Shorpy
2
尝试逐个添加软件包并每次重新运行代码,直到出现错误为止... - Ben Bolker
@BenBolker 感谢您的时间和帮助。 - shiny
显示剩余4条评论
2个回答

3

我现在已经能够重现这个错误。引起问题的软件包是:EcoHydRology_0.4.12

不幸的是,使用detach("package:EcoHydRology", unload=TRUE)命令无法消除错误。重新启动我的R会话确实解决了问题,但只要我再次加载EcoHydRology软件包,问题就会再次出现。一个解决方案是在不加载该软件包的情况下运行代码。


更新:我相信我现在已经解决了这个问题。

当我运行library(EcoHydRology)并仔细查看控制台消息时,我注意到它打印出:

The following object is masked from ‘package:dplyr’:

    %>%

这不太好,并且使用 dplyr::%>% 无法轻松加载(不起作用)。

然而,如果我们运行 library(EcoHydRology),然后再次运行 library(dplyr)dplyr 包将会掩盖其他包中的 %>% 并切换回它自己的版本。现在运行代码,就不会出现错误了。


最初建议将 funs() 更改为 dplyr::funs(),但这并没有解决问题。(编辑以删除多余文本)


1
你是指第二个代码块中的 dplyr::funs(),对吗? - Ben Bolker
1
更正为 dplyr,是的,那就是我想说的。很遗憾它没起作用。 - enpitsu
1
你绝对值得这份奖励!(确保先加载EcoHydRology,然后再加载dplyr能解决问题吗?) - Ben Bolker
3
看起来这段代码来自运算符包(因为EcoHydRology从运算符包中导入了%>%,用于快速将对象写入文件)。或许值得给该包的开发者发送电子邮件,因为前向通道太常见了,他们应该考虑重构代码,不再依赖这个函数(该函数可能会被废弃)。 - Shorpy
3
是的,这就是他们的NAMESPACE文件的样子:

导出所有名称

exportPattern(".")

导入列在Imports或Depends中的所有包

import( operators, topmodel, DEoptim, XML )因此看起来所有东西都被导出了。
- enpitsu
显示剩余3条评论

1
基本上,我们需要检查是否已加载package:EcoHydRology,首先分离package:dplyr,然后分离package:EcoHydRology,以使重新加载package:dplyr......
解决方法是使用require()检查package:EcoHydRology是否已加载。 require()优于library()的优点在于它返回TRUEFALSE,使我们能够使用它来评估其操作的结果。
因此,如果package:EcoHydRology已经加载(TRUE),我们可以在if结构中指定分离顺序,以正确卸载软件包dplyr,然后再是EcoHydRology
现在,我们可以轻松加载所需的package:dplyr
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(dplyr)
library(EcoHydRology)
library(ggplot2)
library(shiny)
if ( require(EcoHydRology) ) {
  detach("package:dplyr", unload = TRUE)
  detach("package:EcoHydRology", unload = TRUE)
  require(dplyr)
}

ui <- fluidPage(
  titlePanel(
    sidebarLayout(
      sidebarPanel(
        tags$h1(tags$strong("Shiny app")),
        fileInput("file", "Upload your file"),
        width =2),
      mainPanel(width =10,
        uiOutput("tb")))))

#server

server <- function(input,output){
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()}
    read.csv(file1$datapath, header=TRUE, sep=',')
  })
  output$sum <- renderTable({
    if(is.null(data())){return ()}

    df_summary <- data()

    df_summary1 <- df_summary %>%
      dplyr::select(cut, color, carat, price)
    summary(df_summary1)
  })

  output$table <- renderDataTable({
    if(is.null(data())){return ()}
    data()
  })

  output$stats <- renderDataTable({
    if(is.null(data())){return ()}

    diam1 <- data()
    print(
      diam_stats <- diam1 %>%
        dplyr::select(cut, color, carat) %>%
        dplyr::arrange(cut, color) %>%
        dplyr::group_by(cut, color) %>%
        dplyr::filter(!is.na(carat)) %>%
        dplyr::summarise_each(funs(
          mean(., na.rm=T),
          sd(., na.rm=T),
          n()))
    )
  })

  output$tb <- renderUI({
    if(is.null(data()))
      h5()
    else
      tabsetPanel(type="tab",
        tabPanel(h3("Summary", align="center"),
          tableOutput("sum")),
        tabPanel(h3("Data"),
          dataTableOutput("table")),
        tabPanel(h3("Stats"),
          dataTableOutput("stats")))
  })
}
shinyApp(ui = ui, server = server)

感谢您的时间和帮助。现在使用这个可重现的例子它运行得很好。然而,我忘记在上面的问题中添加,在我的闪亮应用程序的一个选项卡中,我同时使用两个包dplyr和EcoHydrRology,就像这个问题中一样: http://stackoverflow.com/questions/36807155/r-function-creates-a-new-data-frame-within-my-data-frame df%>% group_by(site) %>% do(cbind(., BaseflowSeparation(.$flow, filter_parameter = 0.925, passes = 3))) %>% ungroup()您的解决方案修复了所有选项卡中的所有错误,除了我同时使用这两个包的选项卡。 - shiny
我的建议是创建一个中间结果。例如,您可以创建EcoHydrRology结果,然后卸载EcoHydrRology,并将中间结果插入到dplyr管道中。 - Technophobe01
感谢您的时间和帮助。分离和连接程序包确实会影响我使用EcoHydRology的选项卡。因此,它将仅对此选项卡正常工作,或者对于使用dplyr但不使用EcoHydRology选项卡的所有其他选项卡都可以正常工作。然而,先加载EcoHydRology,然后再加载dplyr可以解决这个问题。 - shiny
请查看问题中的更新。我已经发布了代码。 - shiny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接