错误编织 flex 仪表板 rmarkdown dplyr
Posted
技术标签:
【中文标题】错误编织 flex 仪表板 rmarkdown dplyr【英文标题】:error knitting flex dashboard rmarkdown dplyr 【发布时间】:2019-04-23 18:20:52 【问题描述】:在 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
【问题讨论】:
请dput
提供您的数据样本。
添加了一个 dput 样本
我的回答解决了你的问题吗?然后你可以这样标记它......
是的,直到现在还没有时间测试它。感谢您的支持!
这是一个老问题,接受的答案提到了它,但是编织 Rmd 会创建一个通常无法访问全局范围的新环境。这意味着需要在文档中定义 df
才能在其中访问它。它是此类文档可复制模型的一部分。在这种情况下,它本质上是一个错字,因为您实际上并没有定义代码(在编织过程中)可以找到它的数据
【参考方案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 真的很难调试。如果仍然遇到问题,只需在控制台中逐行运行代码即可。
【讨论】:
【参考方案2】:你可以试试下面的代码吗
```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
```
【讨论】:
嗨,感谢您的回复和时间。它不起作用。错误是一样的以上是关于错误编织 flex 仪表板 rmarkdown dplyr的主要内容,如果未能解决你的问题,请参考以下文章
为啥 RMarkdown 中的“Knit to pdf”会引发错误?
带有 knitr 和 Rmarkdown 的 Unicode