r - 可以结合 DT、可格式化和 Shiny 吗?

标签 r shiny dt formattable

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?

最佳答案

是的,这似乎是可能的,正如也提到的 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)

关于r - 可以结合 DT、可格式化和 Shiny 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302045/

相关文章:

r - 导入指定时区的日期时间,忽略夏令时

与 Active Directory 集成的 Azure 应用服务上的 R Shiny 应用

r - 使 Valuebox 中的 Shiny 图标变小

R Shiny -为同一分析渲染多个输出

r - Shiny 的 DT 数据表 - 重置过滤器

r - Shiny:合并 DT::datatable 中的单元格

r - 如何使用 R 通过多个单独的组进行相同的字段聚合

正则表达式在括号之间挑选一些文本

在多页数据表中注册所有输入

R Markdown : place an Appendix after the "References" section?