简体   繁体   English

在 R markdown 中安装 ggplot2 编织错误

[英]Installing ggplot2 knitting error in R markdown

I keep getting this error when i try to knit my document in R, after attempting to install GGplot package.在尝试安装 GGplot 包后,当我尝试在 R 中编织我的文档时,我不断收到此错误。

在此处输入图片说明

The error is with line 71 but there is no code on line 71. When i run the section I get this message in the console:错误出现在第 71 行,但第 71 行没有代码。当我运行该部分时,我在控制台中收到此消息:

trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.1/ggplot2_3.3.5.tgz'
Content type 'application/x-gzip' length 4125542 bytes (3.9 MB)
==================================================
downloaded 3.9 MB

The downloaded binary packages are in
    /var/folders/km/tr6lt7kx6h71d83606qh91hc0000gn/T//Rtmp4M6La0/downloaded_packages
RStudio Community is a great place to get help: https://community.rstudio.com/c/tidyverse

The message you are getting is due to the fact that install.packages() requires to specify a CRAN mirror when run not interactively, eg install.packages("ggplot2", repos = "https://cloud.r-project.org") for the cloud mirror.您收到的消息是由于install.packages()在非交互式运行时需要指定 CRAN 镜像,例如install.packages("ggplot2", repos = "https://cloud.r-project.org")为云镜像。 If you run it from the REPL, it will just prompt you to select one from a list.如果您从 REPL 运行它,它只会提示您从列表中选择一个。

The reason it is flagging an error at that line is due to the fact that when knitting an RMarkdown document, the error messages usually refer to the chunk, not the specific lines of code.它在该行标记错误的原因是因为在编写 RMarkdown 文档时,错误消息通常指的是块,而不是特定的代码行。 The error message refers to the first line of code of the chunk, but not necessarily where the error happens.错误信息指的是块的第一行代码,但不一定是错误发生的地方。

As Phil mentioned in the comment, it's bad practice to include install.packages() in an Rmd document.正如 Phil 在评论中提到的,在 Rmd 文档中包含install.packages()是不好的做法。 The best way is usually to insert a setup chunk at the beginning, eg with {r setup, include = FALSE} , which will then contain all the packages you need to load (with library() or require() ), leaving the user to install them as needed.最好的方法通常是在开头插入一个设置块,例如使用{r setup, include = FALSE} ,它将包含您需要加载的所有包(使用library()require() ),让用户根据需要安装它们。 This also avoids the output from the loading package to show up in the knitted document.这也避免了加载包的输出显示在编织文件中。

If you really want to force the package installation in your document, you can use p_load() from thepacman package, but I would advise against in an Rmarkdown document anyway, since it might be hard to trace back any problem if it fails.如果你真的想在你的文档中强制安装包,你可以使用pacman包中的p_load() ,但我还是建议不要在 Rmarkdown 文档中使用,因为如果失败,可能很难追溯任何问题。

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

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