0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

R markdownで画像をfor loopで挿入する

Last updated at Posted at 2021-01-09

r markdownで画像を表示させる場合,knitr::include_graphicsを用いられることが多い.
しかし, knitr::include_graphicsを用いてknit時にfor loopで画像を表示させることはできない. これは, knitrのレポジトリのissueでも言及されており, catを用いることで, この問題を回避できる.
pythonのformatメソッドのような用途のglueを用いることで,loopを用いてrmarkdownに記載することができる.

# 以下の例は画像が表示されない
```{r}
path <- "../hoge/fuga/"
image <- ["a", "b", "c"]
for i in (1:3){
  mage <- glue("{path}/image[i].png")
  knitr::include_graphics(image)
}
# forを用いて画像挿入可能
```{r, results='asis',  out.width = '80%'}
path <- "../hoge/fuga/"
image <- ["a", "b", "c"]
for (i in 1:3){
  image <- glue("{path}/image[i].png")
  cat("![](",image,")")
  cat("\n\n")
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?