RMarkdown - 有条件的标题(表格前的文本标题) [英] RMarkdown - having the title (text heading before a table) be conditional

查看:48
本文介绍了RMarkdown - 有条件的标题(表格前的文本标题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个参数化的 R Markdown.基本上,我通过case_id"过滤数据帧.字段并输出这些过滤结果的表格.我在循环中呈现 RMarkdown 文档,循环遍历每个唯一的 case_id.对于数组中的每个 case_id,我检查数据框,如果它存在,我输出一个过滤表,显示该 case_id 的结果.这里的工作示例:

I have an R Markdown that is parameterized. Basically, I'm filtering a dataframe by a "case_id" field and outputting a table of those filtered results. I render the RMarkdown document in a loop that cycles through each unique case_id. For each case_id in an array, I check the dataframe, and if it exists, I output a filtered table showing results for that case_id. Working example here:

RMarkdown:

---
title: "My Title"
output: 
  html_document:
params:
   case: case

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


## My Table

```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
data <- read.csv(text="case_id,event,group,start,end,color,line.color
                       3,event_1,Project,2020-06-15,2020-10-15,#ffffff,#FF0000
                       3,event_1,Meetings,2020-07-30,2020-07-30,#ffffff,#0000ff
                       5,event_1,Meetings,2020-08-30,2020-08-30,#ffffff,#FF0000
                       5,event_1,Meetings,2020-08-30,2020-08-30,#ffffff,#FF0000
                       5,event_1,Meetings,2020-08-30,2020-08-30,#ffffff,#FF0000
                       9,event_1,Meetings,2017-01-15,2017-01-15,#ffffff,#FF0000")
data$case_id <- as.numeric(data$case_id)
if(any(params$case==data$case_id)) {
    
   #Filter PDMP by the selected case
    data.filtered <- data %>%
                        filter(params$case==case_id)
    
    data.filtered <- data.filtered %>%
      filter(!is.na(start)&!is.na(end))
    
    pdmp_numRows <- nrow(data.filtered)

    if (pdmp_numRows>0) { 

    #Print table of prescriptions
    data.filtered %>%
      arrange(start)%>%
      kable() %>%  kable_styling(bootstrap_options = "striped","condensed", font_size = 12)
      
    }
    
 }


```

这是呈现它的 R 脚本:

And here is the R script that renders it:

library(dplyr)
library(knitr)
library(rmarkdown)


#--------------------- Render RMarkdown Document ---------------------
case_array <- c(1:10)

render_data <- function(case) {
  # assuming the output format of input.Rmd is PDF
  rmarkdown::render(
    "C:/Temp/test_kable.Rmd",
    output_file = paste0('//my_directory/', 'test_cable_ex_',case, '.html'),
    params = list(case=case),
    envir = parent.frame()
  )
}

for (case in case_array) {
  render_data(case)
}

表格按预期输出.问题是,当没有制作表格时(当过滤数据框的行 = 0 时),我不希望表格标题为我的表格".露面.我只想要我的桌子"表存在时显示.

The tables are output as expected. The problem is that when no table is made (when rows of the filtered dataframe = 0 ), I don't want the table heading "My Table" to show up. I only want "My Table" to show up when the table exists.

这就是我想要的:

这就是我不想要的:

提前致谢!

推荐答案

您可以通过将表格标题包装在适当的 if 语句中来实现.

You can do this by wrapping the table heading in appropriate if statements.

   ---
    title: "My Title"
    output: 
      html_document:
    params:
       case: case
    ---

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```

    ```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
    data <- read.csv(text="case_id,event,group,start,end,color,line.color
                           3,event_1,Project,2020-06-15,2020-10-15,#ffffff,#FF0000
                           3,event_1,Meetings,2020-07-30,2020-07-30,#ffffff,#0000ff
                           5,event_1,Meetings,2020-08-30,2020-08-30,#ffffff,#FF0000
                           5,event_1,Meetings,2020-08-30,2020-08-30,#ffffff,#FF0000
                           5,event_1,Meetings,2020-08-30,2020-08-30,#ffffff,#FF0000
                           9,event_1,Meetings,2017-01-15,2017-01-15,#ffffff,#FF0000")
    data$case_id <- as.numeric(data$case_id)
    param_check <- any(params$case==data$case_id)
    if (param_check) {
        
       #Filter PDMP by the selected case
        data.filtered <- data %>%
                            filter(params$case==case_id)
        
        data.filtered <- data.filtered %>%
          filter(!is.na(start)&!is.na(end))
        
        pdmp_numRows <- nrow(data.filtered)
        
    }
    ```

    `r  if (param_check) { if (pdmp_numRows > 0) {"# My Table"} }`

    ```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
    if(param_check) {    
      if (pdmp_numRows>0) { 
    
        #Print table of prescriptions
            data.filtered %>%
          arrange(start)%>%
          kable() %>%  kable_styling(bootstrap_options = "striped","condensed", font_size = 12)
        
      }
      
    }
    
    
    ```

这篇关于RMarkdown - 有条件的标题(表格前的文本标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