2

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.

What I want to get:

enter image description here

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("")))

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

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)

What I got ?

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
cdcarrion
  • 574
  • 6
  • 22
  • Shouldn't it be `textsuperscript` instead of `extsuperscript`. Also your example is not reproducible. Please add necessary `library` calls and try to run the example in an empty environment before posting (for example, there is no definition for `tab.out4` in the example code you provided) – dario Mar 07 '20 at 08:24
  • As @dario pointed out. Should be `\\textsuperscript`. I can't solve your glue question but regarding your stragzaer, xtable problem: The latex code in your table is not interpreted as latex but instead printed as text. I' m not an expert in xtable, stargazer, .. but after figuring that out, I found a solution here https://stat.ethz.ch/pipermail/r-help/2008-September/174366.html. Try `print(xtable::xtable(n.tab, summary = F, header = F), sanitize.text.function = function(x){x})` – stefan Mar 07 '20 at 08:37
  • I did as you mentioned @stefan and it works but the board comes out a little different, how would you fix it? Thank you. – cdcarrion Mar 07 '20 at 13:22
  • @dario t's already corrected, sorry for the mistakes. – cdcarrion Mar 07 '20 at 13:23
  • Hi christian, what you mean by "the board comes out a little different". (; – stefan Mar 07 '20 at 13:35
  • @stefan because I can't modify the latex code to my liking, but it works! Thank you. Now I'd like to solve the problem of the glue collapses. – cdcarrion Mar 07 '20 at 18:44

1 Answers1

1

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})

enter image description here

cdcarrion
  • 574
  • 6
  • 22