Conditionally formatting tables using gridExtra in R - r

Recently I was exposed to the grid.table function in the package gridExtra in R. I am simply looking to make my dataframes look "better" than simply printing them to the console.
The default settings work pretty well, but now I am getting greedy. I am asking for help because I am not sure if the following two formatting settings are possible. I read the documentation but am still not sure if this can happen...
Conditionally format/highlight rows and columns if they meet a certain criteria. My thinking was that I could highlight a certain row to make it stand out from the rest of the table.
Is it possible to left alight the first column in the table and center align everything else?
In short, I really like how easy it is to format a dataframe as a table, but now I am hoping I can do a few extra formatting techniques to get the tables to where I need them to be.

Having just emailed Baptiste the author of gridExtra, that functionality is not currently available.
However, he says that once the gtable package is officially released he is planning to make an update to grid.table which should make it easier to customise the output. But also says not to "hold your breath"...He also however suggested to feel free to use the source code and adapt it.
But I too have been looking for ways to highlight rows in tables and have used the package dprint to do so. Hope that helps.

Related

How to randomize PDF page order using pdftools

I am trying to randomize the page order of a 382-page PDF. I've read that the pdftools package may be the way to go, but I'm not sure if it's able to randomize the PDF order. I was thinking of using pdf_subset to split the existing PDF into two and then using pdf_combine to stick them back together, but I realize that this would just bind them one after the other and not actually mix up the pages. I've also tried something similar in Automator on my Mac (didn't work) but I was curious if there was a way to do this in R.
Thanks in advance!

improving rGL HTML performance with multiple figures (mfrow3d) + rglWidgets

i am using RGL to produce a panel of multiple figures through the mfrow3d command.
for the most part, the html produced from the call to writeWebGL is exemplary.
the one caveat is that for multiple figures (be it 6 or 16), i have noticed a bit of lag when attempting to manipulate any one of these figures (to pan/zoom/look around).
an example can be found here: http://fluxions.dydx.ie:1338/schiz.html (warning, 100MB html file haha).
i wanted to ask people here if there is anything i can do in terms of using the "reuse" argument that may speed up performance.
additionally, i wanted to ask if there is any benefit to using rglWidgets and if there is a small example someone could provide in porting a writeWebGL call produced from the following:
https://johnmuschelli.com/WebGL_Interactive_Paper/supp_1/supp_1_wrap.Rmd
to rglwidgets (in hopes that the reuse argument in widgets may improve performance due to my use of mfrow3d).
i am not familiar on how to capture a multi-figure layout with multiple calls to contour3d as a scene that widgets can use.
dr duncan murdoch has gotten back to me and said there probably is not a way to do this, so i guess i will close it.
he is very helpful and i thank him for his support.

How to make a nice looking table in base r (not markdown)

I’ve been looking for an hour, but everything I can find about how to make a nice looking table out of a data frame mentions that it’s for rmarkdown, html, or latex.
Is it not possible to make a nice looking table in base r?
plot(x, y) makes a graph.
Is there no function like: printTable(df)?
Broadly speaking over what you can get from a normal print in base::print there is not much else you can do. You could try to twist plot function to plot values from selected cells in a data frame but that would be very onerous to develop and impractical in the light of currently available and maintained solutions. There is a number of packages that let you achieve what you need. For instance you can try formattable by renkun-ken.
Example
For a simple example you can try formattable::formattable(mtcars[1:10,])
Creating Images
For a solution creating images from tables, have a look at this discussion. As discussed, in the linked answer if you insist on generating a static image you can use grid.table function offered via gridExtra: tbl <- grid.table(mtcars[1:5,]).
You may be interested in the flextable package that is very easy to use with multiple options to create nice tables.
You can also have multiple word, pdf, or html output types.
I invite you to check the manual : https://cran.r-project.org/web/packages/flextable/vignettes/overview.html

How to make publishable tables and plots using R? [duplicate]

There are a range of tools available for creating publication quality tables using R, Sweave, and LaTeX.
In particular, there are helper functions like latex in the Hmisc package, and xtable in the xtable package. I've also often written my own code so that I could have complete control over table formatting (e.g., see this example).
However, when preparing publication quality tables a range of issues often arise:
how and when to apply numeric formatting
how to precisely control alignment of columns and cells
how to precisely control cell borders
how to convert variable labels to variable names
and so on
Beyond the high level issues of specifying the desired table format, there are issues of implementation.
When should a helper function such as xtable be used?
Which helper function should be used in a given situation?
How can the default output of helper functions be customised to particular requirements?
Question
It seems to me that the above issues are deserving of a detailed textbook-style introduction.
Are there any online or offline resources that provide a detailed overview of how to produce publication quality tables using R, Sweave, and LaTeX, and that address the issues discussed above?
Just to tie this up with a nice little bow at the time of current writing, the best existant tutorials on publication-quality tables and usage scenarios appear to be an amalgamation of these documents:
A Sweave example (source)
The Joy of Sweave: A Beginner's Guide to Reproducible Research with Sweave (source)
Latex and R via Sweave: An example document how to use Sweave (source)
Sweave = R · LaTeX2 (source)
The xtable gallery (source)
The Sweave Homepage
LaTeX documentation
Going beyond the scope of what currently exists, you may want to ask the author of The Joy of Sweave for a document on publication-quality tables specifically. It seems like he's gone above and beyond this problem in his research. In addition to the questions you've raised, this space specifically could use a style guide that, flatly, does not currently exist.
And, as mentioned in the question errata, this is a perfect example of a question for https://tex.stackexchange.com/. I encourage you to continue to ask specific questions there when you run into any difficulties in your current projects.
The package stargazer can create publication-quality - incl. using templates designed to resemble existing academic journals - from commonly used R statistical functions and packages (lm, glm, plm, svyglm, survival, pscl, AER, and others). Also good for creating summary statistics tables, and can directly output data frame content as well.
There is a tabular function in the tables package which addresses formatting, alignment and label operations. The package has a vignette which is a good starting point.
xtable has worked fine for me so far.
In combination with siunitx, and when necessary, longtable, it can produce pretty effective tables, in my opinion. With packages like booktabs and caption, the aesthetics can be pleasing too.
I am not sure this level of detail was asked for by the OP, but for what it's worth, the basic implementation could be something along these lines: https://tex.stackexchange.com/questions/41067/caption-for-longtable-in-sweave/41183#41183 (my own answer to another question).
I highly recommend ConTeXt which makes use of the TABLE package. There is a Table overview in contextgarden and an exhaustive manual.

List of ggplot2 theme options?

After some research I found the way to prevent an uninformative legend from displaying
... + theme(legend.position = "none")
Where can I find all of the available theme options and their default values for ggplot2?
The closest thing to a comprehensive list that I have been able to find is in the ggplot2 wiki on github. I think that most of the options are covered there with examples.
Update
If you would like to see the options that are in use on a current plot, you can use plot_theme(x) to see all of the options that are currently set for the plot named x. It will not be a comprehensive list, but should help if you want to change something like the font size for an axis label.
Update 2
With the transition to version 0.9.0 it's worth noting that the built in documentation has been dramatically improved, and the transition guide from 0.8.9 to 0.9.0 is also worth checking out.
Update 3
There is now a ggplot2 documentation website. Look at the documentation for theme for a complete list. Also, ?theme has a pretty complete list as of 0.9.3.
Update 4
There is now a ggthemes package that has some nice themes and scales to choose from. It might save you from having to create your own. See their github page for more information.
Entering in
theme_get()
will show a comprehensive listing of theme values and options. You can then follow the syntax to modify these attributes in opts().
The ggplot2 package does not contain much reference information-- this is probably because Hadley has put a lot of work into developing and polishing the package, creating a website full of examples and writing an excellent book that describes the system in detail.
The first place I would look for answers would be the ggplot2 website:
http://docs.ggplot2.org/
However, since opts() is not really a geom, stat or scale there are no examples that focus specifically on it. The next place to look would be the section of the website that contains material from the book-- including source code for examples:
http://ggplot2.org/book/
The example code for the chapter "Polishing your plots for publication" contains some well commented examples of using set_theme() and opts() to alter plot appearance.
The ultimate source for information is of course the book it's self. If you find ggplot2 has simplified and streamlined your workflow for producing statistical graphics, buying a copy of the book is a great way to say "thank you" and support the further development of the package.
Update
After some further investigation, you may be able to find a relatively complete list of options by listing out the source of one of the "theme" functions such as theme_bw. The source doesn't provide a description of the possible key=value pairs, but it does at least show most of the key names that have an effect when set via opts().
Besides the obvious references to ggplot2 reference manual and to the graphs section of Cookbook for R, Hadley Wickham provides a nice opts() List on github.
Theme templates:
https://github.com/jrnold/ggthemes
like "The economist", "Stata", "tufte" and more..
I know the answer is not exactly what was asked, but it was what I was looking for when I found this question, so others might too.
All the options I've ever used have been explained in hadley's great ggplot2 book.
Best list I've found for version 0.9.2.1 is here.
?opts
although, this does not display how to finetune its arguments, therefore it's better to check the options given above. If you can get a copy of the ggplot2 reference manual, it will help you a lot.
I made this quick reference for any theme or tasks you might look for. For a more general understanding, this ggplot2 tutorial should help.

Resources