daattali / shinycssloaders

⌛ Add loading animations to a Shiny output while it's recalculating
https://daattali.com/shiny/shinycssloaders-demo/
Other
399 stars 45 forks source link

Using shinycssloaders inside an rmarkdown #8

Closed reshdesu closed 1 year ago

reshdesu commented 7 years ago

I am using flexidashboard and shinycssloader v0.2.0. When I try to get the loading animation I get the message "Loading..." instead of the animation.

andrewsali commented 7 years ago

Do you have an example of how you apply the spinner in flexdashboard? As far as I know with Rmarkdown (runtime: shiny) you don't have control over the ui elements, they are automatically created based on the corresponding renderXXX function (which are in the server function in Shiny).

When I will have a bit more time I'll try to investigate if this can be circumvented somehow...

kent37 commented 6 years ago

I don't know if it is documented anywhere, but you can use the output variable in a RMarkdown Shiny document. This allows use of the plotOutput, etc. For example, the renderPlot portion of the boilerplate RMarkdown Shiny document can be modified to use shinycssloader as below. This example has a 2 second delay added to renderPlot so you can see the "Loading..." message (and not the spinner).

output$plot = renderPlot({
  Sys.sleep(2) # Make it take some time
  hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
       xlab = "Duration (minutes)", main = "Geyser eruption duration")

  dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  lines(dens, col = "blue")
})

shinycssloaders::withSpinner(plotOutput('plot'))
kent37 commented 6 years ago

The CSS for the loader doesn't seem to be inserted into the document.

andrewsali commented 6 years ago

I have added a new branch called rmarkdown. Installing the package from this branch provides an extra function called rmd_in_header. This can be used to create a static header file that can be included in an Rmd document.

Limitations are the following:

A working example is given here.

pernaletec commented 5 years ago

Hi @andrewsali , thank you very much for such a useful library.

I could add shinyccsloaders in my flexdashboard based document. However, I would like to know how to generate a new cssloader_in_header.html file using the rmd_in_header function. As you might suppose, I am using thecssloader_in_header.html example file.

Could you please give me a hint about how to use the rmd_in_header function?

Regards.

melissagwolf commented 4 years ago

Is this possible to use with flexdashboard? Following from the example you linked, I don't understand how to add it to the yaml when I am not outputting an html_document.

---
title: "Dynamic Model Fit"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    source_code: embed
always_allow_html: yes
---
daattali commented 4 years ago

shinycssloaders does not officially support flexdashboards because they are not shiny apps. It's possible that they might work with some special hacking, if someone here is able to get it to work and post their solution here that would be great, but it might not be possible.

melissagwolf commented 4 years ago

Cool, thank you! I definitely will not be the one to figure that out since I thought a flexdashboard was a shiny app, but hopefully someone else will :)

daattali commented 4 years ago

flexdashboard is rmarkdown. The line between rmarkdown and shiny keeps getting fuzzier with time, but it's still not shiny, sorry :)

melissagwolf commented 4 years ago

Thanks! This explains a lot.

pernaletec commented 4 years ago

Hi @melissagwolf @daattali . I was able to use it with Flexdashboard. I just did it the same way @andrewsali explained in his example. What is the exact problem you are having?

My yaml looks something like this:

title: "Dynamic Model Fit"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    includes:
      in_header: cssloaders_in_header.html
runtime: shiny
daattali commented 4 years ago

@pernaletec what version of shinycssloaders did you use? The master branch, or the special branch that @andrewsali created?

pernaletec commented 4 years ago

My main reference was this example: https://github.com/daattali/shinycssloaders/tree/rmarkdown/example/rmarkdown I strictly followed the work of @andrewsali ... thanks BTW!

daattali commented 4 years ago

Thanks for sharing

melissagwolf commented 4 years ago

@pernaletec @daattali Could one of you help me install the version from the special branch? I'm getting a pandoc error without it. Apologies for the entry level question!

I tried this:

library(devtools)
install_github("daattali/shinycssloaders", subdir="rmarkdown")

Downloading GitHub repo daattali/shinycssloaders@master Error: Failed to install 'shinycssloaders' from GitHub: Does not appear to be an R package (no DESCRIPTION)

daattali commented 4 years ago

"rmarkdown" is not a subdirectory, it's a branch. So don't use the "subdir" parameter. I forget what the right parameter is for installing a specific branch, it might be "ref".

melissagwolf commented 4 years ago

