I am writing R shiny, and I want to make the column name of the renderTable to be center-aligned.
I have tried this, but it only makes the table move to the center to the panel.
#UI
column(12, align = "center", tableOutput("table_of_item")), (XXXWRONG!)
So
output$table_of_item <- renderTable({ #the table in the middle
if (is.null(rv2$data_batch))return() #to check the file is imported
t <- dataset_preprocess_batch(rv2$data_batch) #data preprocess
head(t,n=20) #show the data with the first 20 row only
})
table_of_item is one of the example, I want to make all the renderTable 's colname to be center-aligned.
Thanks for your help :)
CodePudding user response:
Try:
renderTable({
yourTable
}, align = "ccc")
with as many c
as there are columns in your table (i.e. one c
for each column).
CodePudding user response:
I would like to point you to a very good R package to easily create (and modify) almost all parts of a table. It is called gt
. See: https://gt.rstudio.com/index.html
It can also be used with shiny via output_gt()
and render_gt()
Here is some example-code that might work for your problem:
library(gt)
yourtable<-data.frame(col1=paste("Sample", letters[1:10]),col2=rnorm(10))
yourtable %>%
gt() %>%
cols_label(
col1="Sample Name",
col2="Values"
) %>%
cols_align(align = "center",
columns = 2)
However the cols_align()
function also aligns the whole column (not only its labels) as you asked in your comment... Not sure if this is possible at all. But maybe you can still use the package to create a nice table suitable to your needs.