How to Use R Package "Formattable" in Shiny Dashboard

How to use R package formattable in shiny dashboard?

you have to use renderFormattable, formattableOutput and formattable, all three for it to work

library("shinydashboard")
library("shiny")
library("formattable")

body <- dashboardBody(
fluidRow(
column(width = 12,
box(formattableOutput("table"))
)
)
)

ui <- dashboardPage(
dashboardHeader(title = "Column layout"),
dashboardSidebar(),
body
)

server <- function(input, output) {

test.table <- data.frame(lapply(1:8, function(x) {1:10}))

output$table <- renderFormattable({formattable(test.table, list())})
}
shinyApp(ui = ui, server = server)

R Shiny : formattable does not change anything

This answer is from KoderKow (https://community.rstudio.com/t/r-shiny-formattable-does-not-change-anything/33465) :

The argument after data in formattable() is expecting a list, all we need to do us wrap disp in a list() function. working code below!

library(DT)
library(shiny)
library(shinydashboard)
library(data.table)
library(formattable)

ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
tabsetPanel(box(formattableOutput("dat"))
)
)
)

server <- function(input, output) {

data <- head(mtcars)

output$dat <- renderFormattable({
formattable(data, list(
disp = formatter("span",
style = x ~ style(color = ifelse(x < 200, "green", "gray")))
))
})

}

shinyApp(ui, server)

It works now

Reactive formattable in shiny?

You're using the syntax for formattable.data.table but in your case tmp is a matrix which behaves differently. It seems you want it to be a data.frame so you can cast it yourself. Also, you seem to have some problems with setting the color in your ifelse. This seems to do what you want

output$tab <- renderFormattable({
tmp <- summary(lmod())$coefficients
colnames(tmp) <- c("Coefficients", "SD", "t statistic", "Pvalue")
tmp <- signif(x = tmp, digits = 3)
tmp <- as.data.frame(tmp)
tmp <- formattable(tmp, list(
Pvalue = formatter("span", style = x ~ style(color = ifelse(x < 0.05, "red", "black"))))
)
})

Possible to combine DT, formattable and shiny?

Yes it seems to be possible, as also mentioned here. Here is some sample code on how to achieve that:

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)

How do you make a search bar to query a formattable table in a Shiny Dashboard

You can use as.datatable as shown below:

as.datatable(
formattable(blah blah blah)
)

Also if you want to use datatable options and filters etc. use them as shown below:

as.datatable(
formattable(blah blah blah)
,filter = 'top', rownames = FALSE)

See below for example: to see where to place your datatable editing features and where to place your formattable editing features:

as.datatable(
formattable(mydata,
align = c("l",rep("r", NCOL(mydata) - 1)),
list(`column_1` = formatter("span", style = ~ style(color = "grey", font.weight = "bold")),
`column_2` = formatter("span", style = ~ style(color = "blue")),
`column_3` = formatter("span", style = ~ style(color = "black")),
`column_4` = formatter("span", style = ~ style(color = "red", font.weight =
"bold"))
))
,filter = 'top', rownames = FALSE)

Format table output in R shiny based on user inputs

Here is your working code.

You must use that input minimal and maximal value as limits for your sequence (I just change it to range - is easier for user to put a range like that)
Then you generate sequence - according your notation - brks() - in my case I use length.out of 10 but you can put as many breaks as you want or dynamically.
Then generate on

number of colors - 1

and in the end in styleInterval() for background add limits of white - or any other color you want.

library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(DT)

sidebar <- dashboardSidebar(
sidebarMenu(id = "tab",
menuItem("1", tabName = "1")
)
)
body <- ## Body content
dashboardBody(box(width = 12,fluidRow(
fluidRow(column(
width = 3,
sliderInput("range_value",
label = h3("Put a range value"),
min = 0,
max = 100,
value = c(5, 15)
)
)
),
DT::dataTableOutput("op")
)))

ui <- dashboardPage(dashboardHeader(title = "Scorecard"),
sidebar,
body)

# Define the server code
server <- function(input, output,session) {
df <- data.frame(month = c("mazda 3", "mazda cx5", "mazda 6","mazda miata","honda
civic","honda accord"),
april = c(9, 8, 11,14,16,1),
may = c(3,4,15,12,11, 19),
june = c(2,11,9,7,14,1))
brks <- reactive({
seq(input$range_value[1], input$range_value[2], length.out = 10)
})

clrs <- reactive({ round(seq(255, 175, length.out = length(brks()) - 1), 0) %>%
{paste0("rgb(",.,",", ., ",255)")}})

df_format<- reactive ({datatable(df,options = list(searching = FALSE, pageLength = 15, lengthChange = FALSE)) %>%
formatStyle(names(df),
backgroundColor = styleInterval(c(brks()), c('white', clrs() ,'white'))
)
})

output$op <-renderDataTable({
df_format()
})
}

shinyApp(ui = ui, server = server)


Related Topics



Leave a reply



Submit