How to append notes to a latex table in Hmisc in R? - r

An unelegant way is just to add lines of tex code with the note text.
But is there a more native way of making notes in latex function of Hmisc package in R?

There is a way not in Hmisc but using starpolishr package,
install.packages("devtools")
library(devtools)
install_github("ChandlerLutz/starpolishr");library(starpolishr)
Insert footnotes with caption package
la<-latex(mtcars[1:2,1:5],file="example.tex",caption = "table using caption",
where="!htbp")
file0<-readLines("example.tex")
file0<-star_notes_tex(file0, note = "file using caption*")
cat(file0, file = paste0(getwd(),"/file0.tex"),sep="\n")
Insert footnotes with threeparttable
Attention, you must use the caption and label argument in Hmisc::latex
mt <- mtcars[1:2,1:5]
mt[1,1]<-paste0(mt[1,1],"\\tnote{*}")
la<-latex(mt,file="example1.tex",label=" ",caption="table using threeparttable",
where="!htbp")
file1<-readLines("example1.tex")
file1<-star_notes_tex(file1, note.type = "threeparttable",
note = "* notes with threeparttable : For this example you must use both label and caption, the
threepartable code is meant to be used with tables generated by
stargazer where a row with label is present. Otherwise the threepart table
argument is badly positionned.")
cat(file1, file = paste0(getwd(),"/file1.tex"),sep="\n")

Related

Stargazer Table in Latex Format - R

I Imagine this pretty simple, but I am very new to R and stargazer so I am not sure how to do this.
I have a code block (seen below):
ses <- diag(vcovHC(model))
stargazer(model,
type='latex',
header = F,
se = ses,
title="")
I want to convert this output into a Latex Table. I've tried playing with markdown but I can't seem to get anything to work.
Can anyone advise on how to do this? I've tried using latex editors but I can't source the data into that.
Thank you!
Output is text file with latex code. You can use it in latex editor with appropriate modifications.
Hope I have answered your query correctly.
sink("output.txt")
stargazer(mod1,se = robustse_mod_1,type = "latex")
sink()

How do I remove this Rmarkdown output after the read_excel function?

How do I go about removing the output shown in the picture below after I use read_excel to import the data in r markdown. Basically I don't want there to be any output after this function. Please see attached image.
bdims <- read_excel("bdims.xlsx")
head(bdims)
You can set results = 'hide'. So if eg. for standard rmarkdown. This will stop the output from showing. Read on the cheat sheet how to use other settings such as echo and eval
{results = 'hide'}
bdims <- read_excel("bdims.xlsx")
or with roxygen document for rendering rmarkdown,
#+ results = 'hide'
bdims <- read_excel("bdims.xlsx")

Is it possible to include a footnote without symbol in KableExtra?

When I use add_footnote("Footnote 1"), KableExtra always introduce a symbol in the footnote. But in many occasions, I would like to include a footnote without a symbol. For example, I would like to use add_footnote("Source: Consumer Expenditure Survey") to include information about the source of the data. But I do not want to have the symbol in front of "Source..." in my table. Is that possible?
Now you can. :)
With current dev ver (should be on CRAN in a week or so), there is a new function called footnote. To get some non-labeled footnotes, just type
mtcars[1:5, 1:5]%>%
kable("latex", booktabs = T)%>%
footnote(general = c("Note 1", "Note 2"), general_title = "")
Similar for html tables.
I don't think so if you want to use kableExtra. If you're producing your document in LaTeX, see https://tex.stackexchange.com/questions/30720/footnote-without-a-marker for a way to do it in pure LaTeX.

xtable italics format for a column

I need to insert the species names in a table created by xtable in my Rnw file and I want to convert the relative column to italics format. Is it possible without any manual intervention?
My call is:
xtable(cklist, caption="Checklist...", align='lllc',label = 'tab:ckzygo')
To typeset a column in italics (or any other non-standard font shape), you should use the >{\cmd} syntax for column specification.
Assigning the column type >{\itshape}l generates a left-justified column in italics.
This is a better solution than iris$Species <- paste0("\\textit{", iris$Species, "}") as suggested in the comments because you neither have to modify your data nor you need to disable text sanitizing.
Small illustration:
\documentclass{article}
\usepackage{array}
\begin{document}
<<xtableItalics, results = "asis">>=
library(xtable)
print(xtable(head(iris), align = c(rep("l", 5), ">{\\itshape}l")))
#
\end{document}
The PDF looks like:
Please note that you need to use the array package for this to work.
EDIT: To show the flexibility of this approach, two more examples:
print(xtable(head(iris), align = c(rep("l", 5), ">{\\textit\\bgroup}l<{\\egroup}")))
print(xtable(head(iris), align = c(rep("l", 5), ">{\\textcolor{red}\\bgroup}l<{\\egroup}")))
The first line uses \textit{} instead of \itshape to typeset the italics. As \textit{} requires the text to modify as an argument, we need a slightly more complex syntax. (It's described in the wikibooks.org article linked above.)
This syntax can also be used to change for example the color of the text. In more complex cases, lrbox is required, as described in the linked article.

header on each page of big table of xtable?

How do you put on a big table of xtable the table header on each page?, So that is easier to read the table the table between pages.
I use the following in Sweave:
test.big<- xtable(test,label="table",caption='test')
align(test.big) <- "|c|c|c|c|l|c|c|c|"
print(test.big,tabular.environment='longtable',include.colnames = TRUE,floating=FALSE)
Thanks for your answers
I think a better answer for this question is provided here: Column names on each page with xtable in Sweave
What if you want to edit your table in R? The solution above edits the output, so you won't need to add those lines in your longtable code manually. Thus this works better:
print(test.big, tabular.environment='longtable', include.colnames = TRUE,
floating=FALSE, add.to.row = list(pos = list(0), command = "\\hline \\endhead "))
Note that you can add several arguments in your add.to.row list:
print(test.big, tabular.environment='longtable', include.colnames = TRUE,
floating=FALSE, list(pos = list(seq(1,nrow(get(groups[i])), by = 2), 0),
command = c("\\rowcolor[gray]{.95} ","\\hline \\endhead ")))
You'll have to add this to your Sweave file:
\usepackage{colortbl}
This produces gray filling on every second row & header for each page.
The longtable (LaTeX) package specification can be found at that URL. The section of code in the examples whose output appears on pages 2 and 3 is in section 8 and I have reproduced bit of it below:
\caption[]{(continued)}\\
\hline\hline
\multicolumn{2}{#{*}c#{*}}%
{This part appears at the top of every other page}\\
\textbf{First}&\textbf{Second}\\
\hline\hline
\endhead
When they say on "every other page", they mean every page other than the first, which had a different header. If the xtable call is not working out of the box without any editing, then you should first check that you have the longtable package specified in your LaTeX preamble:
\usepackage{longtable}

Resources