簡體   English   中英

在 RStudio 中,如何像在控制台中一樣在 quarto/rmarkdown 塊中打印 tibble?

[英]In RStudio, how to print tibble in quarto/rmarkdown chunk as in console?

在編輯四開本/rmarkdown 文檔時,我希望 RStudio 以與在控制台中相同的方式顯示內聯小標題,而不是默認分頁打印。

而不是這個:

在 RStudio IDE 中默認打印 tibbles

我更喜歡控制台中的 output:

# A tibble: 150 × 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# … with 140 more rows
# ℹ Use `print(n = ...)` to see more rows

我試過設置#| results: asis #| results: asis並更改 yaml 中的df-print選項,但這只會影響呈現的文檔。 我可以改為在控制台中使 RStudio 顯示塊 output,但我更願意將其保持內聯。

您可以在您的塊中使用選項paged.print=FALSE ,這將像這樣打開分頁表:

---
format: html
---

```{r paged.print=FALSE}
library(tibble)
as_tibble(iris)
```

以塊的形式輸出:

# A tibble: 150 × 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# … with 140 more rows
# ℹ Use `print(n = ...)` to see more rows

查看此文檔以獲取更多信息和塊中的選項。

我喜歡上面的答案,因為您可以更好地控制各個塊。 只是在這里添加 2 美分:如果你想要一個通用的解決方案,你可以像這樣將它添加到頭部部分:

---
format: html
editor_options:
  chunk_output_type: console
---

Stackoverflow 中的此答案所示。

這是對@Quinten 回答的補充。 我做了一些挖掘,發現 RStudio 默認覆蓋了一些打印方法,選項paged.print改變了。 它們是: print.data.frameprint.tbl_dfprint.paged_dfprint.grouped_dfprint.rowwise_dfprint.tbl_sqlprint.data.tableprint.tbl_lazy源代碼)。 您可以通過多種方式將選項傳遞給 RStudio:作為塊選項(如@Quinten 的回答),作為 knitr 塊選項( knitr::opts_chunk$set(paged.print = FALSE) )和作為R選項。 因此,您可以將其應用於整個文檔:

```{r setup, include=FALSE}
options(paged.print = FALSE)
```

在此處輸入圖像描述

如果你想讓這個在會話之間永久存在,你可以用usethis::edit_r_profile()把它放在你的.Rprofile 中。 如果您想自定義默認打印,還有pillar_options ,它控制 tibble 打印(請參閱?pillar::pillar_options )。 例如:

```{r setup, include=FALSE}
options(paged.print = FALSE,
        pillar.print_max = 25, 
        pillar.print_min = 25)
```

在此處輸入圖像描述

或者你用你喜歡的任何東西再次覆蓋打印方法;)

```{r setup, include=FALSE}
print.tbl_df <- function(x, ...) print(knitr::include_graphics("https://jeroen.github.io/images/frink.png"))
```

同樣,您可以將所有這些都放在您的.Rprofile 中。 但是,這也會影響渲染的 output,這可能不是您想要的。 相反,您只能在交互式會話中運行它:

if (interactive()) {
  print.data.fram <- print.grouped_df <- print.tbl_df <- function(x, ...) print(knitr::include_graphics("https://jeroen.github.io/images/frink.png"))
}

謹慎使用它,因為它真的會讓那些秘密將它放入他們的.Rprofile 中的人感到困惑;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM