R for the Rest of Us Community
Public / Community
Public / Community
This is a place to ask questions and get help along the way on your R journey.
In addition to discussions of general questions, you’ll see threads for office hours. These are twicemonthly sessions to help you get unstuck. Ask questions and get live answers from me as well as guest experts. All code for office hours can be found here.
Not yet a member? Create a free account to join now.
Exporting tables

Sorry for the newbie question, but is there a way to export nicely made tables into R Markdown (e.g. using formattable) as images, similar how you can do so with figures? I realize this isn’t an ideal thing to do, but it’s something I often end up needing to do. Thank you in advance.

Great question, Daisy! You can do this in two packages that I’m familiar with: gt and flextable. I actually did this in a package I developed for a client. Sometimes you have to go with workarounds for odd reasons!

Hi David,
If you see this, I’m totally stuck on a flextable question – I have a table where every row is a group. Each column shows the mean at a particular time point, and then I have a final column that’s the overall mean across all time points. Is there any way I can do conditional formatting on the row values based on the overall mean value in the final column? I’ve been stuck on this over an hour and ready to give up and do it by hand!
Thanks for any advice,
Daisy

Sorry, just seeing this! Any chance we could go over it on Friday during the office hours session?

I’ll try to make it on Friday! But I figured out a solution, not sure if there is a more elegant one: I created a column that subtracts the mean from the value I’m interested in comparing. Then I wrote a formula for color that shades the value if the column difference is > 0. Finally, I used col_keys to only display the columns with the shaded values (and not the calculated difference columns). Probably a little clumsy but it worked…

Hi David,
Sorry for changing up my question today! After my boss saw the conditional formatting I did using flextable, he decided that what he wanted was a shaded heat map instead similar to what you can do in Excel. So I switched from using flextable to formattable after seeing this website that you posted a link to in one of your blogs. The problem I’m having is that I need the shading to be according to row values, not column values. I’ve Googled endlessly on this but can’t seem to make it work. Maybe I need to switch packages again? It looks like dt table has a rowwise heatmap option, but I can’t export the end result in an image (the table needs to go into a powerpoint deck). This is the first time since using R that I’ve been tempted to just do it in Excel, but I’m hoping you might have some ideas! ðŸ™‚
Thanks in advance,
Daisy

No worries! It’s definitely possible. I just couldn’t remember off the top of my head. Does this tutorial by Thomas Mock help you? I believe the
data_color()
function from gt does what you need. 

Thanks David! I will check this out and see if I can get it to work using gt

Hi David,
I picked this up again today after giving myself a mental break from it over the weekend, and I was able to get it work!! Thank you SO much for the sample code – it helped tremendously. I now have nice heatmap shading on my data, no Excel required. Best part is gt let me save it as an image. I think gt will be my goto table package from now on.
Daisy
Log in to reply.