Code Monkey home page Code Monkey logo

Comments (5)

renkun-ken avatar renkun-ken commented on June 3, 2024

@timelyportfolio any ideas?

from formattable.

daniel-matthews avatar daniel-matthews commented on June 3, 2024

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.

timelyportfolio avatar timelyportfolio commented on June 3, 2024

@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.

daniel-matthews avatar daniel-matthews commented on June 3, 2024

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.

github-actions avatar github-actions commented on June 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.