gpt4 book ai didi

r - 捕获 R 输出并替换为 LaTeX 代码

转载 作者:行者123 更新时间:2023-12-04 10:28:20 25 4
gpt4 key购买 nike

我试图从一些 R 代码中捕获输出并将其替换为 latex 代码。

如果您运行此代码:

library(stargazer)
x <- capture.output(stargazer(mtcars[1:5, 1:3], summary = FALSE, title="The main caption of the table."))

x

这是输出:
 [1] ""                                                                                                         
[2] "% Table created by stargazer v.5.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu"
[3] "% Date and time: Sat, Jun 27, 2015 - 11:36:07"
[4] "\\begin{table}[!htbp] \\centering "
[5] " \\caption{The main caption of the table.} "
[6] " \\label{} "
[7] "\\begin{tabular}{@{\\extracolsep{5pt}} cccc} "
[8] "\\\\[-1.8ex]\\hline "
[9] "\\hline \\\\[-1.8ex] "
[10] " & mpg & cyl & disp \\\\ "
[11] "\\hline \\\\[-1.8ex] "
[12] "Mazda RX4 & $21$ & $6$ & $160$ \\\\ "
[13] "Mazda RX4 Wag & $21$ & $6$ & $160$ \\\\ "
[14] "Datsun 710 & $22.800$ & $4$ & $108$ \\\\ "
[15] "Hornet 4 Drive & $21.400$ & $6$ & $258$ \\\\ "
[16] "Hornet Sportabout & $18.700$ & $8$ & $360$ \\\\ "
[17] "\\hline \\\\[-1.8ex] "
[18] "\\end{tabular} "
[19] "\\end{table} "

我需要用这个替换第 5 行:
"  \\caption[short caption]{The main caption of the table.} "

我怎样才能做到这一点?

最佳答案

尝试:

x <- sub("\\caption{The main caption of the table.}", 
"\\caption[short caption]{The main caption of the table.}", fixed = TRUE, x)

关于r - 捕获 R 输出并替换为 LaTeX 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31087731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com