Shiny:dplyr返回错误消息

时间:2016-04-28 03:26:57

标签: r shiny dplyr

使用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文件,用于下面的闪亮应用

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)

我在摘要标签

中收到此错误
  

没有适用的方法来选择_&#39;应用于课程对象&#34; c(&#39; standardGeneric&#39;,&#39; genericFunction&#39;,&#39; function&#39;,&#39; OptionalFunction&#39;,&# 39; PossibleMethod&#39;,&#39; optionalMethod&#39;)&#34;

和统计信息标签中的此错误

  

论证&#34; funs&#34;缺少,没有默认

我正在使用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  

更新 May_04

我没有意识到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

2 个答案:

答案 0 :(得分: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 :(得分:1)

基本上,我们需要检查是否已加载package:EcoHydRology,先分离package:dplyr,然后分离package:EcoHydRology,以便重新加载package:dplyr ... < / p>

解决方案是使用require()来检查是否已加载package:EcoHydRologyrequire()优于library()的优势在于它返回TRUEFALSE,使我们能够使用它来评估其操作结果。

因此,如果package:EcoHydRology已经加载(TRUE),我们可以在if构造中指定分离顺序,以正确的顺序dplyr卸载软件包,然后{ {1}}。

我们现在可以毫无问题地加载所需的EcoHydRology

package:dplyr