I am trying to generate the following table as a output in R shiny:
data <- matrix(c(67,543940,85),ncol = 1,byrow = T)
colnames(data) <- "France"
rownames(data) <- c("Population (m)", "Size (km)", "No. of cities")
summary <- as.table(data)
output$table <- renderTable(summary)
However the data is outputted in the following format:
Var1 | Var2 | Freq |
---|---|---|
Population (m) | France | 67 |
Size (km) | France | 543940 |
No. of cities | France | 85 |
Instead I want it the table to be displayed as:
France | |
---|---|
Population (m) | 67 |
Size (km) | 543940 |
No. of cities | 85 |
How would I do this in R shiny?
CodePudding user response:
There are likely many workarounds for this. I personally like the DT package for shiny data tables. To output your desired table you would need to coerce data into a data frame:
x <- as.data.frame(data)
shiny server:
output$france_table <- DT::renderDataTable({ x })
UI:
DT::dataTableOutput(france_table)
There are many options within the DT package to change how the table looks. Don't forget to install.packages("DT") and call the DT library.
CodePudding user response: