isubirana / compareGroups

R package to easily build publication-ready univariate or bivariate descriptive tables from a data set.
https://isubirana.github.io/compareGroups/
33 stars 14 forks source link

export2latex in rmarkdown r chunk #3

Closed raffdoc closed 5 years ago

raffdoc commented 5 years ago

I am tring to use export2 talex with rmarkdown file but id does not function. It puts out only a varbatim code but does not create a table itself. export2latex(tab1demo,caption = "The main table")

isubirana commented 5 years ago

The export2latex function creates LaTeX code to generate the table. However, it requires longtable, multirow and multicol LaTeX packages. In addition,expot2latex doesn't work properly on Rmd files (using knitr), but it must be used on RSweave (.Rnw) document. Below, there is a reproducible toy example Rnw code file. If you want to compile Rmardown (.Rmd) document, use the export2md function.

Hope it helps.

Isaac.

############# example.Rnw #############

\documentclass{article}

\usepackage{longtable} \usepackage{multirow} \usepackage{multicol}

\begin{document}

<>= library(compareGroups) tab <- descrTable(iris) export2latex(tab) @

\end{document}

#####################

raffdoc commented 5 years ago

Tanks for rapid response. There are some templates for scientific journals (rticles package from rstudio) that are written in rmarkdown put at the end converted to latex and pdf files. Pity, it does not function. There is a this wierd \start{verbatim} \end{verbatim} in the middle of the code. I think if you can eliminate this behavior you will fix this issue. Thanks

isubirana commented 5 years ago

If you want to generate a PDF document with Rmarkdown (.Rmd) document, you can use the export2md function. This function works for PDF, HTML and Word documents. Although it detects automatically in which format the file is compiled, you can set the argument format to "latex" if the file is compiled to PDF. I hope it works.

raffdoc commented 5 years ago

It does not do the trick. Thanks anyway.

raffdoc commented 5 years ago

Dear @isubirana
Here is a reproducable example.

`

title: Title here

to produce blinded version set to 1

blinded: 0

authors:

keywords:

abstract: | The text of your abstract. 200 or fewer words.

bibliography: bibliography.bib output: rticles::asa_article

Introduction

This template demonstrates some of the basic latex you'll need to know to create a ASA article.

\section{Verifications} \label{sec:verify} This section will be just long enough to illustrate what a full page of text looks like, for margins and spacing.

@Campbell02 @Schubert13 [@Chi81]

The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. With this spacing we have 30 lines per page.

library(compareGroups)
data("predimed")
restab <- createTable(compareGroups(group ~ age + sex + smoke + 
    waist + hormo, data = predimed))
export2latex(restab)

` You will find that wierd verbatim inside the latex table.

isubirana commented 5 years ago

I couldn't reproduce your example and couldn't figure out the solution. Could you send me the .Rmd file to my e-mail (isubiranaimim.es)? Thanks.

Isaac.

isubirana commented 5 years ago

Hi Rafik,

Bellow, you will find the syntax with two little modifications (tagged with “ISAAC”). The strange behavior was caused by export2latex, as you mentioned. In compareGroups there are two function to export tables to PDF documents in a Rmarkdown file: export2latex and export2md. The latest (export2md) you have to specify the argument format=”latex”. The export2md function works fine under “knitr” (“.Rmd”), while expor2latex works fine with “sweave” (.Rnw) files. Clearly, this should be message to be included in the help manuals.

Let us know if it works, and many thanks for your feedback.

Regards,

Isaac.

######## code ##########


title: Title here

to produce blinded version set to 1

blinded: 0

authors:

keywords:

abstract: | The text of your abstract. 200 or fewer words.

bibliography: bibliography.bib

output: rticles::asa_article header-includes:

Introduction

This template demonstrates some of the basic latex you'll need to know to create a ASA article.

\section{Verifications} \label{sec:verify} This section will be just long enough to illustrate what a full page of text looks like, for margins and spacing.

The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. With this spacing we have 30 lines per page.

library(compareGroups)
data("predimed")
restab <- createTable(compareGroups(group ~ age + sex + smoke + 
    waist + hormo, data = predimed))
export2md(restab, format="latex")   ## ISAAC> change the export2latex to export2md

Tables