簡體   English   中英

使用 formattable 格式化 shiny 數據表中的文本 output - 似乎已停止工作

[英]Formatting text output in shiny data table using formattable--seems to have stopped working

我有一個使用此處的格式示例在 shiny 中工作的數據表

代碼似乎已經停止工作,而不是用正數綠色、負數紅色和零作為黑色格式化的表格,我得到一個空白的 output。

對於可重現的示例,我使用 mtcars 作為簡單的 shiny 示例,其他代碼“開銷”最少。

最終目標是格式化表格,以便表格中的數字根據上述顏色顯示。

感謝幫助!

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的 package 。 將其更改為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)

-輸出

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM