如何使用RMarkdown使用胶水包装折叠? [英] How can I make a collapse with glue package using RMarkdown?

查看:75
本文介绍了如何使用RMarkdown使用胶水包装折叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用胶和stargazer软件包自动化乳胶中某些df表的结果,但是我没有任何结果(我要显示的是"^ {*}"的含义,到表中的每个值),然后再使用RMarkdown.

I've been trying to automate the results of some df table in latex using the glue and stargazer packages, but I haven't had any results (what I want is for the meaning "^{*}" to appear next to each value as it is in the table) to use then RMarkdown.

我想要得到的东西:

我当前的丑陋且容易出错的修复程序:

My current ugly and error-prone fix:

library(dplyr)
library(glue)
library(stargazer)
X1 = c(4.70e1, 4.72e1, 4.76e1, 2.73e20)
X2 = c(4.67e1, 4.69e1, 4.77e1, 2.05e20)
tab.out = data.frame(X1, X2)
tab.out$max<-apply(tab.out, 1, max)

one = "1"
n.tab = tab.out %>%
  mutate(test1 = if_else(tab.out$X2 < tab.out$max,
                         glue("\\textsuperscript{*} is $<<one>>$.", .open = "<<", .close = ">>"),  #It doesn't work with ^{*}
                         glue("")))

注意:one只是为了测试崩溃,因为我同时尝试了glue_dataglue_collapse,但没有用.

Note: one was just to test the collapse because I tried glue_data as well as glue_collapse and it didn't work.

另一方面,假设折叠有效,那么我该如何正确调试乳胶代码?因为我尝试使用stargazerxtabletextreg,但是在每个功能中它都不能识别"\,},^ {*}" .

On the other hand, assuming the collapse works, how would I do to debug the latex code right? Because I tried with stargazer, xtable and textreg but in each of the functions it doesn't recognize "\, }, ^{*}".

n.tab = n.tab[c(1,2,4)]
stargazer(n.tab, summary = F, header = F)

我得到了什么?

推荐答案

我使用 https://stackoverflow.com/a/55565490/11570343>此处并根据@stefan的建议,但现在我想为n列自动化相同的功能

I achieved this using the paste0 function as mentioned here and on the recommendation of @stefan but now I would like to automate the same function for n-columns

library(dplyr)
col.nam = c("AIC(n)", "HQ(n)", "SC(n)", "FPE(n)")
tab.out = data.frame(col.nam, X1, X2)

n.tab = tab.out %>%
  mutate(test1 = if_else(tab.out$X1 < tab.out$X2,
                         paste0(X1,"$^{*}$"),
                         paste0(X1)),
         test2 = if_else(tab.out$X2 < tab.out$X1,
                         paste0(X2,"\\textsuperscript{*}"),
                         paste0(X2)))%>%
  select(col.nam, test1, test2)

colnames(n.tab) = c("Parámetros", "Lag 1", "Lag 2")
print(xtable::xtable(n.tab, 
                     header = F, 
                     caption = "asdasdasdasd",
                     label="table:tb1",
                     caption.placement = "top",
                     align="llcc"),
      hline.after = c(-1,0), 
      include.rownames=FALSE, 
      include.colnames = TRUE,
      add.to.row = list(pos = list(nrow(n.tab)),
                        command = paste("\\hline \n",
                            "\\multicolumn{3}{l}{\\footnotesize{$^{*}$Indica el orden de retraso seleccionado}} \\\\",
                            "\\multicolumn{3}{l}{\\footnotesize{\\textit{Elaboración: Los autores}}}",
                            sep = "")), comment=FALSE,
      sanitize.text.function = function(x){x})

这篇关于如何使用RMarkdown使用胶水包装折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