如何保存和编辑有线打印的内容? [英] How to save and edit the content of a kable print?
问题描述
这是对考虑这个工作示例
```{r table, results='asis'}
library(knitr)
library(kableExtra)
library(magrittr)
dataframe <- data.frame(mytext1 = c('HELLO',
'WORLD'),
mytext2 = c('HELLO',
'AGAIN'),
value1 = c(1,2),
value2 = c(1,2))
piper <- dataframe %>%
kable(format = 'latex', booktabs = TRUE) %>%
add_header_above(header = c("Text" = 2, "Values" = 2))
```
给出
\begin{tabular}{llrr}
\toprule
\multicolumn{2}{c}{Text} & \multicolumn{2}{c}{Values} \\
\cmidrule(l{2pt}r{2pt}){1-2} \cmidrule(l{2pt}r{2pt}){3-4}
mytext1 & mytext2 & value1 & value2\\
\midrule
HELLO & HELLO & 1 & 1\\
WORLD & AGAIN & 2 & 2\\
\bottomrule
\end{tabular}
在这里,我想将此输出写入tex
文件,并手动删除它的第一行和最后一行.
Here I would like to write this output to a tex
file, and remove manually the first and last line of it.
不幸的是,天真
piper %>% filter(row_number() >=2 & row_number() <=(length(piper) - 1))
Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "knitr_kable"
在这里不起作用.有任何想法吗? 谢谢!
does not work here. Any ideas? Thanks!
推荐答案
打印piper
时,实际上是在调用进行很多更改的打印方法. piper
不仅仅是那十行文字.
When you print piper
, you're actually calling a print method which makes a lot of changes. piper
isn't just those 10 lines of text.
如果要获得这些行,可以调用capture.output(piper)
,并且应该能够将其过滤为字符向量.我认为row_number()
函数不适用于这些功能,但是常规索引应该可以.例如,
If you want to get those lines, you can call capture.output(piper)
, and you should be able to filter that as a character vector. I don't think the row_number()
function works on those, but regular indexing should. For example,
lines <- piper %>% capture.output
lines[c(-1, -length(lines))]
编辑后添加:要打印不带行号的内容,请使用cat()
.例如,
Edited to add: To print that without line numbers, use cat()
. For example,
lines[c(-1, -length(lines))] %>% cat(sep = "\n")
这篇关于如何保存和编辑有线打印的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!