with html results, css not working

Everything related to the development of modules in jamovi
Post Reply
jcthrawn
Posts: 5
Joined: Fri Jul 26, 2024 1:49 pm

with html results, css not working

Post by jcthrawn »

Hi,

I'm trying to create a html result for a Jamovi module, but the css stylesheets never work.

I'm trying to add the stylesheets as htmltools::htmlDependencies() attributes of the result object. Some styling is found in an "inst/tab.css" external file of the R package.

Below is a test example of the kind of things I'm trying to send into self$result$html_table$setContent() in my "*.b.R" R script. As an attachment you will also find a zipped RProject with all the required files and a test script ("R/test.R") :

Code: Select all

htmltestClass <- if (requireNamespace('jmvcore', quietly=TRUE)) R6::R6Class(
    "htmltestClass",
    inherit = htmltestBase,
    private = list(
        .run = function() {

          tab <- table(dplyr::pull(self$data, self$options$row_var),
                       dplyr::pull(self$data, self$options$col_var))

          html_tab <- tab |>
            knitr::kable(format = "html") |>
            kableExtra::kable_classic(lightable_options = "hover") |>
            kableExtra::add_footnote("This should be a very small footnote (font-size: 40%).",
                                     notation = "none", escape = FALSE)

          # from kableExtra:::print.kableExtra
          dep <- list(
            rmarkdown::html_dependency_jquery(),
            rmarkdown::html_dependency_bootstrap(theme = "cosmo"),
            kableExtra::html_dependency_kePrint(),
            kableExtra::html_dependency_lightable(),

            # A css with just ".lightable-classic tfoot {font-size: 20%;}"
            htmltools::htmlDependency(
              name = "tab_css",
              version = "1.00",
              src = "inst",
              meta = NULL,
              script = NULL,
              stylesheet = "tab.css",
              head = NULL,
              attachment = NULL,
              package = "jamovitest",
              all_files = FALSE
            )

          )
          html_tab <- htmltools::HTML(as.character(html_tab) )

          htmltools::htmlDependencies(html_tab) <- dep    # add attributes

          self$results$html_table$setContent(html_tab)
        })
)
The problem may come from the setContent() function of the html R6 result object, https://github.com/jamovi/jmvcore/blob/ ... 1/R/html.R :

Code: Select all

setContent=function(value) {

            knitted <- knitr::knit(text=value)

            knitMeta <- attr(knitted, 'html_dependencies')
            if ( ! is.null(knitMeta)) {
 […] 
                scripts <- sapply(knitMeta$script,     joinPaths, USE.NAMES=FALSE)
                sss     <- sapply(knitMeta$stylesheet, joinPaths, USE.NAMES=FALSE)

                private$.scripts <- scripts
                private$.stylesheets <- sss
            }

 […] 

        }
With R 4.3.1, jmvcore 2.4.7, knitr 1.4.7, the knitted object loses attributes and all html dependencies are lost. Just replacing the 2nd line by knitMeta <- attr(value, 'html_dependencies') may work, but I cannot replace my R6 objet setContent() function without having a "cannot change value for locked binding" error. If I am right, is there a workaround to make it work until the change is made in Jamovi ? If there's something I'm totally missing, what is the right format and the right way to use self$result$html_table$setContent() for an html result ?

Thanks,
User avatar
jonathon
Posts: 2752
Joined: Fri Jan 27, 2017 10:04 am

Re: with html results, css not working

Post by jonathon »

i'll need to look into this ... i did have knitr etc. working previously ... but i think there have been some changes to how knitr works in the intermediate years ...

could you create an issue over here:

https://github.com/jamovi/jamovi/issues

cheers

jonathon
jcthrawn
Posts: 5
Joined: Fri Jul 26, 2024 1:49 pm

Re: with html results, css not working

Post by jcthrawn »

Post Reply