Comments (5)
@timelyportfolio any ideas?
from formattable.
I've made some progress on this myself. I'm not confident in the quality of the code so haven't forked your code (also I'm new to github so not even sure I would know how if I tried), but it certainly seems to be doing what I hoped. The code I have added is the following:
formattableOutput <- function(outputId, width = "100%", height = "100%") {
shinyWidgetOutput(outputId, "formattable_widget", width, height, package = "formattable")
}
renderFormattable <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
shinyRenderWidget(expr, formattableOutput, env, quoted = TRUE)
}
The width and the height I have pretty much arbitrarily set to 100% (they wont accept NULL values and 100% seems to work..). Also the formattable object needs to be wrapped in as.htmlwidget in order for the render function to work (the automatic detection of interactive environments seems to fail. )
This is the full code of the shiny app I've used to testing (you will notice I've pinched your table form the readme)
##################################
# Global functions / dependencies
##################################
#load required libraries
library(shiny)
library(formattable)
library(htmlwidgets)
#This is the part that could be added to formattable to allow shiny integration. Not sure about height 100% but the function wont accept NULL
formattableOutput <- function(outputId, width = "100%", height = "100%") {
shinyWidgetOutput(outputId, "formattable_widget", width, height, package = "formattable")
}
renderFormattable <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
shinyRenderWidget(expr, formattableOutput, env, quoted = TRUE)
}
#Define a dataframe (example stolen from formattable readme)
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)
##################################
# Shiny server
##################################
server <- function(input, output) {
#use our new function to create an output called formattableexample. We require an explicit call to the as.htmlwidget function as this does not register as an interactive environment
output$formattableexample <- renderFormattable({
as.htmlwidget(
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")))
))
)
})
}
##################################
# Shiny server
##################################
ui <- fluidPage(
#use our new function to show the output we just defined.
fluidRow(formattableOutput("formattableexample"))
)
shinyApp(ui = ui, server = server)
from formattable.
@daniel-matthews That is exactly as I would have attacked it using the htmlwidget
Shiny template functions. As you noticed we are forced to explicitly use as.htmlwidget
or change the function to
renderFormattable <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(as.htmlwidget(expr)) } # force quoted
shinyRenderWidget(expr, formattableOutput, env, quoted = TRUE)
}
This works but I'm not shiny expert enough to know if this causes downstream difficulties so will require more testing. See bokeh/rbokeh#25 for a thorough discussion.
height
doesn't matter since HTML tables
disregard it as you might have noticed while you experimented with different values. I think width = "100%"
is a very reasonable default.
@renkun-ken this would be a good first step to a pull request or if @daniel-matthews is ok with it just copy these two functions into the as.htmlwidget file. Note this version forces as.htmlwidget
so the user does not have to remember.
#This is the part that could be added to formattable to allow shiny integration. Not sure about height 100% but the function wont accept NULL
formattableOutput <- function(outputId, width = "100%", height = "0") {
shinyWidgetOutput(outputId, "formattable_widget", width, height, package = "formattable")
}
renderFormattable <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(as.htmlwidget(expr)) } # force quoted
shinyRenderWidget(expr, formattableOutput, env, quoted = TRUE)
}
from formattable.
Thanks both, it works great. I've used it in quite a complicated shiny site and it works perfectly. I created the pull request.. but as Im new to this...no idea if I did it right...
from formattable.
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.
from formattable.
Related Issues (20)
- Embed hyperlink into Glyphicon HOT 3
- Support fontawesome HOT 1
- Feature Request: easy creation of image columns HOT 2
- Make formattable work with promises-package HOT 3
- Warning: Error in writeImpl: Text to be written must be a length-one character vector HOT 5
- Extract vector classes to a separate package?
- latex output
- Retire Travis and AppVeyor HOT 9
- Change header text by icontext
- invisible white text when rendering a table in knitr chunk HOT 3
- Update NEWS and create tag HOT 2
- Feature Request - Implement vctrs methods to support tidyr
- digit number HOT 2
- Is there a way to save formattable to pure html, not through htmlwidgets?
- Does formattable has similar function as `merge_v` in `flextable`?
- Formattable + DT Sorting Issue with multi colour bars.
- No formatted table produced HOT 4
- Use of hex codes within style() HOT 1
- conflict with patchwork HOT 2
- replace glyphicons with FontAwesome ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from formattable.