繁体   English   中英

Shiny:dplyr返回错误消息

[英]Shiny: dplyr returns error messages

使用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)

我在摘要选项卡中收到此错误

没有适用于'select_'的方法适用于类“c('standardGeneric','genericFunction','function','OptionalFunction','PossibleMethod','optionalMethod')”的对象

和stats选项卡中的此错误

参数“funs”缺失,没有默认值

我正在使用dplyr_0.4.3

有任何建议如何解决这些错误?

更新

下面是错误的打印屏幕

在此输入图像描述

这里出现在控制台中

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

现在已经能够重现此错误。 导致问题的包是: 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包将掩盖其他包中的%>%并切换回其版本。 现在运行代码,我们没有得到错误。


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

从本质上讲,我们需要检查是否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)

暂无
暂无

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

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