我有一个数据表,使用格式示例 here
代码似乎已停止工作,而不是使用正数绿色、负数红色和零作为黑色格式化的表格,我得到一个空白输出。
对于可重现的示例,我使用 mtcars 作为一个简单的闪亮示例,其他代码“开销”最小。
最终目标是格式化表格,以便表格中的数字根据上述颜色显示。
感谢您的帮助!
library(shiny)
library(formattable)
library(tidyverse)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("example"),
mainPanel(
dataTableOutput("Table")
)
)
# Define server
server <- function(input, output) {
# Create formattable function for tables
sign_formatter <- formatter("span",
style = x ~ style(
color = ifelse(x > 0, "green",
ifelse(x < 0, "red", "black")),
font.weight = "bold"
))
# Identify numeric columns of table
numeric_cols <- colnames(mtcars[sapply(mtcars, is.numeric)])
# Render the table
output$Table <- renderDataTable({
table_to_return <- as.datatable(
formattable(
mtcars,
list(
area(, numeric_cols) ~ sign_formatter
)
) # formattable close
) # datatable close
}) # Render close
}
# Run the application
shinyApp(ui = ui, server = server)
答案 0 :(得分:1)
可能是因为 dataTableOutput
中未提及该软件包。将其更改为 DT::dataTableOutput
和 DT::renderDataTable
library(shiny)
library(formattable)
library(dplyr)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("example"),
mainPanel(
DT::dataTableOutput("Table")
)
)
# Define server
server <- function(input, output) {
# Create formattable function for tables
sign_formatter <- formatter("span",
style = x ~ style(
color = ifelse(x > 0, "green",
ifelse(x < 0, "red", "black")),
font.weight = "bold"
))
# Identify numeric columns of table
numeric_cols <- colnames(mtcars[sapply(mtcars, is.numeric)])
# Render the table
output$Table <- DT::renderDataTable({
table_to_return <-
as.datatable(
formattable(
mtcars,
list(
area(, numeric_cols) ~ sign_formatter
)
) # formattable close
) # datatable close
}) # Render close
}
# Run the application
shinyApp(ui = ui, server = server)
-输出