使用formattable格式化闪亮数据表中的文本输出--似乎已停止工作
我有一个数据表,它使用格式示例在Shining中工作 代码似乎已经停止工作,我得到的不是一个格式为正数绿色、负数红色、零为黑色的表,而是一个空白输出 对于一个可复制的示例,我使用mtcars作为一个简单的闪亮示例,其他代码“开销”最小 最终目标是格式化一个表格,以便表格中的数字根据上述内容显示为彩色 谢谢你的帮助使用formattable格式化闪亮数据表中的文本输出--似乎已停止工作,r,shiny,datatable,formattable,R,Shiny,Datatable,Formattable,我有一个数据表,它使用格式示例在Shining中工作 代码似乎已经停止工作,我得到的不是一个格式为正数绿色、负数红色、零为黑色的表,而是一个空白输出 对于一个可复制的示例,我使用mtcars作为一个简单的闪亮示例,其他代码“开销”最小 最终目标是格式化一个表格,以便表格中的数字根据上述内容显示为彩色 谢谢你的帮助 library(shiny) library(formattable) library(tidyverse) # Define UI ui <- fluidPage(
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)
库(闪亮)
库(格式化表)
图书馆(tidyverse)
#定义用户界面
ui这可能是因为没有为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)
库(闪亮)
库(格式化表)
图书馆(dplyr)
#定义用户界面
谢谢你,阿克鲁!确实如此。我会接受答案(让我再等几分钟)。我以前没有遇到需要指定该包的情况……这是因为DT和data.table有冲突/屏蔽吗?@DaveM当我检查?dataTableOutput
时,它返回闪亮的,并且在DT
中。最好指定正确的包以避免屏蔽