为什么在Rmarkdown的编织过程中,一段R代码无法使用

时间:2019-08-16 14:25:40

标签: r dplyr knitr ggcorrplot

编织包含R代码块的Rmarkdown(R Studio)文档时,块返回

line 192 Error in stats::hclust(dd, method=hc.method) :
  must have n>= 2 objects to cluster Calls: <Anonymous> ... eval -> ggcorrplot -> .hc_cormat_order -> <Anonymous>

在R(R Studio)中运行并在markdown内运行时,同一段代码可以正常运行。这仅在编织期间发生!

从R文件复制/粘贴大块中没有重大更改的代码。添加了cache=FALSEecho=FALSE。没有帮助。 确实将每一行放在单独的块中。这表明错误不是在library (dplyr)行,而是在实际的ggcorrplot

library(dplyr)
dfNed_2b <- dfNed_2a %>% dplyr:: select(grep("GDP", names(dfNed_2a)), grep("GDY", names(dfNed_2a)), grep("GNP", names(dfNed_2a)))
dfNed_2b[is.na(dfNed_2b)] <- 0
corrNed_2b <- round(cor(dfNed_2b), 4)
library(ggcorrplot)
ggcorrplot(corrNed_2b, hc.order = TRUE, 
           type = "lower", 
           lab = TRUE, 
           lab_size = 2, 
           method="circle",
           tl.cex=7,
           colors = c("orangered1", "white", "darkolivegreen1"), 
           title="Correlogram GDP, GDY en GNP indicatoren", 
           ggtheme=theme_minimal)

从R代码中,我得到了很好的相关图。 Rmd中的编织返回:

line 192 Error in stats::hclust(dd, method=hc.method) :
must have n>= 2 objects to cluster Calls: <Anonymous> ... eval -> ggcorrplot -> .hc_cormat_order -> <Anonymous>

当实际错误在ggcorrplot行上时,错误似乎又回到了块的第一行

================================================ === 错误似乎仍然存在。将Rmd更改为仅使用外部R脚本 Rmd:         ### Correlatie plot van de GDP,GDY和GNP指标
        {r cache=FALSE} knitr::read_chunk('6e_corr_dfNed_2b.R')

    ```{r 6e_corr_dfNed_2b}
    ```

R:         ## ---- 6e_corr_dfNed_2b         图书馆(dplyr)         dfNed_2c <-dfNed_2b%>%dplyr :: select(grep(“ GDP”,名称(dfNed_2b)),         grep(“ GDY”,名称(dfNed_2b)),grep(“ GNP”,名称(dfNed_2b)))         dfNed_2c [is.na(dfNed_2c)] <-0         头(dfNed_2c,2)         库(ggplot2)         图书馆(秤)         库(ggcorrplot)         corrNed_2c <-round(cor(dfNed_2c),4)         ggcorrplot(corrNed_2c,hc.order = TRUE,                    类型=“下层”,                    lab = TRUE,                    lab_size = 2                    method =“ circle”,                    tl.cex = 7,                    颜色= c(“ orangered1”,“白色”,“ darkolivegreen1”),                    title =“相关图GDP,GDY和GNP指标”,                    ggtheme = theme_minimal)

我唯一能想到的是Knitr确实喜欢先前块中使用的库(dplyr)。认为由cache = FALSE解决的

0 个答案:

没有答案