我在 Shiny 中使用 renderTable 来显示数据集,我想对表格进行一些格式化。例如,如果我的代码如下所示:
用户界面:
shinyUI(fluidPage(
titlePanel("Hello Shiny!"),
fluidRow(
column(4,
... ...
)
),
column(8,
tableOutput("mydata")
)
)
))
服务器.R:
shinyServer(function(input, output) {
df <- data.frame(A=1:10, B=11:20)
output$mydata <- renderTable(df)
})
此示例仅显示了一个非常基本的表。如果我想将表格宽度设置为列宽的 100%,而 A 列占据表格宽度的 70% 并左对齐,则 B 列占据表格宽度的 30% 并向右对齐,并且列名的背景颜色设置为蓝色,如何使用renderTable 实现?非常感谢您的帮助。
顺便说一句,我可以引用有关renderTable格式设置的任何文档吗?
最佳答案
我不明白您所说的“将表格宽度设置为列宽的 100%”是什么意思,但您可能可以使用 DT 包实现所需的功能。 https://rstudio.github.io/DT/和渲染数据表
例如(不做对齐):
library(shiny)
library (DT)
df = data.frame(A=1:10, B=11:20)
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl')),
server = function(input, output) {
output$tbl = DT::renderDataTable(
df, options = list(
lengthChange = FALSE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#42f', 'color': '#fff'});",
"}"),
autowidth = TRUE,
columnDefs = list(list(width = '70%', targets = 1))
)
)
}
)
关于r - 如何格式化 Shiny 的渲染表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34009922/