ระงับตาราง kable ที่มีความยาวในเอาต์พุตอินไลน์ Rmd ในขณะที่เก็บไว้ใน PDF สุดท้าย

ฉันกำลังพยายามให้ R ระงับตารางที่มีความยาวที่สร้างด้วย kable & kableExtra ในเอาต์พุต Rmd แบบอินไลน์ในขณะที่เก็บไว้ใน PDF ที่ถักขั้นสุดท้าย ฉันต้องการทำสิ่งนี้เพียงไม่กี่ชิ้นเท่านั้น ดังนั้นฉันไม่ควรไปทางการตั้งค่าตัวเลือกส่วนกลางในการปิดเอาต์พุตอินไลน์ทั้งหมด

ฉันได้ทำซ้ำตัวเลือกอันต่างๆ ที่แสดงอยู่ที่นี่หลายครั้ง: https://yihui.name/knitr/demo/output/ และที่นี่: https://yihui.name/knitr/options/#plots แต่ยังไม่ได้ลงถูกจุด ดังนั้นฉันไม่แน่ใจว่าฉันดูถูกที่แล้วหรือว่าฉันเพิ่งข้ามการตั้งค่าที่ถูกต้องไป

YAML:

---
output:
  pdf_document:
    latex_engine: xelatex
---

รหัส:

```{r}
# Setup
library(knitr)
library(kableExtra)

# Create some data
dat <- data.frame ("A" = c(1:5),
                   "B" = c("Imagine a really long table",
                           "With at least 50 rows or so",
                           "Which get in the way in the inline output",
                           "But I want in the final PDF",
                           "Without influencing the other chunks")
                   )
# Produce the table
kable(dat, booktabs=TRUE, format="latex", longtable=TRUE) %>%
  kable_styling(latex_options="HOLD_position")
```

เอาต์พุตอินไลน์ที่ฉันไม่ต้องการให้ปรากฏขึ้นทุกครั้งที่เรียกใช้สิ่งนี้:

\begin{table}[H]
\centering
\begin{tabular}{rl}
\toprule
A & B\\
\midrule
1 & Imagine a really long table\\
2 & With at least 50 rows or so\\
3 & Which get in the way in the inline output\\
4 & But I want in the final PDF\\
5 & Without influencing the other chunks\\
\bottomrule
\end{tabular}
\end{table}

หากคุณลองนึกภาพว่าต้องเลื่อนดูเนื้อหานี้ประมาณ 50-100 บรรทัดในขณะที่พยายามเขียนโค้ด คุณจะเห็นว่ามันน่ารำคาญและใช้เวลานานขนาดไหน


person bcarothers    schedule 16.07.2018    source แหล่งที่มา


คำตอบ (1)


ฟังก์ชั่นนี้ตรวจพบว่าเอกสาร RMarkdown กำลังถูกประมวลผลแบบอินไลน์ใน RStudio แทนที่จะผ่านการถัก:

is_inline <- function() {
  is.null(knitr::opts_knit$get('rmarkdown.pandoc.to'))  
}

ดังนั้นคุณจึงสามารถห่อโค้ดที่มีปัญหาในลักษณะนี้

if (!is_inline()) {
  kable(dat, booktabs=TRUE, format="latex", longtable=TRUE) %>%
  kable_styling(latex_options="HOLD_position")
}

หรือสร้างฟังก์ชันอื่น

hide_inline <- function(x) {
  if (is_inline())
    cat("[output hidden]")
  else
    x
}

และเพิ่มลงในไปป์ของคุณ:

kable(dat, booktabs=TRUE, format="latex", longtable=TRUE) %>%
  kable_styling(latex_options="HOLD_position") %>%
  hide_inline()

ทั้งสองอย่างนี้มีข้อเสียคือต้องมีการแก้ไขโค้ดของคุณซึ่งจะแสดงหาก echo=TRUE ฉันไม่คิดว่าจะมีตัวเลือกอันใดที่เทียบเท่ากับ hide_inline แต่ฉันอาจผิด

หากคุณหมดหวังจริงๆ คุณสามารถใช้ echo=2:3 หรือคล้ายกันเพื่อซ่อนบรรทัด if (!is_inline()) { และ }

person user2554330    schedule 18.07.2018