错误编织Flex仪表板rmarkdown dplyr

时间:2018-11-21 08:11:55

标签: r dplyr r-markdown knitr

在rmarkdown中编织到html flex_dashboard时出现以下错误。

UseMethod(“ mutate_”)中的错误:   没有适用于“ mutate_”的适用方法应用于“函数”类的对象 调用:... withVisible->-> mutate-> mutate.default-> mutate_ 执行停止

收到此错误很奇怪,因为我看不到自己的错误。我在做什么错了?

---
title: "Untitled"
output: 
   flexdashboard::flex_dashboard:
   orientation: rows
   vertical_layout: fill
---

```{r setup, include = FALSE}
# --- This chunk sets the general options for compiling the final output 
document with knitr

knitr::opts_chunk$set(echo = FALSE,          # display code in output
                      warning = FALSE,      # display code warnings in document
                      results = 'asis',     # passthrough results 
                      error = FALSE,         # display error messages in document
                      message = FALSE,      # display code messages in document
                      fig.align = 'center', # type of alignment of figures
                      eval = TRUE           # evaluate code in code chunk
                      )
# there are more options available to tune the way the document can be compiled.

# set global options for table output in notebook when not compiled.
options(knitr.table.format = "html")
```


```{r libraries, include = FALSE}
# library loads
library(flexdashboard)
library(dygraphs)
library(dplyr)
library(lubridate)
library(xts)
library(zoo)
library(magrittr)
```

```{r dataprep, include = FALSE}
# create promodata
df %>%
  mutate(promo_active = if_else((datum_besteld >= datum_start_promo & 
         datum_besteld <= datum_end_promo), 1, 0, missing = NULL),
         weekday = weekdays(datum_besteld),
         weekend = if_else(weekday == "Saturday" | weekday == "Sunday", 1, 0, missing = NULL)
         )
```

df的提取:

structure(list(ktk_nr_ts = c(2.01703127263e+18, 2.01703127302e+18, 
2.01703127304e+18, 2.01703127263e+18, 2.01703127263e+18, 
2.01703124599e+18, 
2.01703127263e+18, 2.01807087263e+18, 2.01807087263e+18, 
2.01807087263e+18, 
2.01807087314e+18, 2.01807084123e+18, 2.01807084123e+18, 
2018070836540006400, 
2018070836540006400, 2.01807083747e+18, 2.01807083747e+18, 
2.01807084121e+18, 
2.01807084121e+18, 2.01807083637e+18, 2.01807084232e+18, 
2.01807083829e+18, 
2.01807083829e+18, 2018070841230006784, 2018070841230005760, 
2.01807084121e+18, 2.01807084121e+18, 2.01807084121e+18, 
2.01807087263e+18, 
2.01807087263e+18), omzet = c(898, 377, 341.1, 29.95, 1899, 48.75, 
349, 36.72, 12.16, 21.74, 29.95, 23.96, 23.96, 139, 19.95, 44.97, 
37.46, 18.36, 44.96, 21.95, 14.95, 46.71, 89.99, 0, 166, 52.42, 
35.12, 44.99, 49.95, 429), datum_besteld = structure(c(17237, 
17237, 17237, 17237, 17237, 17227, 17237, 17716, 17716, 17716, 
17716, 17715, 17715, 17714, 17715, 17713, 17713, 17716, 17716, 
17715, 17717, 17708, 17708, 17704, 17717, 17712, 17712, 17712, 
17720, 17720), class = "Date"), datum_start_promo = structure(c(2932896, 
2932896, 17232, 2932896, 2932896, 2932896, 2932896, 17712, 17713, 
17713, 2932896, 17712, 17712, 2932896, 2932896, 17712, 17712, 
17712, 17712, 2932896, 2932896, 17687, 2932896, 2932896, 2932896, 
17712, 17712, 2932896, 2932896, 2932896), class = "Date"), datum_end_promo 
= structure(c(2932896, 
2932896, 17253, 2932896, 2932896, 2932896, 2932896, 17743, 17727, 
17727, 2932896, 17743, 17743, 2932896, 2932896, 17743, 17743, 
17743, 17743, 2932896, 2932896, 17711, 2932896, 2932896, 2932896, 
17743, 17743, 2932896, 2932896, 2932896), class = "Date")), row.names = 
c(NA, 
30L), class = "data.frame")

sessionInfo()

sessionInfo()
    R version 3.5.1 (2018-07-02)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Red Hat Enterprise Linux Server 7.5 (Maipo)

Matrix products: default
BLAS: /home/AD/jbr1ftr/.conda/envs/r-jl/lib/R/lib/libRblas.so
LAPACK: /home/AD/jbr1ftr/.conda/envs/r-jl/lib/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] bindrcpp_0.2.2        xts_0.11-0            zoo_1.8-3             dygraphs_1.1.1.6      flexdashboard_0.5.1.1 iml_0.5.1             doFuture_0.6.0       
 [8] iterators_1.0.10      foreach_1.5.0         future_1.9.0          kableExtra_0.9.0      knitr_1.20            ruler_0.1.3           AMR_0.2.0            
[15] table1_1.1            skimr_1.0.3           descriptr_0.4.1       corrr_0.3.0           plotROC_2.2.1         pROC_1.12.1           RANN_2.6             
[22] caret_6.0-80          lattice_0.20-35       magrittr_1.5          odbc_1.1.6            DBI_1.0.0             glue_1.3.0            rvest_0.3.2          
[29] xml2_1.2.0            httr_1.3.1            jsonlite_1.5          forcats_0.3.0         tibble_1.4.2          ggvis_0.4.3           ggplot2_3.0.0        
[36] purrr_0.2.5           stringr_1.3.1         lubridate_1.7.4       dplyr_0.7.6           tidyr_0.8.1           readr_1.1.1           readxl_1.1.0         
[43] RevoUtils_11.0.1      RevoUtilsMath_11.0.0 

loaded via a namespace (and not attached):
 [1] colorspace_1.3-2   class_7.3-14       rprojroot_1.3-2    base64enc_0.1-3    pls_2.6-0          rstudioapi_0.7     DRR_0.0.3          listenv_0.7.0      bit64_0.9-7       
[10] mvtnorm_1.0-8      prodlim_2018.04.18 codetools_0.2-15   splines_3.5.1      robustbase_0.93-2  libcoin_1.0-1      RcppRoll_0.3.0     Formula_1.2-3      Metrics_0.1.4     
[19] broom_0.5.0        ddalpha_1.3.4      kernlab_0.9-26     sfsmisc_1.1-2      shiny_1.1.0        compiler_3.5.1     backports_1.1.2    assertthat_0.2.0   Matrix_1.2-14     
[28] lazyeval_0.2.1     later_0.7.3        htmltools_0.3.6    tools_3.5.1        partykit_1.2-2     gtable_0.2.0       reshape2_1.4.3     Rcpp_0.12.18       cellranger_1.1.0  
[37] nlme_3.1-137       inum_1.0-0         timeDate_3043.102  gower_0.1.2        globals_0.12.1     mime_0.5           DEoptimR_1.0-8     MASS_7.3-50        scales_0.5.0      
[46] ipred_0.9-6        hms_0.4.2          promises_1.0.1     yaml_2.2.0         rpart_4.1-13       stringi_1.2.4      checkmate_1.8.5    lava_1.6.2         geometry_0.3-6    
[55] rlang_0.2.1        pkgconfig_2.0.1    evaluate_0.11      bindr_0.1.1        htmlwidgets_1.2    recipes_0.1.3      CVST_0.2-2         bit_1.1-14         tidyselect_0.2.4  
[64] plyr_1.8.4         R6_2.2.2           dimRed_0.1.0       pillar_1.3.0       withr_2.1.2        survival_2.42-3    abind_1.4-5        nnet_7.3-12        crayon_1.3.4      
[73] rmarkdown_1.10     grid_3.5.1         data.table_1.11.4  blob_1.1.1         ModelMetrics_1.1.0 digest_0.6.15      xtable_1.8-2       httpuv_1.4.5       keyholder_0.1.2   
[82] glmnet_2.0-16      stats4_3.5.1       munsell_0.5.0      viridisLite_0.3.0  magic_1.5-8

2 个答案:

答案 0 :(得分:1)

我不确定,但是我想您忘记了在文件中包含df吗?在我的情况下,以下另存为test.Rmd的脚本起作用了:

