【问题标题】:Errors when knitting RMarkdown to HTML (no graphs showing up in the HTML)将 RMarkdown 编织到 HTML 时出错(HTML 中没有显示图表)
【发布时间】:2020-04-26 08:39:07
【问题描述】:

关于编织 RMarkdown 的问题。

我在将 Rmarkdown 文件编织成 HTML/pdf 时遇到问题。当我在 Rmarkdown 文件中运行我的代码块时,一切都运行顺利(我的图表是用 ggplot 制作的),但是在编织时,我得到的输出没有图表和错误(eval 错误,ggplot 错误,打印错误)。 有人有这方面的经验吗?

错误:

eval(lhs, parent) 中的错误:找不到对象“iso3166”

ggplot(inci_100k, aes(long, lat, map.id=mapname,fill=inci)) 出错:找不到对象“inci_100k”

打印错误(INCIPLOT):找不到对象“INCIPLOT”

代码:

---
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

【问题讨论】:

  • 如果您显示您的降价代码和错误,它将更容易帮助您。
  • 好的,谢谢。我编辑了我的帖子。
  • 一个问题是您似乎没有任何几何图形来显示inci_100k 数据。你遇到了什么错误?
  • 使用 RMarkdown 在 Rstudio 中制作我的绘图没有任何错误。只有在将其编织为 HTML 时才会出错。我得到的情节错误是: eval 中的错误(lhs,父):未找到对象 'iso3166' ggplot 中的错误(inci_100k,aes(long,lat,map.id=mapname,fill=inci)):对象' inci_100k' 未找到。我添加了一张图片来显示 Rstudio 中的情节。
  • 错误信息告诉你问题所在。 iso3166 未在您的 RMarkdown 文件中定义。它可能是在您的 RStudio 环境中定义的,这就是该图在 RStudio 中有效的原因。如果你重新启动 RStudio,你会得到同样的错误。

标签: html r rstudio r-markdown


【解决方案1】:

在 R 中制作地图时,我也遇到过类似的问题。一种解决方法是创建图表并将其保存在本地并包含图像。在 R Markdown 中添加图片的语法是 ![alt text](path to image)

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2018-05-18
    • 2017-12-04
    • 2022-10-24
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多