renkun-ken / formattable Goto Github PK
View Code? Open in Web Editor NEWFormattable Data Structures
License: Other
Formattable Data Structures
License: Other
I realize that this is not the standard use case. But I would like to display a matrix of weights with, say, color_tile, but the color should be chosen as a function of the row values.
On your example page, would there be a way to (for example) make all the 27 year old student's names be green? ie.. Can I reference a different column somehow?
Loving formattable in rstudio but would love to use it in shiny. Is there a plan to add render and output functions?
Implement the following functionality: parse character vector to predefined formattable objects. For example:
percent("15%")
accounting(c("1,000.0", "(1,500.0)"))
Hi,
UPDATE : I only see this problem with the output of knit HTML
but not when running the R script and Export
Save as Web Page
.
Using version 0.1.7
my first column that uses the formatter
syntax always appears too wide.
I notice in the generated HTML that the generated table width
and col width
values appear to be using %
rather than px
- is this a local knitr/RStudio/formattable setup issue or an issue with the package.
If I run your id
name
age
grade
example I see the following output...
`
`marcel_uk
> lapply(percent(c(0.1,0.2,0.3)), function(p) class(p))
[[1]]
[1] "numeric"
[[2]]
[1] "numeric"
[[3]]
[1] "numeric"
formattable
atomic vectors do not preserve formattable
class when called with *apply
functions. Note that lapply
coerces input vector by as.list
. Implement as.list.formattable
should work.
Assuming we can work through some of the lingering small issues, I'd like to make formattable
the new htmlwidget
of the week especially since it helps illustrate the various contexts in which R might use html/javascript
. Would this be ok?
There seems to be an issue formatting tables with just one row using the formatter. Using the example code given in the readme, replacing "df" with "df[1,]" invokes the problem:
formattable(df[1,], list(
# instead of
formattable(df, list(
The error message is:
Error in data.frame(mapply(function(x, name) { :
'row.names' should specify one of the variables
And the end of the traceback shows that there might be a problem in the "format_table" function in formattable.R:
13: stop("'row.names' should specify one of the variables")
12: data.frame(mapply(function(x, name) {
f <- formatters[[name]]
value <- if (is.null(f))
x
else if (inherits(f, "formula"))
eval_formula(f, x)
else match.fun(f)(x)
if (is.formattable(value))
as.character.formattable(value)
else value
}, x, names(x), SIMPLIFY = FALSE), row.names = row.names, check.rows = check.rows,
check.names = check.names, stringsAsFactors = FALSE)
11: format_table(list(id = 1L, name = "Bob", age = 28, grade = "C",
test1_score = 8.9, test2_score = 9.1, final_score = 9, registered = TRUE),
list(age = function (x)
{
if (length(x) == 0L)
return(character())
values <- lapply(args, function(arg) {
value <- if (is.function(arg))
arg(x)
else if (inherits(arg, "formula"))
eval_formula(arg, x)
else arg
if (is.null(value))
NA
else value
})
tags <- .mapply(function(...) {
attrs <- list(...)
htmltools::tag(.tag, attrs[!is.na(attrs) & nzchar(attrs)])
}, values, NULL)
vapply(tags, as.character, character(1L))
........
I've tried to debug it, but my understanding of R are not that good yet. Maybe someone know what needs to be done?
Thanks :)
Attaching formatting to values is a very neat approach. Any thoughts on making this compatible with ggplot? Example:
data.frame(x=formattable::percent(1:10) / 100, y = 1:10) %>% ggplot(aes(x=x, y=y)) + geom_point()
I would expect the formatting to show up on the axes.
Made this simple function to be able to put a formattable
in a pdf report; not sure if something like this is useful in the package itself. It exports a formattable
as e.g. png. Possibly something more generic would be useful in the webshot
package (see also wch/webshot#1). Also it has the external dependency on phantomjs
. But in any case, I thought I'd share it here:
#' Export a Formattable as PNG, PDF, or JPEG
#'
#' @param f A formattable.
#' @param file Export path with extension .png, .pdf, or .jpeg.
#' @param width Width specification of the html widget being exported.
#' @param height Height specification of the html widget being exported.
#' @param background Background color specification.
#' @param delay Time to wait before taking webshot, in seconds.
#'
#' @importFrom formattable as.htmlwidget
#' @importFrom htmltools html_print
#' @importFrom webshot webshot
#'
#' @export
export_formattable <- function(f, file, width = "100%", height = NULL,
background = "white", delay = 0.2)
{
w <- as.htmlwidget(f, width = width, height = height)
path <- html_print(w, background = background, viewer = NULL)
url <- paste0("file:///", gsub("\\\\", "/", normalizePath(path)))
webshot(url,
file = file,
selector = ".formattable_widget",
delay = delay)
}
Hi,
thanks for you package. It really works great!
Do you see a way to use sparklines together with your package?
Is there a way to truncate long strings to n characters for the table but then displaying the full string as a mouse-over tooltip?
An example here from DT package: See section 4.4.
> x <- 1:10
> x[5] <- 2.5
> x
[1] 1.0 2.0 3.0 4.0 2.5 6.0 7.0 8.0 9.0 10.0
> str(x)
num [1:10] 1 2 3 4 2.5 6 7 8 9 10
> x <- formattable(1:10)
> x[5] <- 2.5
> x
[1] 1 2 3 4 2 6 7 8 9 10
> str(x)
Classes 'formattable', 'integer' atomic [1:10] 1 2 3 4 2.5 6 7 8 9 10
..- attr(*, "formattable")=List of 4
.. ..$ formatter: chr "formatC"
.. ..$ format : list()
.. ..$ preproc : NULL
.. ..$ postproc : NULL
Love this idea. It might be nice to also have a function that will apply simple rules for the variable names to format them as well. For example, include an option to make variable names title case, and replace "_" and "." with a space. Probably could be set at the data.frame level.
I'd suggest check.names = FALSE
in the call to data.frame
in the body of formattable.data.frame
. (Otherwise, spaces in the column names get clobbered.)
Great package!!!
Create a simple HTML Rmd document and put a chuck with the following code:
library(formattable)
formattable(head(mtcars), list(mpg = color_bar("lightpink", 0.5)))
If the code is evaluated in interactive mode (in RStudio), the Viewer pane shows the following result:
The column mpg
with color_bar
is left-aligned, which is inconsistent with the rendered html output by Knit HTML from the Rmd:
Create a rmd with a chuck of the following code:
library(formattable)
formattable(head(mtcars), list(mpg = color_tile("transparent", "lightpink")))
formattable(head(mtcars), list(mpg = color_tile("transparent", "lightpink")),
format = "pandoc", caption = "table 2")
The caption of the table 2 in output html goes to table 1, as shown below:
However the resulted md
look normal but pandoc seems to regard the caption Table: table 2
below table 2 as the title of table 1.
Hi, discovering this package has made my day / week / month ... thank you!
I've found two issues with color_bar. Here is the original (ignore the yellow stuff bleeding over from the next column):
My solution was to override your normalize function and return x / max.
Here is the result
Hi. I have created a matrix object to show specific values based on cluster assignment. Originally, it had 5 columns and 17 rows. But I want the formatting to be specific to the rows, so I transposed it when I created it as a data frame. This threw the following error:
Error in match.fun(f) :
c("'structure(list(list(structure(list(name = \"i\", attribs = structure(list(' is not a function, character or symbol", "' class = \"glyphicon glyphicon-user\"), .Names = \"class\"), children = list()), .Names = c(\"name\", ' is not a function, character or symbol", "'\"attribs\", \"children\"), class = \"shiny.tag\")), NULL), class = c(\"shiny.tag.list\", ' is not a function, character or symbol", "'\"list\"))' is not a function, character or symbol")
So I went back and redrew the matrix to make the rows the columns and created my data frame from that, but I'm still getting the same error. I ran r is.formattable(clustMedian)
which came back FALSE, and then r is.formattable(formattable(clustMedian))
, which came back TRUE, but to no avail. I'm still getting the same error.
Here is my data frame code and a summary of the output.
# Prepare cluster summaries
clustMedian <- matrix(0,0, ncol = 17, nrow = 5)
rownames(clustMedian) <- c("Cluster 1", "Cluster 2", "Cluster 3",
"Cluster 4", "Cluster 5")
colnames(clustMedian) <- c("Total Players per Cluster",
"Mean Annual Visits",
"Mean Bet Amount",
"Mean Bet Total Per Visit",
"Mean Win/Loss Per Visit",
"Jan 15 Bet Mean", "Feb 15 Bet Mean",
"Mar 15 Bet Mean", "Apr 15 Bet Mean",
"May 15 Bet Mean", "Jun 15 Bet Mean",
"Jul 15 Bet Mean", "Aug 15 Bet Mean",
"Sept 15 Bet Mean",
"Oct 15 Bet Mean",
"Nov 15 Bet Mean", "Dec 15 Bet Mean")
clustMedian[,1] <- round(tapply(hClustReCluster$Player,
hClustReCluster$clusterID,
length))
clustMedian[,2] <- round(tapply(hClustReCluster$Track.Visit,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,3] <- round(tapply(hClustReCluster$MeanBet,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,4] <- round(tapply(hClustReCluster$MeanPerVisit,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,5] <- round(tapply(hClustReCluster$MeanWinLoss,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,6] <-
round(tapply(hClustReCluster$Jan15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,7] <-
round(tapply(hClustReCluster$Feb15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,8] <-
round(tapply(hClustReCluster$Mar15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,9] <-
round(tapply(hClustReCluster$Apr15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,10] <-
round(tapply(hClustReCluster$May15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,11] <-
round(tapply(hClustReCluster$Jun15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,12] <-
round(tapply(hClustReCluster$Jul15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,13] <-
round(tapply(hClustReCluster$Aug15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,14] <-
round(tapply(hClustReCluster$Sept15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,15] <-
round(tapply(hClustReCluster$Oct15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,16] <-
round(tapply(hClustReCluster$Nov.15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian[,17] <-
round(tapply(hClustReCluster$Dec.15.Bet.Amount,
hClustReCluster$clusterID,
mean), digits = 2)
clustMedian <- data.frame(clustMedian)
# Fix variable names
names(clustMedian) <- gsub(" ", ".", names(clustMedian))
names(clustMedian) <- gsub("/", ".", names(clustMedian))
> head(clustMedian)
Total.Players.per.Cluster Mean.Annual.Visits Mean.Bet.Amount
Cluster 1 177 1.72 4.85
Cluster 2 62 6.85 9.13
Cluster 3 68 24.62 11.21
Cluster 4 64 57.52 15.01
Cluster 5 133 128.37 14.07
Mean.Bet.Total.Per.Visit Mean.Win.Loss.Per.Visit
Cluster 1 28.21 -6.21
Cluster 2 123.57 -42.31
Cluster 3 158.91 -47.54
Cluster 4 200.60 -59.81
Cluster 5 448.72 -118.81
Jan.15.Bet.Mean Feb.15.Bet.Mean Mar.15.Bet.Mean
Cluster 1 10.53 5.07 1.71
Cluster 2 59.56 41.35 6.29
Cluster 3 261.73 168.33 116.73
Cluster 4 608.93 476.53 396.54
Cluster 5 3597.77 3965.24 4337.09
Apr.15.Bet.Mean May.15.Bet.Mean Jun.15.Bet.Mean
Cluster 1 0.61 6.10 3.05
Cluster 2 4.65 50.88 50.70
Cluster 3 57.82 135.29 125.58
Cluster 4 423.97 497.18 493.23
Cluster 5 4666.22 4579.95 3484.20
Jul.15.Bet.Mean Aug.15.Bet.Mean Sept.15.Bet.Mean
Cluster 1 0.47 0.74 0.86
Cluster 2 12.89 17.81 8.70
Cluster 3 81.64 120.59 127.57
Cluster 4 352.38 320.27 345.01
Cluster 5 3387.89 3717.29 3284.13
Oct.15.Bet.Mean Nov.15.Bet.Mean Dec.15.Bet.Mean
Cluster 1 3.69 2.18 8.73
Cluster 2 51.18 32.18 80.69
Cluster 3 161.89 174.77 145.09
Cluster 4 529.89 345.20 371.67
Cluster 5 3787.63 3572.47 3506.76
And here is the code I'm using to create the formattable table.
# CREATE FORMATTABLE TABLE
formattable(clustMedian, list(
Total.Players.per.Cluster = icontext("user"),
Mean.Annual.Visits = formatter("span",
style = x ~ style(color=ifelse(x > 46, "green","red"),
icontext(ifelse(x > 46, "chevron up", "chevron down")
))),
Mean.Bet.Amount = formatter("span",
style = x ~ style(color=ifelse(x > 10,"green","red"),
icontext(ifelse(x > 10, "chevron up","chevron down"))
)),
Mean.Bet.Total.Per.Visit = formatter("span",
style = x ~ style(color=ifelse(x >190,"green","red"),
icontext(ifelse(x > 190, "chevron up","chevron down"))
)),
Mean.Win.Loss.Per.Visit = formatter("span",
style = x ~ style(color=ifelse(x >-53,"green","red"),
icontext(ifelse(x > -53,"chevron up", "chevron down")
))),
Jan.15.Bet.Mean = color_bar("cadetblue1", 0.2),
Feb.15.Bet.Mean = color_bar("coral1", 0.2),
Mar.15.Bet.Mean = color_bar("aquamarine1", 0.2),
Apr.15.Bet.Mean = color_bar("goldenrod1", 0.2),
May.15.Bet.Mean = color_bar("orchid1", 0.2),
Jun.15.Bet.Mean = color_bar("lightcyan2", 0.2),
Jul.15.Bet.Mean = color_bar("palevioletred1", 0.2),
Aug.15.Bet.Mean = color_bar("lightcoral", 0.2),
Sept.15.Bet.Mean = color_bar("paleturquoise1", 0.2),
Oct.15.Bet.Mean = color_bar("plum", 0.2),
Nov.15.Bet.Mean = color_bar("lightsteelblue1", 0.2),
Dec.15.Bet.Mean = color_bar("salmon1", 0.2)))
For some reason, it does run if there Cluster IDs are the variables, but I want to be able to format the values for each cluster and don't see a way to apply formatting to rows. (I see that someone has asked that question.) Would appreciate any help you could offer.
Also would like to know if it's possible to combine the output with r xtable
in order to render the table vertically on the page. I tried it and got a mess.
Thanks!
Explore different ways to allow formattable
to work as an htmlwidget
. Currently, formattable
only works when in the context of a rmarkdown
document. Adding htmlwidget
functionality would allow it to work in other contexts such as console, RStudio Viewer, and Shiny.
> normalize(c(NA,NA,1), na.rm = TRUE)
[1] 1 1 1
> normalize(c(NA,NA,NA), na.rm = TRUE)
Error in if (all(x == 0)) return(x) :
missing value where TRUE/FALSE needed
Hi, found a bug related to encoding, very weird. Here's a mini repo exmpale.
formattable::formattable(
data.frame(
Name = '分红险农行'
))
formattable::formattable(
data.frame(
Name = '分红险-农行'
))
R version 3.2.2 (2015-08-14)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936
[2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
loaded via a namespace (and not attached):
[1] shiny_0.12.2.9006 htmlwidgets_0.5.1 R6_2.1.1
[4] markdown_0.7.7 htmltools_0.3 tools_3.2.2
[7] yaml_2.1.13 Rcpp_0.12.3 formattable_0.1.6
[10] highr_0.5.1 knitr_1.12 jsonlite_0.9.19
[13] digest_0.6.9 xtable_1.8-0 httpuv_1.3.3
[16] mime_0.4
It seems this use case example would be perfect for formattable
. I will attempt to recreate it.
If a formattable data.frame
is rendered to markdown
, then htmlwidget
feature would work. However, some table style like caption
seems unsupported by markdown
but supported by pandoc
. If one runs
formattable(head(mtcars), format = "pandoc")
Then the resulted htmlwidget
will be a messy output since pandoc table representation is not recognized by markdown
. One way to avoid this is to unsupport pandoc
output in format_table
. But a minor consequence is that arguments of knitr::kable
like caption
will no longer be supported or will be always ignored.
For example, if I run:
percent( c(a = .13, b =.34))
I get:
[1] 13.00% 34.00%
The vector still has the names property, but it is not being printed.
PS. Awesome package!
> num <- rnorm(5, 0.01, 0.01)
> num
[1] 0.020818893 0.004526958 0.009157277 0.030189282 0.005815931
> color_tile("white", "red")(num)
[1] "<span style=\"display: block; padding: 0 4px; border-radius: 4px; background-color: #ff5d5d\">0.0208188925786569</span>"
[2] "<span style=\"display: block; padding: 0 4px; border-radius: 4px; background-color: #ffffff\">0.00452695838799669</span>"
[3] "<span style=\"display: block; padding: 0 4px; border-radius: 4px; background-color: #ffd0d0\">0.00915727656078365</span>"
[4] "<span style=\"display: block; padding: 0 4px; border-radius: 4px; background-color: #ff0000\">0.0301892822241635</span>"
[5] "<span style=\"display: block; padding: 0 4px; border-radius: 4px; background-color: #fff2f2\">0.00581593056279019</span>"
> color_tile("white", "red")(percent(num))
Error: missing attribute for class 'formattable'.
Thanks for the fantastic package! I have wanted something like this for a long time.
Small issue (on the CRAN version): percent(NA)
gives an error: Error during wrapup: no applicable method for 'percent' applied to an object of class "logical"
However, percent(as.numeric(NA))
does work.
Workaround: formattalbe:::percent.numeric(NA)
Hi Renkun-Ken,
Thanks for this cool package! It really looks good and works well. I was wondering if it is possible to easily integrate dataTable features like sorting and filtering (as implemented in RStudio's DT library). I guess it's not that easy, but I am curious about you opinon.
Many thanks in advance.
Kind regards,
Jelle
Is it possible to escape characters in the column name so they will appear in the table? For example, if I have a column named "Profit Margin" when it is rendered into a table the name will be converted to "Profit.Margin". I have attached a sample image below:
I looked into using the formatter function, but from what I can tell it is only used to create different html attributes, such as style, for a given html tag, and not manipulate the inner text.
> gradient(c(1:10, NA), "white", "red")
Error in if (max == min) return(rep(1, length(x))) :
missing value where TRUE/FALSE needed
If normalize
in gradient
is called with na.rm = TRUE
then a column of NA
s will appear in the matrix, which causes csscolor
to produce #NANANA
color string.
Hi, How can content be aligned (left, center, right) within the table? For text as well as icons. Thanks.
formattable(
df,
list(
var1=formatter("span", style = x ~ style("text-align" = "center"))
)
)
Hi,
What a great package!
I see on the cran pdf a couple of ways to set width, but none of them seem to work anymore?
I just want to set the width for two tables I would like to place side by side in knitr via html.
Thanks
Scott
The following code works perfectly;
library(formattable)
df <- data.frame(A = 1:10,
B = 10:1)
formattable(df, list(
A = formatter('span', style = x ~ ifelse(x >= 5, style(color = "green"), NA)),
B = formatter('span', style = x ~ ifelse(x >= 5, style(color = "green"), NA))
))
However, when the plotly
package is loaded, I get the following error:
Error in last_plot() : The last plot doesn't exist
Any insight would be great.
sessionInfo() with only formattable loaded
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin15.0.0 (64-bit)
Running under: OS X 10.11.1 (El Capitan)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] formattable_0.1.6
loaded via a namespace (and not attached):
[1] shiny_0.12.2.9006 htmlwidgets_0.5 R6_2.1.1 markdown_0.7.7
[5] htmltools_0.3 tools_3.2.3 yaml_2.1.13 Rcpp_0.12.2
[9] highr_0.5.1 knitr_1.11 jsonlite_0.9.19 digest_0.6.8
[13] xtable_1.8-0 httpuv_1.3.3 mime_0.4
sessionInfo() with formattable + plotly loaded
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin15.0.0 (64-bit)
Running under: OS X 10.11.1 (El Capitan)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] plotly_2.1.0 ggplot2_2.0.0 formattable_0.1.6
loaded via a namespace (and not attached):
[1] Rcpp_0.12.2 magrittr_1.5 knitr_1.11 munsell_0.4.2
[5] colorspace_1.2-6 xtable_1.8-0 R6_2.1.1 stringr_1.0.0
[9] highr_0.5.1 httr_1.0.0 plyr_1.8.3 tools_3.2.3
[13] grid_3.2.3 gtable_0.1.2 htmltools_0.3 yaml_2.1.13
[17] digest_0.6.8 gridExtra_2.0.0 shiny_0.12.2.9006 viridis_0.3.2
[21] base64enc_0.1-3 htmlwidgets_0.5 mime_0.4 stringi_1.0-1
[25] scales_0.3.0 jsonlite_0.9.19 httpuv_1.3.3 markdown_0.7.7
I really enjoy this package! There is only one problem I would like to deal with. Take al look at this screenshot.
The two columns show scores with (I pressume) theoretically the same ranges. To my opinion the bar lengths should have the same length when dealing with the same score. As you can see this is not the case with score 9.1. I would like to be able to compare scores in two or more columns 'in one glance'. Because of the different bar lengths this not possible.
Is there way to fix the ranges withing the bar function? Many thanks in advance!
Hi,
I wonder whether it is possible to define a formatter based on multiple columns of a data.frame. I would like to do something along these lines...
p_format <- formatter("span",
style = x ~ ifelse(x <= 0.05 && my_second_variable <= 0, "background-color:red",NA))
Thanks!
Jurgen.
[df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE)
test <- formattable(df, list(
age = color_tile("white", "orange"),
grade = formatter("span",
style = x ~ ifelse(x == "A", style(color = "green", font.weight = "bold"), NA)),
test1_score = color_bar("pink", 0.2),
test2_score = color_bar("pink", 0.2),
final_score = formatter("span",
style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
registered = formatter("span",
style = x ~ style(color = ifelse(x, "green", "red")),
x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))
saveWidget(tst,'test.html')
Error in system.file(config, package = package) :
'package' must be of length 1](url)
Should this work within an R Markdown document? When I print out the df in the console the Value column has commas, but the column does not have commas within the datatable
df <- data.frame(Category=c("A", "B", "C"), Value=c(1324, 560, 2000), stringsAsFactors = FALSE)
df$Value <- formattable::comma(df$Value)
DT::datatable(df)
In RStudio, create an HTML RMarkdown document with a chunk of the following code:
library(formattable)
formattable(head(mtcars))
The options of the chunk is
echo=FALSE, results='asis'
If one clicks Knit HTML toolbar button to render the document, then everything works fine. If one calls
rmarkdown::render("~/test1.Rmd")
to render the document, assuming the document is saved at ~/test1.Rmd
, then the formattable data.frame
will be rendered to RStudio Viewer pane rather than the output html file. As a result, the file does not contain the table at all.
It seems that calling rmarkdown::render
does not set interactive()
to be FALSE
so that print.formattable
thinks the render should happen in an interactive environment.
Internally, rmarkdown::render
calls knitr::knit
to produce md
from Rmd
. I tested with
knitr::knit("~/test1.Rmd")
and the resulted md
does not contain the table contents.
This new package captioner
might work very well with formattable
. Do some investigation to see how these packages could benefit each other.
The current implementation of operators and methods for formattable
class preserves the base class in a way that may not work if mode has changed.
> z <- formattable(1:10) + 0.1
> z
[1] 1 2 3 4 5 6 7 8 9 10
> str(z)
Classes 'formattable', 'integer' atomic [1:10] 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1
..- attr(*, "formattable")=List of 4
.. ..$ formatter: chr "formatC"
.. ..$ format : list()
.. ..$ preproc : NULL
.. ..$ postproc : NULL
I think this is a conflict with df
from another package.
For some reason, I get this now. Can you check to see if you might also be having this problem? I'll keep digging for a solution.
Error in is.finite(x) : default method not implemented for type 'closure'
In addition: Warning message:
In formatC(function (x, df1, df2, ncp, log = FALSE) :
class of 'x' was discarded
When a data frame with unicode characters (e.g. Chinese), the formattable
is not correctly rendered in RStudio viewer pane but rendered correctly in html document produced by rmarkdown renderer.
df <- data.frame(id = 1:2, name = c("小明", "小红"), age=c(10,20))
formattable(df)
The locale of the R session must be set as Chinese first using
Sys.setlocale(locale = "Chinese")
for example, I have a column called rate, it comes in 0.xx format.
On one hand, I want to style it as colored bar chart using the formatter(style) function.
On the other hand, I'd like to change the text to xx%
Could you provide a minimal example on how to achieve this?
Thanks.
I really love beautiful tables, and this package gives me all I want! Thanks.
However, I would like the package has one feature that is: It should behave the same using library(formattable)
or just use formattable::fun
However, when in a shiny app, it cannot work, with error could not find function "as.htmlwidget"
here's a mini reproducible example.
library(shiny)
shiny::runApp(list(
ui = fluidPage(
formattable::formattableOutput("test")
),
server = function(input, output) {
output$test <- formattable::renderFormattable({
df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE)
formattable::formattable(df, list(
age = formattable::color_tile("white", "orange"),
grade = formattable::formatter(
"span",
style = x ~ ifelse(x == "A", formattable::style(color = "green", font.weight = "bold"), NA)),
test1_score = formattable::color_bar("pink", 0.2),
test2_score = formattable::color_bar("pink", 0.2),
final_score = formattable::formatter(
"span",
style = x ~ formattable::style(color = ifelse(rank(-x) <= 3, "green", "gray")),
x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
registered =
formattable::formatter("span",
style = x ~ formattable::style(color = ifelse(x, "green", "red")),
x ~ formattable::icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))
})
}
))
library(shiny)
library(formattable)
shiny::runApp(list(
ui = fluidPage(
formattable::formattableOutput("test")
),
server = function(input, output) {
output$test <- formattable::renderFormattable({
df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE)
formattable::formattable(df, list(
age = formattable::color_tile("white", "orange"),
grade = formattable::formatter(
"span",
style = x ~ ifelse(x == "A", formattable::style(color = "green", font.weight = "bold"), NA)),
test1_score = formattable::color_bar("pink", 0.2),
test2_score = formattable::color_bar("pink", 0.2),
final_score = formattable::formatter(
"span",
style = x ~ formattable::style(color = ifelse(rank(-x) <= 3, "green", "gray")),
x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
registered =
formattable::formatter("span",
style = x ~ formattable::style(color = ifelse(x, "green", "red")),
x ~ formattable::icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))
})
}
))
library(formattable)
Create a formattable data.table data.frame
object,
library(formattable)
library(data.table)
x1 <- formattable(data.table(x=1:5, y=rnorm(5)))
x1[1]
and subsetting does not work:
Error in FUN(X[[i]], ...) :
Invalid column: it has dimensions. Can't format it. If it's the result of data.table(table()), use as.data.table(table()) instead.
> percent(0.5) * 2
[1] 100.00%
> 2 * percent(0.5)
[1] 1
> percent(0.5) + 0.1
[1] 60.00%
> 0.1 + percent(0.5)
[1] 0.6
The current implementation of operators for class formattable
does not correctly handle -
which is +
with missing y
.
> library(formattable)
> z <- comma(123)
> -z
Error in create_obj(op(remove_class(x, class), unclass(y)), class, attr(x, :
argument "y" is missing, with no default
Hi, been loving formattable. Right now I am trying to reduce formattable column widths in a shiny app, the table I am working with is only 5 columns and it stretches across all available area!
Thanks for your help and time :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.