2017-07-25 99 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