使用 formattable 格式化闪亮数据表中的文本输出 - 似乎已停止工作

问题描述

我有一个数据表,使用格式示例 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)

解决方法

可能是因为 dataTableOutput 中未提及该软件包。将其更改为 DT::dataTableOutputDT::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)

-输出

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...