vincentarelbundock / modelsummary

Beautiful and customizable model summaries in R.
http://modelsummary.com
Other
912 stars 75 forks source link

Datasummary to latex output without floating table environment #202

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hi! Thanks for all the great work, I love the package. Is it possible to output directly from datasummary or modelsummary to LaTex in a way that does not include the floating table environment, i.e. \begin{table} and \end{table}, but only produces the tabular environment? Something like the floating = FALSE option in Stargazer?

Thanks in advance!

vincentarelbundock commented 3 years ago

I don't think it's possible at the moment because for the vast majority of use cases we need the table environment, so I add it by default. And I don't think that kableExtra has a function to remove an environement when it was added (I may be wrong).

The gt LaTeX writer is not super developed, so I doubt there is something there. huxtable seems to have a tabular_only argument in the to_latex function, but I don't have experience with it.

Of course, it would be trivial for you to apply a regex to remove it, and even to define a short function to automate the work (see below).

I'm curious, what's your use-case?

library('modelsummary')
library('magrittr')

mod = lm(hp ~ mpg, mtcars)

clean_table = function(x) {
  regex = "\\\\begin\\{table\\}|\\\\end\\{table\\}|\\\\centering"
  gsub(regex, "", x)
}

modelsummary(mod, output = "latex") %>%
  clean_table

\begin{tabular}[t]{lc}
\toprule
  & Model 1\\
\midrule
(Intercept) & 324.082\\
 & (27.433)\\
mpg & -8.830\\
 & (1.310)\\
\midrule
Num.Obs. & 32\\
R2 & 0.602\\
R2 Adj. & 0.589\\
AIC & 336.9\\
BIC & 341.3\\
Log.Lik. & -165.428\\
F & 45.460\\
\bottomrule
\end{tabular}
ghost commented 3 years ago

Thanks for the super quick response, Vincent!

Your solution should work out alright, I was just hoping to avoid a workaround like that. My use-case is a relatively common one I think: I want to use datasummary and modelsummary with the fixest package to output summary statistics and regression results straight to LaTex for a paper draft. I often combine multiple panels in a table in Latex using for example the subtable environment, and add the table caption in Latex instead of the R output. Subtable or even something like resizebox or adjustbox in LaTex (to adjust the scaling of a panel) requires a tabular environment and doesn't work with table environments.

Thanks again!

Christoph

On Fri, Jan 22, 2021 at 3:24 PM Vincent Arel-Bundock < notifications@github.com> wrote:

I don't think it's possible at the moment because for the vast majority of use cases we need the table environment, so I add it by default. And I don't think that kableExtra has a function to remove an environement when it was added (I may be wrong).

The gt LaTeX writer is not super developed, so I doubt there is something there. huxtable seems to have a tabular_only argument in the to_latex function, but I don't have experience with it.

Of course, it would be trivial for you to apply a regex to remove it, and even to define a short function to automate the work (see below).

I'm curious, what's your use-case?

library('modelsummary') library('magrittr') mod = lm(hp ~ mpg, mtcars) clean_table = function(x) { regex = "\\begin\{table\}|\\end\{table\}|\\centering" gsub(regex, "", x) }

modelsummary(mod, output = "latex") %>% clean_table

\begin{tabular}[t]{lc} \toprule & Model 1\ \midrule (Intercept) & 324.082\ & (27.433)\mpg & -8.830\ & (1.310)\ \midruleNum.Obs. & 32\R2 & 0.602\R2 Adj. & 0.589\AIC & 336.9\BIC & 341.3\Log.Lik. & -165.428\F & 45.460\ \bottomrule \end{tabular}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/modelsummary/issues/202#issuecomment-765718032, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASSBDH2LJOXCVXWWXPE4GX3S3H3KBANCNFSM4WPCWIQA .

vincentarelbundock commented 3 years ago

That seems like a totally reasonable use-case. I am averse to adding new arguments because one of the reasons I developed modelsummary is that I could never find my way around stargarzer's 85(!!!) arguments.

But here's an idea: What if i added an output type so you could call:

modelsummary(mod, output = "latex_tabular")

Would that work?

If so, what would you expect for title and notes? Would those just be unsupported for tabular output?

vincentarelbundock commented 3 years ago

Now that I think about it, I think we should offer two new output formats:

kableExtra_latex: would feed the table directly to kableExtra::kbl, and would pass all unrecognized arguments (via ...) to the kbl function. This means that you would be able to pass any of the options here directly to modelsummary():

https://rdrr.io/cran/kableExtra/man/kbl.html

You could also apply kable_styling after to customize in the ways described here: https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf

The default LaTeX table produced by this function would only have a tabular, since users need to call kable_styling or use the table.envir argument to specify the kind of float they want.

We would then also have a kableExtra_html that would a vanilla html table, and allow customization in the same ways as above.

The benefits of all this is that (a) it's super powerful since it gives access to litterally all of kableExtra arguments, (b) it is super easy to implement on my end, and (c) it requires adding no argument to modelsummary itself, since the ellipses do all the work and the arguments are documented by kableExtra

ghost commented 3 years ago

