Kable没有在if..elif ... else语句中打印一个表

时间:2016-10-24 15:57:56

标签: r knitr r-markdown

我已经使用此代码在if..elif..else语句中使用kable打印了不同的表。

```{r}
    if (sum(df_final2$change)!=0 && nrow(df_final[is.na(df_final$PEH.1),])!=0) {
        cat("The new peptides are:\n")
        kable(df_peptides_spectra2)
        cat("Decreased or increased:\n")
        kable(df_final3)
    } else if (sum(df_final2$change)==0 && nrow(df_final[is.na(df_final$PEH.1),])!=0) {
        cat("The new peptides are:")
        kable(df_peptides_spectra2)
        cat("The rest peptides are the same.")
    } else if (sum(df_final2$change)!=0 && nrow(df_final[is.na(df_final$PEH.1),])==0) {
        cat("The are not new peptides:\n")    
        cat("Decreased or increased:\n")
        kable(df_final3)  
    } else
        cat("The number of peptides are the same\n")
```

使用我的数据框,满足条件的条件是选项2,所以我希望有这个:

cat("The new peptides are:")
kable(df_peptides_spectra2)
cat("The rest peptides are the same.")

虽然,我的输出是这样的,没有任何表格。 :

The new peptides are:
The rest of peptides are the same.

你知道有什么方法可以解决这个问题吗?我在每次打印电话(R: why kable doesn't print inside a for loop?)之后尝试添加换行符,但我遇到了同样的问题。

这里有一个可重现的例子:

```{r}



    df <- structure(list(ID = structure(c(1L, 2L, 3L, 4L, 5L), 
    .Label = c("1", "2", "3", "4" ,"5") ), 
    sequence = structure(c(1L,2L, 3L, 4L, 5L), 
    .Label = c(" actgat   "," atagattg ", " atatagag ", " atggggg  ", " atgtagtt "), class = "factor"), 
    peptides = structure(c(1L, 2L, 3L, 4L, 5L), 
    .Label = c(" 54  ", " 84  ",  " 32  ", " 36  ", "12"),
    class = "factor"), n_project = structure(c(1L, 1L, 1L, 1L, 1L), 
    .Label = " project ", class = "factor")), .Names = c("ID", "sequence", "peptides", "n_project"), class = "data.frame", row.names = c(NA,  -5L))



    if (0!=0 && (0)!=0) {
        cat("The new peptides are:\n")
        kable(df)
        cat("Decreased or increased:\n")
        kable(df)
    } else if (10==10 && 2!=0) {
        cat("The new peptides are:")
        kable(df)
        cat("The rest peptides are the same.")
    } else if (10!=10 && 2!=0) {
        cat("The are not new peptides:\n")    
        cat("Decreased or increased:\n")
        kable(df)  
    } else
        cat("The number of peptides are the same\n")


```

1 个答案:

答案 0 :(得分:2)

在可重现的示例中,使用print(kable(df))来显示表格和{r, results = "asis"}

```{r, results='asis'}

    cat("The new peptides are:\n")
    print(kable(df))
    cat("\n")
    cat("The rest peptides are the same.")

---

enter image description here