Ah. I got it to install, but I had to bypass the update for glue that was requested by the branch (only the binary version is available and my computer won't install it). This causes shinycss to fail when I run @andrewsali 's demo:

pandoc.exe: C:\Users\missg\AppData\Local\Temp\RtmpMBZ78l\cssloaders_in_header.html: openBinaryFile: does not exist (No such file or directory)

Warning: Error in : pandoc document conversion failed with error 1

C'est la vie - I will find another package! Thanks all for your help - I really appreciate it.

xiangnandang commented 4 years ago

Hi, this package and thread is new to me, but after some trial and error I sort of succeeded in my application:

I largely followed @andrewsali instructions, which worked mostly from the beginning. The only two things didn't work well were the spinner was on the very top edge of the container, and my plotlyOutput(height = "100%") was changed to default 400px.

I did the following to correct the two issues: in the cssloaders_in_header.html file, change from: .shiny-spinner-output-container { position: relative; } to: .shiny-spinner-output-container { height: 100%; }

This worked for my narrow-scoped application, but I didn't test for broader situations.

Best, Xiangnan

daattali commented 4 years ago

Thanks! If you think you got it working very well in a robust way, feel free to add a small section to the README telling future people how to do this :)

xiangnandang commented 4 years ago

Hi @daattali , thanks for your comment. I did a one-off trouble-shoot around the existing code, and provided the necessary modifications to make it successfully run in my only application. I'd definitely love to contribute more and do more robust testing. However, the fact that this function is not in the master branch (it's only in the rmarkdown branch last updated three years ago), makes it hard to contribute. Do you have plan to merge the rmarkdown branch to master?

Best, Xiangnan

daattali commented 4 years ago

You're right, I forgot this was all dependent on another branch. I suppose the best solution if we wanted to support Rmd would be to create a separate function and separate files, in the master branch, that would work with rmd. If anybody wants to take on that initiative, I would welcome that.

xiangnandang commented 4 years ago

I just checked, there's no merge conflict between rmarkdown and master branches, do you want me to create a pull request, or it's easier for you to do it. I could take a better look at the rmd_in_header function by @andrewsali and hopefully improve upon.

daattali commented 4 years ago

I'll let you take care of that :) Bring the necessary files in and please do some testing to make sure the regular withSpinner isn't affected

daattali commented 1 year ago

For completion, here is the solution @andrewsali (original author) proposed years ago (I'm copying it here because I want to clean up stale git branches). It includes 3 files:

File 1: R/rmd_in_header.R ```r #' Generate an html file to be included as `in_header` for Rmarkdown documents #' @param header_file The path to the html header file to be created #' @param type #' @param color #' @param size #' @param proxy.height rmd_in_header <- function(header_file="cssloaders_in_header.html", type=getOption("spinner.type",default=1),color=getOption("spinner.color",default="#0275D8"),size=getOption("spinner.size",default=1),color.background=getOption("spinner.color.background")) { file.remove(header_file) .shinycssloaders_headers <- file(header_file,"w"); writeLines("",.shinycssloaders_headers) writeLines("",.shinycssloaders_headers) close(.shinycssloaders_headers); } ```
File 2: cssloaders_in_header.html ``` ```
File 3: rmarkdown_example.Rmd ```` --- title: "shinycssloaders test" author: "Andras Sali" date: "12/16/2017" output: html_document: includes: in_header: cssloaders_in_header.html runtime: shiny --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library(tidyverse) ``` ## Including Plots You can also embed plots, for example: ```{r pressure, echo=FALSE} sliderInput("sleep_time","Sleep time:",1,10,5) plotOutput("pressure") %>% shinycssloaders::withSpinner(type=1) output$pressure <- renderPlot({ Sys.sleep(input$sleep_time) plot(1:5,1:5) }) ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. ````

If anyone has a good idea to add rmd support in a clean way, I'd be happy for a PR!

daattali commented 1 year ago

@mkinare @kent37 @pernaletec @xiangnandang @melissagwolf @M-Z @intael @robbfitzsimmons @seabbs @lvalnegri @fawda123 @juliasilge @pernaletec @razielar @MarcoFelipeKing

The latest github version of shinycssloaders should now support rmarkdown. Can you please try it and let me know if it works or not for you. Thanks!

kent37 commented 1 year ago

My simple example works.

melissagwolf commented 11 months ago

@mkinare @kent37 @pernaletec @xiangnandang @melissagwolf @M-Z @intael @robbfitzsimmons @seabbs @lvalnegri @fawda123 @juliasilge @pernaletec @razielar @MarcoFelipeKing

The latest github version of shinycssloaders should now support rmarkdown. Can you please try it and let me know if it works or not for you. Thanks!

It works well for me! showPageSpinner() and hidePageSpinner() were very easy to implement in my rmarkdown.