---
title: "Untitled"
output: 
   flexdashboard::flex_dashboard:
   orientation: rows
   vertical_layout: fill
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = FALSE,          # display code in output
                      warning = FALSE,      # display code warnings in document
                      results = 'asis',     # passthrough results 
                      error = FALSE,         # display error messages in document
                      message = FALSE,      # display code messages in document
                      fig.align = 'center', # type of alignment of figures
                      eval = TRUE           # evaluate code in code chunk
                      )
# there are more options available to tune the way the document can be compiled.

# set global options for table output in notebook when not compiled.
options(knitr.table.format = "html")
```


```{r libraries, include = FALSE}
# library loads
library(flexdashboard)
library(dygraphs)
library(dplyr)
library(lubridate)
library(xts)
library(zoo)
library(magrittr)
```

```{r dataprep, include = FALSE}
df <- structure(list(ktk_nr_ts = c(2.01703127263e+18, 2.01703127302e+18, 
2.01703127304e+18, 2.01703127263e+18, 2.01703127263e+18, 
2.01703124599e+18, 
2.01703127263e+18, 2.01807087263e+18, 2.01807087263e+18, 
2.01807087263e+18, 
2.01807087314e+18, 2.01807084123e+18, 2.01807084123e+18, 
2018070836540006400, 
2018070836540006400, 2.01807083747e+18, 2.01807083747e+18, 
2.01807084121e+18, 
2.01807084121e+18, 2.01807083637e+18, 2.01807084232e+18, 
2.01807083829e+18, 
2.01807083829e+18, 2018070841230006784, 2018070841230005760, 
2.01807084121e+18, 2.01807084121e+18, 2.01807084121e+18, 
2.01807087263e+18, 
2.01807087263e+18), omzet = c(898, 377, 341.1, 29.95, 1899, 48.75, 
349, 36.72, 12.16, 21.74, 29.95, 23.96, 23.96, 139, 19.95, 44.97, 
37.46, 18.36, 44.96, 21.95, 14.95, 46.71, 89.99, 0, 166, 52.42, 
35.12, 44.99, 49.95, 429), datum_besteld = structure(c(17237, 
17237, 17237, 17237, 17237, 17227, 17237, 17716, 17716, 17716, 
17716, 17715, 17715, 17714, 17715, 17713, 17713, 17716, 17716, 
17715, 17717, 17708, 17708, 17704, 17717, 17712, 17712, 17712, 
17720, 17720), class = "Date"), datum_start_promo = structure(c(2932896, 
2932896, 17232, 2932896, 2932896, 2932896, 2932896, 17712, 17713, 
17713, 2932896, 17712, 17712, 2932896, 2932896, 17712, 17712, 
17712, 17712, 2932896, 2932896, 17687, 2932896, 2932896, 2932896, 
17712, 17712, 2932896, 2932896, 2932896), class = "Date"), datum_end_promo 
= structure(c(2932896, 
2932896, 17253, 2932896, 2932896, 2932896, 2932896, 17743, 17727, 
17727, 2932896, 17743, 17743, 2932896, 2932896, 17743, 17743, 
17743, 17743, 2932896, 2932896, 17711, 2932896, 2932896, 2932896, 
17743, 17743, 2932896, 2932896, 2932896), class = "Date")), row.names = 
c(NA, 
30L), class = "data.frame")

df %>%
  mutate(promo_active = if_else((datum_besteld >= datum_start_promo & 
         datum_besteld <= datum_end_promo), 1, 0, missing = NULL),
         weekday = weekdays(datum_besteld),
         weekend = if_else(weekday == "Saturday" | weekday == "Sunday", 1, 0, missing = NULL)
         )
```

重要提示:Rmd确实很难调试。如果仍然遇到问题,只需在控制台中逐行运行代码即可。

答案 1 :(得分:0)

您可以一次尝试下面的代码

```{r include = FALSE}
# create promodata
df2<-df %>%
  mutate(promo_active = if_else((datum_besteld >= datum_start_promo & 
         datum_besteld <= datum_end_promo), 1, 0, missing = NULL),
         weekday = weekdays(datum_besteld),
         weekend = if_else(weekday == "Saturday" | weekday == "Sunday", 1, 0, missing = NULL)
         )

df2
```