我有两张桌子想要垂直堆叠。下表包含原始数据,上表包含原始数据的摘要统计数据。问题是下面的表在最右侧有一个额外的列,该列在汇总表中不存在。我希望表格的前 N 列对齐,下部表格中的 N+1 列“伸出”到右侧。解释起来感觉很尴尬,所以我在 Excel 中做了一个可视化的例子。
下面是一个简单、可重现的 Shiny 示例,后面是我希望它执行的操作的 Excel 示例。
library( shiny )
library( data.table )
library( matrixStats )
ui = fluidPage(
mainPanel(
DT::dataTableOutput( "stats" ),
HTML( "<br>" ),
DT::dataTableOutput( "raw" )
)
)
server = function( input, output ){
set.seed( 123 )
raw = data.table( Item = c( "ABC", "DEF", "LMO", "XYZ" ),
Data1 = runif( 4, 1, 1000 ),
Data2 = runif( 4, 1, 1000 ),
Data3 = runif( 4, 1, 1000 ),
Data4 = runif( 4, 1, 1000 ),
Note = c( "Some notes are quite short", "Some notes could be very long, perhaps up to 100 or 150 characters", NA_character_, NA_character_ )
)
cols = colnames( raw )[ 2:5 ]
stats = data.table( min = colMins( as.matrix( raw[ , ..cols ] ) ),
mean = colMeans( as.matrix( raw[ , ..cols ] ) ),
median = colMedians( as.matrix( raw[ , ..cols ] ) ),
max = colMaxs( as.matrix( raw[ , ..cols ] ) ) )
stats = data.table( t( stats ), keep.rownames = T )
colnames( stats ) = c( "Stat", cols )
raw = DT::datatable( raw, rownames = F, filter = "none", options = list( dom = 't', iDisplayLength = -1 ) )
stats = DT::datatable( stats, rownames = F, filter = "none", options = list( dom = 't', iDisplayLength = -1 ) )
output$raw <- DT::renderDataTable( raw )
output$stats <- DT::renderDataTable( stats )
}
shinyApp (ui = ui,server = server )
您最好的选择是手动设置列的宽度,并确保您还指定了总宽度:
ui = fluidPage(
mainPanel(
DT::dataTableOutput("stats", width = "1000px"),
tags$br(),
DT::dataTableOutput("raw", width = "1200px")
)
)
# [...]
raw = DT::datatable(
raw,
rownames = FALSE,
filter = "none",
options = list(
dom = "t",
iDisplayLength = -1,
autoWidth = TRUE,
columnDefs = list(
list(width = '200px', targets = "_all")
)
)
)
stats = DT::datatable(
stats,
rownames = FALSE,
filter = "none",
options = list(
dom = "t",
iDisplayLength = -1,
autoWidth = TRUE,
columnDefs = list(
list(width = '200px', targets = "_all"))
)
)