Can DT be merged, formatted and shiny?
Formattable
there are some simple options for formatting the table, for example:
library(shiny)
library(DT)
library(formattable)
df <- formattable(iris, lapply(1:4, function(col){
area(col = col) ~ color_tile("red", "green")
This can then be hidden until DT
datatable
df <- as.datatable(df)
For me this works perfect for Viewer in RStudion. However, I would like to somehow deploy it as a Shiny app. Complete code:
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)
This doesn't work, is there any work to do? I like the conditional formatting from Formattable
, but would also like to use some of the options it DT
offers, for example for filtering, searching, colvis, etc.
To just expand it like Formattable
, there is a thread:
How do I use the R "formattable" package in a shiny dashboard?
source to share
Yes, it is possible it is possible as it is mentioned here . Here's a sample code on how to do it:
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)
source to share