简体   繁体   English

将 RMarkdown 编织到 HTML 时出错(HTML 中没有显示图表)

[英]Errors when knitting RMarkdown to HTML (no graphs showing up in the HTML)

Question about knitting RMarkdown.关于编织 RMarkdown 的问题。

I am having issues when knitting a Rmarkdown file to HTML/pdf.将 Rmarkdown 文件编织为 HTML/pdf 时遇到问题。 When I run my chunks of code in the Rmarkdown file everything runs smoothly (and I get my graphs made with ggplot) but when knitting I get an output with no graphs and errors (error in eval, error in ggplot, error in print).当我在 Rmarkdown 文件中运行我的代码块时,一切运行顺利(并且我用 ggplot 制作了我的图表)但是在编织时我得到了一个没有图表和错误的输出(eval 中的错误,ggplot 中的错误,打印中的错误)。 Does anyone have experience with this?有任何人对此有经验吗?

The errors:错误:

Error in eval(lhs, parent): object 'iso3166' not found eval(lhs, parent) 中的错误:找不到对象“iso3166”

Error in ggplot(inci_100k, aes(long, lat, map.id=mapname,fill=inci)): object 'inci_100k' not found ggplot(inci_100k, aes(long, lat, map.id=mapname,fill=inci)) 中的错误:找不到对象“inci_100k”

Error in print(INCIPLOT): object 'INCIPLOT' not found打印错误(INCIPLOT):找不到对象“INCIPLOT”

The code:编码:

---
title: "R Markdown MAP"
author: "Alexandra V"
date: "1/4/2020"
output:
  html_document: default
  pdf_document: default
  word_document: default
---
```{r,echo = FALSE, warning = FALSE, message=FALSE, error=TRUE}
knitr::opts_chunk$set(cache=TRUE)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(error = TRUE)
```

Loading the packages we will need for the following analysis.
```{r echo=FALSE, warning=FALSE}
library(tidyverse)
library(lubridate)
library(ggmap)
library(countrycode)
library(grid)
library(gridExtra)
```

To only keep the data needed to make a worldmap of TB incidences, only the relevant data will be taken from the TB_burden_countries_2020-01-04.csv file. Column 1: country names, column 3: iso3 (country codes), column 6: years, column 8: e_inc_100k (estimated incidence all TB forms per 100.000). To make the file easier to work with the names of the columns will be changed to: country, code, year and inci respectively. 

```{r, message=FALSE}
TB.burden <- read.csv("TB_burden_countries_2020-01-04.csv")
TBworldINC.map <- as.data.frame(TB.burden[,c(1,3,6,8)], drop=false)
write.csv(TBworldINC.map, 'TBworldINC.map.csv', row.names = FALSE) 
tb.INC <- read_csv("TBworldINC.map.csv") %>%
  setNames(c("country", "code", "year", "inci"))
```

```{r}
world <- map_data("world")
tb_some_years <- tb.INC %>%
  filter(year %in% c(2005, 2010, 2015, 2018))
inci_100k <- tb_some_years %>%
  inner_join(iso3166 %>% select(a3, mapname), by = c(code = "a3")) %>%
  left_join(world, by = c(country = "region"))

INCIPLOT <- ggplot(inci_100k, aes(long, lat, map_id = mapname, 
                                                fill = inci)) +
  geom_map(map = world) +
  scale_fill_gradient(low = "blue", high = "yellow") +
  theme_void() +
  coord_map(xlim = c(-180, 180)) +
  labs(fill = "Incidence per year") +
  facet_wrap(~ year, ncol = 2)
print(INCIPLOT)
```

picture of the output I get in Rstudio我在 Rstudio 中得到的输出图片

I have had similar issues when making maps in R. One work around is to create your graphs and to save it locally and including the images.我在 R 中制作地图时遇到了类似的问题。一种解决方法是创建图形并将其保存在本地并包括图像。 The syntax for adding images in R Markdown is ![alt text](path to image)在 R Markdown 中添加图片的语法是 ![alt text](path to image)

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

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