当前位置:  开发笔记 > 编程语言 > 正文

你如何在R markdown中创建一个子节

如何解决《你如何在Rmarkdown中创建一个子节》经验,为你挑选了2个好方法。

我正在尝试创建一个如下所示的R降价.在一个名为app的向量中,我将有几个应用程序,将会创建一个pdf文件

---
title: "Java Based Apps"
date: "January 13, 2017"
output: 
  pdf_document: 
    number_sections: yes
    toc: true
    highlight: zenburn
    fig_width: 7
    fig_height: 6
    fig_caption: true

tables: yes
keep_tex: true
fontsize: 12
---



```{r message=FALSE, results = 'asis', echo=FALSE, warning=FALSE}

app<-c("Sample APP")

for (i in app){

    cat(paste("## ", "- Correlation Analysis between performance KPI's"))
    cat("\n")
    m<-corrplot(M, method="number")

    cat(paste("## ", "- JVM %CPU Usage"))
    cat("\n")

    print(ggplot(data, aes(Date, JVM_CPU, group=JVM))+geom_point()+geom_smooth(method="lm",se=F)+theme_bw()+
    ggtitle(paste(i, " - JVM %CPU Usage/15 Minute Interval"))+facet_wrap(~JVM, scale="free"))

    cat(paste("## ", "- JVM Heap Usage"))
    cat("\n")

    print(ggplot(data, aes(Date, JVM_Mem, group=JVM))+geom_point()+geom_smooth(method="lm",se=F)+theme_bw()+
    ggtitle(paste(i, " - JVM Memory Usage/15 Minute Interval"))+facet_wrap(~JVM, scale="free")+ylab("Memory Usage/MG"))

 }
```

输出需要像这样:

Sample App
   - Correlation Analysis between performance KPI's
   - JVM %CPU Usage
   - JVM Heap Usage

等等

我看到了这个:

Sample App
   - Correlation Analysis between performance KPI's

但其他人不是作为小节创建的.知道我在这里做错了什么吗?



1> Ryan Morton..:

cat("\n\n")在每个绘图后添加以添加空间.这应该有助于使##工作正确地分离子部分.



2> 小智..:

如果你将问题分开则更容易.我创建了一些虚假数据.

您可以拥有main.Rmd运行for循环的位置,例如:


title: "Java Based Apps"
date: "January 13, 2017"
output: 
  pdf_document: 
    number_sections: yes
    toc: true
    highlight: zenburn
    fig_width: 7
    fig_height: 6
    fig_caption: true
tables: yes
keep_tex: true
fontsize: 12
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F, message = F, warning = F)
library(tibble)
library(lubridate)
library(dplyr)
library(corrplot)
library(ggplot2)
library(knitr)
# Create some sample data
set.seed(2887)
data <- tibble(
  app = sort(rep(c("sample app", "sample app 2"), 500))
  , Date = rep(seq(now() - as.difftime(7, unit="days"), now(), length.out = 500), 2)
  , JVM = sample(c("a", "b", "c"), size = 1000, replace = T)
  , JVM_CPU = runif(1000, 0, 100)
  , JVM_Mem = runif(1000, 0, 100)
)
```

```{r}
app <- sort(unique(data$app))

out <- NULL
for (i in app){
  data.filtered <- dplyr::filter(data, app == i)
  out <- c(out, knit_child('app_template.Rmd', quiet = TRUE))
}
```

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

你将迭代我在这里调用的模板,app_template.Rmd看起来像这样(注意它没有yaml:

\pagebreak

# `r i`

## Correlation Analysis between performance KPI's

```{r, fig.width=5, fig.height=5}
m <- matrix(rnorm(1000), ncol = 10)
M <- cor(m)
corrplot(M, method="number")
```

## JVM %CPU Usage

```{r, fig.width=10, fig.height=5}
ggplot(data.filtered, aes(Date, JVM_CPU, group=JVM)) +
  geom_point() +
  geom_smooth(method = "lm", se = F) + 
  theme_bw() +
  ggtitle(paste(i, " - JVM %CPU Usage/15 Minute Interval")) + 
  facet_wrap(~ JVM, scale = "free")
```

## JVM Heap Usage

```{r, fig.width=10, fig.height=5}
ggplot(data.filtered, aes(Date, JVM_Mem, group = JVM)) + 
  geom_point() + 
  geom_smooth(method = "lm", se = F) + 
  theme_bw() +
  ggtitle(paste(i, " - JVM Memory Usage/15 Minute Interval")) + 
  facet_wrap(~ JVM, scale = "free") + 
  ylab("Memory Usage/MG")
```

如果你knit,main.Rmd你应该得到.pdf你的规格.

推荐阅读
手机用户2402852307
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有