LoginSignup
1
1

More than 5 years have passed since last update.

Rmarkdown: list in tbl_df からタブセット&タブを自動生成する

Last updated at Posted at 2018-01-21

前回のRmarkdown: for文でtab & DT::datatableを生成するをもう少し発展させ、
より実践的なカンペを作成した。

以下のような、データや出力オブジェ等が列で収まっている今風 (???) なtbl_dfを対象とし、

data summary ggobj
1 <data.frame [50 × 2]> <S3: datatables> <S3: gg>
2 <data.frame [150 × 5]> <S3: datatables> <S3: gg>
3 <data.frame [60 × 3]> <S3: datatables> <S3: gg>

列名がセクション名、各要素名がタブ名、中身は当然中身、と出力させる。
汎用性を上げるべく (?)、要素の抜き出しにはpurrr::pluck()を用いた。

 ## データ準備部分 (ちょっとアホなコード…)
  #   組み込みデータ3つを list in tbl_df化し、出力用要素を列で付け加える

data_tbl <- list(cars = cars, iris = iris, ToothGrowth = ToothGrowth) %>% 
  tibble(data = .) %>% 
  mutate(summary = map(data, ~ summary(.) %>% 
                         xtable() %>% datatable()),
         ggobj = list(ggplot(data[[1]], aes(speed, dist)) + geom_point(),
                      ggplot(data[[2]], aes(Petal.Length, Sepal.Length, colour = Species)) + geom_point(),
                      ggplot(data[[3]], aes(dose, len, fill = supp, group = interaction(dose, supp))) + geom_boxplot()) %>% 
           set_names(names(data)))
 ## Rmarkdownでの出力用文字列を作成する
  #  中身抜き出し用の文字列に加えて、
  #    各列の先頭要素には、タブセット用文字列 "## 列名 {.tabset}\n\n" を
  #    全要素共通で、タブ用文字列 "### 要素名" を、頭につけ加える

out <- data_tbl %>%
  select(summary, ggobj) %>%                                         # 列選択
  imap(~ paste0(
    c(paste0("##", .y, " {.tabset}\n\n"), rep("", length(.) - 1)),   # タブセット生成用
    "###", names(.x),                                                # タブ生成用 (次行は中身抜き出し用)
    "\n```{r results='asis', echo=FALSE} \n\n pluck(data_tbl, '", .y, "', '",  names(.x), "') \n\n``` \n")
  ) %>%
  unlist()

あとはこいつを以下で出力させる

`r paste(knit(text = out), collapse = '\n')`



以下、.Rmdファイル全体

---
title: "test"
author: "hoge"
date: "`r Sys.Date()`"
output: 
  html_document:
    md_extensions: -ascii_identifiers
    toc: true
    toc_float: true  
    toc_depth: 3
---

```{r setup, include=FALSE, echo=FALSE}
# Rmarkdown options
knitr::opts_chunk$set(echo=FALSE, out.width="50%", message=FALSE, warning=FALSE)

# package load
library(tidyverse); library(xtable); library(DT); library(knitr)
```

```{r}
 ## データ準備
  #   組み込みデータ3つを list in tbl_df化し、出力用要素を列で付け加える

data_tbl <- list(cars = cars, iris = iris, ToothGrowth = ToothGrowth) %>% 
  tibble(data = .) %>% 
  mutate(summary = map(data, ~ summary(.) %>% 
                         xtable() %>% datatable()),
         ggobj = list(ggplot(data[[1]], aes(speed, dist)) + geom_point(),
                      ggplot(data[[2]], aes(Petal.Length, Sepal.Length, colour = Species)) + geom_point(),
                      ggplot(data[[3]], aes(dose, len, fill = supp, group = interaction(dose, supp))) + geom_boxplot()) %>% 
           set_names(names(data)))


 ## Rmarkdownでの出力用文字列を作成する
  #  中身抜き出し用の文字列に加えて、
  #    各列の先頭要素には、タブセット用文字列 "## 列名 {.tabset}\n\n" を
  #    全要素共通で、タブ用文字列 "### 要素名" を、頭につけ加える

out <- data_tbl %>%
  select(summary, ggobj) %>%                                         # 列選択
  imap(~ paste0(
    c(paste0("##", .y, " {.tabset}\n\n"), rep("", length(.) - 1)),   # タブセット生成用
    "###", names(.x),                                                # タブ生成用 (次行は中身抜き出し用)
    "\n```{r results='asis', echo=FALSE} \n\n pluck(data_tbl, '", .y, "', '",  names(.x), "') \n\n``` \n")
  ) %>%
  unlist()
```


`r paste(knit(text = out), collapse = '\n')`

## Let's Enjoy !!
1
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
1
1