2017-07-25 98 views
2

Formattable有一些简单的选项格式表,例如:可能结合DT,格式和闪亮?

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

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

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

这以后可以coverted到DT数据表

df <- as.datatable(df) 

对我来说,它的工作原理perfefect查看在浏览器中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?

回答

2

是的,它似乎是可能的,也提到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) 
+0

您是否设法在您的计算机上运行此操作?我复制粘贴和获取括号错误,但一切似乎没问题... – MLEN

+0

对不起,这是马虎。我已经复制到堆栈溢出时对代码进行了修改,但是我错过了一个括号。请看我更新的答案。 – Florian

+0

如何向DT添加附加参数?例如逃脱rownames。 – MLEN