小编MLe*_*ine的帖子

使用 Word 输出在 R Markdown 文档中循环 Flextables

我希望在单个 R Markdown 文档中打印多个 flextables。这在使用 html 输出时并不具有挑战性,但我需要 Word 输出。

对于 html 输出,以下 R Markdown 代码(示例取自https://davidgohel.github.io/flextable/articles/offcran/examples.html#looping-in-r-mardown-documents)生成具有多个 Flextables 的文档:

---
output:
  html_document: default

---

```{r}
library(htmltools)
library(flextable)

ft <- flextable(head(iris))
tab_list <- list()
for(i in 1:3){
  tab_list[[i]] <- tagList(
    tags$h6(paste0("iteration ", i)),
    htmltools_value(ft)
  )
}
tagList(tab_list)
```
Run Code Online (Sandbox Code Playgroud)

我一直无法使用 Word 文档输出获得等效的输出。How to knit_print flextable with loop in a rmd file 中提出的解决方案 同样适用于 html 输出,但我未能使其在 Word 输出中正确呈现。

任何有关与上述示例等效的 R Markdown Word 文档输出的建议都会非常有帮助!

r flextable knitr r-markdown

5
推荐指数
1
解决办法
606
查看次数

使用 sf 和 ggplot2 围绕 +/- 180 日期线绘图

我正在使用 sf 绘制一系列形状文件和点数据。该数据跨越经度 =/-180 的日期变更线。

sf::st_shift_longitude()处理这种情况并按预期绘制点。然而,在分配经度刻度线时,ggplot 的行为很奇怪。下面的代码提供了一个示例,该示例在日期线的一侧有两个点 - 注意 ggplot 添加了经度的逻辑刻度线。在第二个示例中,点跨越日期变更线。

编辑:添加了第三种情况,手动扩展了 x 限制。当这些变得足够大时,标线就会按预期绘制。然而,我只是通过实验才发现“足够大” xlim

library(sf)
library(rnaturalearth)
library(ggplot2)

#get basic world map 
world = ne_coastline(scale = "medium", returnclass = "sf")

#example 1: without dateline
#minimal data- two points on one side of dateline
sites = data.frame(longitude = c(-173.9793, -177.7405), latitude = c(52.21415, 51.98994))

#convert to sf object
sites = st_as_sf(sites, coords = c("longitude", "latitude"), crs = 4326)

#plot with ggplot
ggplot()+
  geom_sf(data = world, fill = 'transparent')+ …
Run Code Online (Sandbox Code Playgroud)

r spatial ggplot2 r-sf

5
推荐指数
1
解决办法
1279
查看次数

标签 统计

r ×2

flextable ×1

ggplot2 ×1

knitr ×1

r-markdown ×1

r-sf ×1

spatial ×1