条件标题标题取决于代码块 R Markdown 的输出
Conditional title heading depending on output of code block R Markdown
我知道以前有人问过这个问题,但我仍在努力寻找适用于我的示例的答案。基本上,我有这个代码块可以读取数据帧的行数。如果数据框中有任何行,它将使用 kable()
输出 table。如果有零行,则不输出 table。
当 table IS 输出时,我希望在 table 上方有一个标题标题(只是粗体文本,即 "My Table"
)。当没有 table 输出时,我想要没有标题。我怎样才能做到这一点?我尝试在 r setup 块中使用 print_option
之类的 eval 语句,但没有成功。这是 stripped-down pseudo-code:
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
#Existing data frame my_df
numRows <- nrow(my_df)
if (numRows>0) {
#Print table
my_df %>%
arrange(my_field)%>%
kable() %>% kable_styling(bootstrap_options = "striped","condensed", font_size = 12)
}
```
我对使用 iris 数据集实现代码进行了最小的更改,它可以按照您的意愿工作。
没有行(也没有 header)
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
library(dplyr)
library(kableExtra)
#Existing data frame my_df
my_df <- iris[NULL, ]
numRows <- nrow(my_df)
```
`r if (numRows > 0) {"## This is your heading written in markdown"}`
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
if (numRows>0) {
#Print table
my_df %>%
arrange(Species) %>%
kable() %>%
kable_styling(bootstrap_options = "striped","condensed", font_size = 12)
}
```
有数据(和tableheader)
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
library(dplyr)
library(kableExtra)
#Existing data frame my_df
my_df <- iris[1:10, ]
numRows <- nrow(my_df)
```
`r if (numRows > 0) {"## This is your heading written in markdown"}`
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
if (numRows>0) {
#Print table
my_df %>%
arrange(Species) %>%
kable() %>%
kable_styling(bootstrap_options = "striped","condensed", font_size = 12)
}
```
我知道以前有人问过这个问题,但我仍在努力寻找适用于我的示例的答案。基本上,我有这个代码块可以读取数据帧的行数。如果数据框中有任何行,它将使用 kable()
输出 table。如果有零行,则不输出 table。
当 table IS 输出时,我希望在 table 上方有一个标题标题(只是粗体文本,即 "My Table"
)。当没有 table 输出时,我想要没有标题。我怎样才能做到这一点?我尝试在 r setup 块中使用 print_option
之类的 eval 语句,但没有成功。这是 stripped-down pseudo-code:
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
#Existing data frame my_df
numRows <- nrow(my_df)
if (numRows>0) {
#Print table
my_df %>%
arrange(my_field)%>%
kable() %>% kable_styling(bootstrap_options = "striped","condensed", font_size = 12)
}
```
我对使用 iris 数据集实现代码进行了最小的更改,它可以按照您的意愿工作。
没有行(也没有 header)
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
library(dplyr)
library(kableExtra)
#Existing data frame my_df
my_df <- iris[NULL, ]
numRows <- nrow(my_df)
```
`r if (numRows > 0) {"## This is your heading written in markdown"}`
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
if (numRows>0) {
#Print table
my_df %>%
arrange(Species) %>%
kable() %>%
kable_styling(bootstrap_options = "striped","condensed", font_size = 12)
}
```
有数据(和tableheader)
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
library(dplyr)
library(kableExtra)
#Existing data frame my_df
my_df <- iris[1:10, ]
numRows <- nrow(my_df)
```
`r if (numRows > 0) {"## This is your heading written in markdown"}`
```{r warning = FALSE, echo = FALSE, fig.height=10, fig.width=6}
if (numRows>0) {
#Print table
my_df %>%
arrange(Species) %>%
kable() %>%
kable_styling(bootstrap_options = "striped","condensed", font_size = 12)
}
```