【问题标题】:Possible to combine DT, formattable and shiny?可以结合DT,可格式化和闪亮吗?
【发布时间】:2017-12-31 07:20:37
【问题描述】:

Formattable 有一些简单的选项来格式化表格,例如:

library(shiny)
library(DT)
library(formattable)

  df <- formattable(iris, lapply(1:4, function(col){

    area(col = col) ~ color_tile("red", "green")

稍后可以将其转换为 DT 数据表

df <- as.datatable(df)

对我来说,在 RStudion 的查看器中查看是完美的。但是,我想以某种方式将其部署为 Shiny 应用程序。完整代码:

library(DT)
library(shiny)

ui <- fluidPage(
  DT::dataTableOutput("table1"))


server <- function(input, output){

  df <- formattable(iris, lapply(1:4, function(col){

    area(col = col) ~ color_tile("red", "green")

  }))

  df <- as.datatable(df)

  output$table1 <- DT::renderDataTable(DT::datatable(df))

}

shinyApp(ui, server)

这不起作用,有什么解决方法吗?我喜欢formattable 的条件格式,但也想使用DT 提供的一些选项,例如过滤、搜索、colvis 等。

要将其部署为formattable,有一个线程:

How to use R package "formattable" in shiny dashboard?

【问题讨论】:

    标签: r shiny dt formattable


    【解决方案1】:

    是的,这似乎是可能的,正如here 所提到的。下面是一些关于如何实现的示例代码:

    library(shiny)
    library(data.table)
    library(formattable)
    
    ui <- fluidPage(
      selectInput("input1","Species: ", choices = c("setosa", "versicolor", "virginica")),
      DT::dataTableOutput("table1"))
    
    # make a data.table of the iris dataset. 
    df <- iris
    
    server <- function(input, output){
    
      output$table1 <- DT::renderDataTable( {
    
        my_df <- df[df$Species==input$input1,]
    
        return(as.datatable(formattable(my_df, lapply(1:4, function(col){area(col = col) ~ color_tile("red", "green")}))))
      }
      )
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 您是否设法在您的计算机上运行此程序?我复制粘贴并收到括号错误,但一切似乎都很好......
    • 对不起,这太草率了。我在已经复制到 Stack Overflow 时对代码进行了修改,但我错过了一个括号。请查看我的更新答案。
    • 现在如何向 DT 添加附加参数?例如转义行名。
    • 您可以在 as.datatable 调用中传递所有参数,函数在the documentation of formattable 中描述。希望这会有所帮助!
    猜你喜欢
    • 2016-12-16
    • 2019-10-25
    • 1970-01-01
    • 2019-03-28
    • 2016-10-25
    • 2020-09-18
    • 2021-05-08
    • 2018-12-03
    • 2018-08-04
    相关资源
    最近更新 更多