-1

I have some data, example below, where i have used the base package 'table' creating a pivot with three objects. the table, as a list of values, prints how I'd like to see it, but it isn't formatted presentation quality. I'm wanting a pack_row (if i could get that to work) where a heading is "Country" while "Orbit", "Frequency" & "Type" are in 2 column and 1 row. If i make a dataframe from this, I get "Country" printed out 6 times where I'd rather have a heading only. datafram vs. table, prefer table output in Rmarkdown

here is some reproducible code. My work has about 60 "Country", and this is a minimal example:

{r initial}
library(knitr)
library(kableExtra)
#####
df <- data.frame(Country = c("AUS", "AUS", "GER","GER"),
                 Orbit = c("GEO","LEO","GEO","LEO"),
                 Type = c("TBD","Payload","Debris","Payload"),
                 Freq = c(1,2,4,8)
                 )
tbl <- table(df$Type, df$Orbit, df$Country, exclude = NA)
output <- as.data.frame(table(df$Type, df$Orbit, df$Country, exclude = NA) )
colnames(output) <- c("Type","Orbit","Country","Frequency")
output
kable(output)

Any ideas on how to format this nicely into an RMarkdown html output? thank you!

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
Barry vT
  • 109
  • 2

1 Answers1

0

There are a lot of HTML table formats, one simple HTML table that has a lot of customization is the DT package. Here is an example of what you posted with a DataTable(), it has built in ascend/descend function which is simple.

---
title: "Untitled"
author: "SO Answer"
date: "11/10/2020"
output: html_document
---

```{r}
library(knitr)
library(kableExtra)
library(DT)
#####
df <- data.frame(Country = c("AUS", "AUS", "GER","GER"),
                 Orbit = c("GEO","LEO","GEO","LEO"),
                 Type = c("TBD","Payload","Debris","Payload"),
                 Freq = c(1,2,4,8)
                 )
tbl <- table(df$Type, df$Orbit, df$Country, exclude = NA)
output <- as.data.frame(table(df$Type, df$Orbit, df$Country, exclude = NA)     )
colnames(output) <- c("Type","Orbit","Country","Frequency")
output
datatable(output)
```
Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • thanks Daniel, I really like R and learning RMarkdown as a more modern way of document deliverables. Trouble is i have to learn CSS and all that stuff! – Barry vT Nov 12 '20 at 03:48