1

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)?

Konrad
  • 17,740
  • 16
  • 106
  • 167
Joe
  • 662
  • 1
  • 7
  • 20
  • 1
    I don't know about "nice", but you can use `edit(df)` – Rorschach Aug 01 '19 at 02:58
  • Thanks, I wasn’t aware of the edit( ) function. Maybe I’ll use that and a screenshot, if I cannot figure out another way. But it’s definitely not nice looking. – Joe Aug 01 '19 at 03:05
  • 2
    Where do you want to view this nice looking table? Print to the console? Maybe `knitr::kable(mtcars)`. In its own window? `View(mtcars)`. Save as an image? Something else? – Gregor Thomas Aug 01 '19 at 03:05
  • @Gregor, save as an image. Sorry for not being more clear. – Joe Aug 01 '19 at 03:06
  • I'd suggest [Create a PDF table](https://stackoverflow.com/q/3881278/903061) as a possible duplicate. Many of the answers would work with any graphic device. – Gregor Thomas Aug 01 '19 at 03:07
  • `xtabs` is maybe more "functional" than "nice-looking", but could be a step in the right direction? – Jason Aug 01 '19 at 03:11
  • @Gregor, if that link answers my question, then am I correct that the answer is “No. There is not a way in base R.”? – Joe Aug 01 '19 at 03:18
  • I think base R considers data frames as objects for calculation and manipulation of data, not visualization. The great thing about R is its package system, which extends it. Is there some reason you can only use base R ? – neilfws Aug 01 '19 at 03:40
  • @neilfws, no, I can and do use packages. I’m just trying to learn about R. If the answer is that printing nice looking tables is not a feature in base R, then someone should submit that as an answer, and I’ll accept it. – Joe Aug 01 '19 at 04:22

3 Answers3

3

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,])

Pretty mtcars output


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,]).

Konrad
  • 17,740
  • 16
  • 106
  • 167
  • 1
    Just so that I’m clear, my question was if there’s any base R function for rendering tables that look nice (enough to include in a report), and I’m going to infer that your answer, since you’re referring me to a package, is, “No. There is no function for that in base R.” – Joe Aug 01 '19 at 10:37
  • @Joe Hi Joes, yes that's my view. I have expanded the answer a little bit to address your valid comment. – Konrad Aug 01 '19 at 10:47
1

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

tom
  • 725
  • 4
  • 17
0

If you are using lm, lmer, glm, or glmer, the package sjplot creates really nice tables. sjplot is compatable with a suite of regression model packages. The function, tab_model can be utilized to create summary outputs of one or several models at once. I find that they are almost publication ready and easy to manipulate.