That would be a great solution in my eyes. My impression is that I'm not the only one using the "floating = FALSE" option in Stargazer and I've heard from a few people using workarounds with regular expressions to grab tabular outputs from modelsummary. It's probably the last thing holding me back from switching fully over to modelsummary for my outputs. Either the new output = "latex_tabular"option or passing it to kableExtra would work as far as I can tell.

Thanks again!

On Sat, Jan 23, 2021 at 4:10 PM Vincent Arel-Bundock < notifications@github.com> wrote:

Now that I think about it, I think we should offer two new output formats:

kableExtra_latex: would feed the table directly to kableExtra::kbl, and would pass all unrecognized arguments (via ...) to the kbl function. This means that you would be able to pass any of the options here directly to modelsummary():

https://rdrr.io/cran/kableExtra/man/kbl.html

You could also apply kable_styling after to customize in the ways described here: https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf

The default LaTeX table produced by this function would only have a tabular, since users need to call kable_styling or use the table.envir argument to specify the kind of float they want.

We would then also have a kableExtra_html that would a vanilla html table, and allow customization in the same ways as above.

The benefits of all this is that (a) it's super powerful since it gives access to litterally all of kableExtra arguments, and (b) it is super easy to implement on my end.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/modelsummary/issues/202#issuecomment-766196741, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASSBDH7CQEEOBONCMARPDS3S3NJPHANCNFSM4WPCWIQA .

vincentarelbundock commented 3 years ago

A new latex_tabular output format is now available. You can access it by installing the latest version from Github using the remotes package. (Make sure you restart R completely to ensure that the change takes effect.) In principle, this should allow you to do something like this:

library('modelsummary')
mod = lm(hp ~ mpg, mtcars)
modelsummary(mod, "latex_tabular")
#> 
#> \begin{tabular}[t]{lc}
#> \toprule
#>   & Model 1\\
#> \midrule
#> (Intercept) & 324.082\\
#>  & (27.433)\\
#> mpg & -8.830\\
#>  & (1.310)\\
#> \midrule
#> Num.Obs. & 32\\
#> R2 & 0.602\\
#> R2 Adj. & 0.589\\
#> AIC & 336.9\\
#> BIC & 341.3\\
#> Log.Lik. & -165.428\\
#> F & 45.460\\
#> \bottomrule
#> \end{tabular}

Please let me know when you've had a chance to try it so I can close this issue.

This will be part of the next CRAN release, but it might take a little while since I just released a new version.

Thanks again for the suggestion!

ghost commented 3 years ago

Thanks Vincent! Looks very promising. I'll give it a spin tomorrow.

Best, Christoph

On Sat, Jan 30, 2021, 12:36 PM Vincent Arel-Bundock < notifications@github.com> wrote:

A new latex_tabular output format is now available. You can access it by installing the latest version from Github using the remotes package. (Make sure you restart R completely to ensure that the change takes effect.) In principle, this should allow you to do something like this:

library('modelsummary')mod = lm(hp ~ mpg, mtcars) cat(modelsummary(mod, "latex_tabular"))#> #> \begin{tabular}[t]{lc}#> \toprule#> & Model 1\#> \midrule#> (Intercept) & 324.082\#> & (27.433)\#> mpg & -8.830\#> & (1.310)\#> \midrule#> Num.Obs. & 32\#> R2 & 0.602\#> R2 Adj. & 0.589\#> AIC & 336.9\#> BIC & 341.3\#> Log.Lik. & -165.428\#> F & 45.460\#> \bottomrule#> \end{tabular}

Please let me know when you've had a chance to try it so I can close this issue.

This will be part of the next CRAN release, but it might take a little while since I just released a new version.

Thanks again for the suggestion!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/modelsummary/issues/202#issuecomment-770269187, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASSBDH6BOIYAUJHOSFWRMB3S4RNVRANCNFSM4WPCWIQA .

ghost commented 3 years ago

Hi Vincent, at the risk of looking like an idiot (and this issue is most likely on my side), I'm having some trouble installing the new version using remotes::install_github('vincentarelbundock/modelsummary'). I get the following error message (I don't have much experience with 'remotes'):

Downloading GitHub repo vincentarelbundock/modelsummary@master

Error: Failed to install 'modelsummary' from GitHub: HTTP error 404. No commit found for the ref master Did you spell the repo owner (easystats) and repo name (parameters) correctly?

  • If spelling is correct, check that you have the required permissions to access the repo.

Best, Christoph

On Sat, Jan 30, 2021 at 6:47 PM Christoph Schiller ch.m.schiller@gmail.com wrote:

Thanks Vincent! Looks very promising. I'll give it a spin tomorrow.

Best, Christoph

On Sat, Jan 30, 2021, 12:36 PM Vincent Arel-Bundock < notifications@github.com> wrote:

A new latex_tabular output format is now available. You can access it by installing the latest version from Github using the remotes package. (Make sure you restart R completely to ensure that the change takes effect.) In principle, this should allow you to do something like this:

