r - 在 Shiny 中显示 htmlTable
问题描述
我创建了一个返回 htmlTable 的函数,但我找不到我需要在闪亮的应用程序中正确打印它的渲染和输出。
我已经尝试过:
- 带有 uiOutput 和 htmlOutput 的 renderUI
- 带有 tableOutput 的 renderTable
- 带有 dataTableOutput 的 renderDataTable
闪亮的输出(带有renderUI和htmlOutput)是用于制作表格而不是表格本身的html代码。在 html() 中将 htmlTable() 包装在我的函数中,导致没有输出闪亮。
更新:在我的函数中包装 htmlTable() 在 HTML() 中有效。感谢大家的所有提示!
my_function <- function(nr_TP, nr_sus_normal, nr_FP, nr_FN, nr_sus_fraud, nr_TN){
result_table <- cbind(Normal = c("nr_TP", "nr_sus_normal", "nr_FP"), Fraud = c("nr_FN", "nr_sus_fraud", "nr_TN"))
row.names(result_table) <- c('Normal', "Suspicious", "Fraud")
where <- rbind(c(1,1), c(1,2), c(2,1), c(2,2), c(3,1), c(3,2))
style <- c('background-color: green; color: white;',
'background-color: red; color: white;',
'background-color: yellow; color: black;',
'background-color: yellow; color: black;',
'background-color: orange; color: black;',
'background-color: green; color: white;')
css.cell <- matrix('', nrow(result_table), ncol(result_table))
css.cell[where] <- style
htmlTable(result_table, css.cell = css.cell, cgroup = c("As classified by AD"),n.cgroup = c(2))}
ui <- fluidPage(
titlePanel("Test-report and correlation overview"),
sidebarLayout(
sidebarPanel(
numericInput("nr_TP",
h3("Number of Digits"),
value = 4, min = 0, max = 10),
numericInput("nr_sus_normal",
h3("Number of Digits"),
value = 4, min = 0, max = 10),
numericInput("nr_FP",
h3("Number of Digits"),
value = 4, min = 0, max = 10),
numericInput("nr_FN",
h3("Number of Digits"),
value = 4, min = 0, max = 10),
numericInput("nr_sus_fraud",
h3("Number of Digits"),
value = 4, min = 0, max = 10),
numericInput("nr_TN",
h3("Number of Digits"),
value = 4, min = 0, max = 10)
),
mainPanel(
uiOutput("performance_table"),
)
)
)
server <- function(input, output) {
output$performance_table <- renderUI({
inFile <- dataInput()
if (is.null(inFile))
{return(NULL)}
else
{
performance_table(input$nr_TP, input$nr_sus_normal, input$nr_FP, input$nr_FN, input$nr_sus_fraud, input$nr_TN)
}
}
我通常有不同的输入,函数 performance_table 会自行计算这些值。但问题不在于功能,问题是我无法在我的闪亮中显示 htmlTable。当我使用格式表时(连同 formattableOutput 和 renderFormattable 我很好,但我想在不使用依赖格式的情况下为不同的单元格着色,而且我无法在格式表中修复它)。
解决方案
您应该结合使用 print 功能和 kable extra。然后像这样使用 renderUI 和 UI 输出:
服务器部分:
output$table <- renderUI(print(kable(table_of_analysis(), caption = "Title : Subitle") %>% kable_styling()))
用户界面部分:
uiOutput("table")
推荐阅读
- javascript - 如何使滚动框响应?
- linux - 如何使用托管在 Kestrel/linux 中的 aspnet core 3.1 配置签名 SSL 证书
- windows - CMD 提示在 bat 打开时立即关闭
- json - 在 ARM 模板中正确定义 AutomaticUnprovisionInterval
- python - 直接绘制数据集的不同颜色的条形图
- oracle - 查询的 XML 处理中发生 Oracle SQL 错误
- python - Quart 的基本身份验证 - python
- python - 有理由不在任何地方使用 cython 吗?
- regex - 使用正则表达式检测文本中的表情符号。拆分文本而不删除表情符号。(飞镖,颤振)
- python - Python:使用 Dijkstra 帮助寻找最短路径