25

This is a very basic question but any answers would really help.

My question is are there any package or install requirements for LaTeX in order to be able to export from R to LateX?

I am asking because while I have been able to export html and text files from R, trying to export as LateX doesn't output a file at all. I am simply trying to produce nice looking tables for publication.

E.g

xtable(newobject2, type = "latex", file = "filename2.tex")

Does nothing, whereas

xtable(newobject2, type = "html", file = "filename2.htm")

Gives me the desired file output.

Do I need to install MacTex in order for this to work? I am on a Mac

Full Code for reference is below:

library(foreign)
library(xtable)
library(stargazer)
chap <- read.csv("chap2.csv", header = TRUE)
new <- chap[c("party", "galtan", "sociallifestyle", "civlib_laworder", "religious_principle", "immigrate_policy", "multiculturalism", "urban_rural", "ethnic_minorities", "deregulation", "nationalism", "econ_interven", "redistribution", "spendvtax", "corrupt_salience", "antielite_salience", "international_security", "environment")]
new2 <- new[1:243, 2:18]
fa1 <- factanal(new2, factors = 4, rotation = "varimax", sort = TRUE)
print(fa1, digits = 3, cutoff = .5, sort = TRUE)

newobject2 <- as.data.frame(unclass(fa1$loadings))
xtable(newobject2, type = "latex", file = "filename2.tex")
5
  • Welcome to TeX.SX! According to documentation this should work. Please help us help you by posting some more information.
    – TeXnician
    Apr 11, 2017 at 12:04
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Apr 11, 2017 at 12:05
  • Thanks both. well I suppose I can say that I am trying to export factor analysis output in R. Stargazer and xtable packages do not seem to work directly on that type of object so I have tried to convert to a data frame (newobject2) before passing this to xtable (I also tried stargazer). If it helps I will put my full R code above.
    – Henry Cann
    Apr 11, 2017 at 12:08
  • On my system xtable(obj) produces LaTeX output by default, but there's no "file=" option. I have to do sink("~/file.tex") then xtable(obj) then sink() to get an output file. I have xtable 1.8-2
    – Thruston
    Apr 11, 2017 at 12:15
  • 1
    Your question about "Do I have to install MacTeX" makes me think you are not already using LaTeX on your machine. If that's the case, then Yes, you do have to install MacTeX at least to be able to use the resulting document. You don't need to have MacTeX installed to use the relevant R functionality I don't think, although it seems somewhat pointless to produce LaTeX tables from R if you can't also compile the resulting LaTeX.
    – Alan Munn
    Apr 11, 2017 at 17:14

1 Answer 1

27

Welcome! Look at ?print.xtable. This works

print(xtable(newobject2, type = "latex"), file = "filename2.tex")
6
  • 2
    This answers the question so it should be the accepted answer!
    – Paddre
    Feb 21, 2018 at 12:47
  • What if the table is a longtable to be broken over several pages? Do I need to do the work from my master latex document or from R?
    – skan
    Apr 8, 2018 at 20:19
  • 2
    @skan print(xtable(newobject2, type = "latex", tabular.environment="longtable"), file = "filename2.tex")and you will have to load the library longtable. Or ask a new question with mre
    – nebi
    Apr 9, 2018 at 7:59
  • @nebi is an mre a minimal r example?
    – thymaro
    Apr 30, 2018 at 9:24
  • @thymaro I was meaning minimal reproducible example (not r specific). If it wasn't clear, sorry!
    – nebi
    Apr 30, 2018 at 15:17

Not the answer you're looking for? Browse other questions tagged .