library('modelsummary')mod = lm(hp ~ mpg, mtcars) cat(modelsummary(mod, "latex_tabular"))#> #> \begin{tabular}[t]{lc}#> \toprule#> & Model 1\#> \midrule#> (Intercept) & 324.082\#> & (27.433)\#> mpg & -8.830\#> & (1.310)\#> \midrule#> Num.Obs. & 32\#> R2 & 0.602\#> R2 Adj. & 0.589\#> AIC & 336.9\#> BIC & 341.3\#> Log.Lik. & -165.428\#> F & 45.460\#> \bottomrule#> \end{tabular}

Please let me know when you've had a chance to try it so I can close this issue.

This will be part of the next CRAN release, but it might take a little while since I just released a new version.

Thanks again for the suggestion!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/modelsummary/issues/202#issuecomment-770269187, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASSBDH6BOIYAUJHOSFWRMB3S4RNVRANCNFSM4WPCWIQA .

vincentarelbundock commented 3 years ago

Can you try to update your remotes package. I know that older versions had issues with repositories that use main instead of master.

ghost commented 3 years ago

That seems to be doing the trick, thanks Vincent.

On Mon, Feb 1, 2021 at 10:42 AM Vincent Arel-Bundock < notifications@github.com> wrote:

Can you try to update your remotes package. I know that older versions had issues with repositories that use main instead of master.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/modelsummary/issues/202#issuecomment-771031347, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASSBDH57S4NJ32CIEE26POLS43RXPANCNFSM4WPCWIQA .

ghost commented 3 years ago

Hi Vincent, just tried out the new latex_tabular option. It works great to get a tabular output in the console. If I want to use modelsummary() to output directly to a .tex file (instead of the console) I still need to get the console output and print it out in a second step, right?

Christoph

On Mon, Feb 1, 2021 at 12:24 PM Christoph Schiller ch.m.schiller@gmail.com wrote:

That seems to be doing the trick, thanks Vincent.

On Mon, Feb 1, 2021 at 10:42 AM Vincent Arel-Bundock < notifications@github.com> wrote:

Can you try to update your remotes package. I know that older versions had issues with repositories that use main instead of master.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/modelsummary/issues/202#issuecomment-771031347, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASSBDH57S4NJ32CIEE26POLS43RXPANCNFSM4WPCWIQA .

vincentarelbundock commented 3 years ago

Yes, unfortunately, that's right. The issue is that output guesses how it should write to file using the file extension. But obviously, tabular and table environments are both saved with .tex, so it can't tell them apart. There are many ways to save the table, but something like this should work:

library('modelsummary')
library('magrittr')

mod = lm(hp ~ mpg, mtcars)
modelsummary(mod, "latex_tabular") %>%
  cat(file = "table.tex")
ghost commented 3 years ago

Thanks Vincent, that absolutely makes sense and works. Thanks for your work on this, it definitely helped me understand the package better and I'm much more likely to use it on a daily basis. You can close the issue if you want?

Chris

On Mon, Feb 1, 2021 at 3:08 PM Vincent Arel-Bundock < notifications@github.com> wrote:

Yes, unfortunately, that's right. The issue is that output guesses how it should write to file using the file extension. But obviously, tabular and table environments are both saved with .tex, so it can't tell them apart. There are many ways to save the table, but something like this should work:

library('modelsummary') library('magrittr') mod = lm(hp ~ mpg, mtcars) modelsummary(mod, "latex_tabular") %>% cat(file = "table.tex")

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/modelsummary/issues/202#issuecomment-771192895, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASSBDH42UOSV6SZJUMCTGZDS44Q6VANCNFSM4WPCWIQA .

adamaltmejd commented 3 years ago

Just found my way here because I have almost exactly the same use case: I like to use \threeparttable and keep that caption, notes, etc in the latex document and just including the tabular-only table with \input{}. latex_tabular seems to work great. Am I understanding correctly that it just pipes to kbl(format = "latex")?

vincentarelbundock commented 3 years ago

Cool. Yes, that's pretty much it.

ravivmg commented 5 months ago

Yes, unfortunately, that's right. The issue is that output guesses how it should write to file using the file extension. But obviously, tabular and table environments are both saved with .tex, so it can't tell them apart. There are many ways to save the table, but something like this should work:

library('modelsummary')
library('magrittr')

mod = lm(hp ~ mpg, mtcars)
modelsummary(mod, "latex_tabular") %>%
  cat(file = "table.tex")

When I do that then I get the following error:

Error in cat(., file = "table.tex") : 
  argument 1 (type 'S4') cannot be handled by 'cat'

Did something change about what gets piped out?

vincentarelbundock commented 5 months ago

@ravivmg Yes, this is the consequence of the move from kableExtra to tinytable as default table-drawing package in version 2.0.0 of modelsummary. This is documented in NEWS, and instructions to revert to kableExtra (which I do NOT recommend) are printed in the console every time a user calls library(modelsummary).

Staying with tinytable, you can achieve the same result with something like:

library(tinytable)
library(modelsummary)
mod <- lm(mpg ~ hp, mtcars)
modelsummary(mod, output = "latex_tabular") |> 
    tinytable::save_tt("table.tex")

Note that this will use tabularray's tblr environment rather than a tabular.