Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a predictable file name within a temp folder #2137

Merged
merged 3 commits into from
May 12, 2021

Conversation

schloerke
Copy link
Contributor

Issue

When rendering runtime: shiny documents through shinytest, any plot figures are put into a *_files folder that is random every time a test is run.

This is problematic as the html field of the dynamic UI produced in

rmarkdown/R/shiny.R

Lines 304 to 318 in c88635f

# For test snapshots. (The snapshotPreprocessOutput function was added
# in shiny 1.0.4.)
if (exists("snapshotPreprocessOutput", asNamespace("shiny"))) {
doc_ui <- shiny::snapshotPreprocessOutput(
doc_ui,
function(value) {
# Since the html data can be very large, just record a hash of it.
value$html <- sprintf("[html data sha1: %s]",
digest::digest(value$html, algo = "sha1", serialize = FALSE)
)
value
}
)
}

If a plot is produced, value$html will contain something similar to

<p><img src="file847d61950236_files/figure-html/unnamed-chunk-12-1.png" width="960" data-figure-id=fig1 /></p>

The problem comes from the random name of file847d61950236_files, as it is random every time the rmarkdown document is rendered.

rmarkdown/R/shiny.R

Lines 452 to 454 in c88635f

# It's not cacheable, and should be rendered to a session-specific temporary
# file
output_dest <- tempfile(fileext = ".html")

tempfile()s do not listen to the set.seed() value.

> set.seed(1234); tempfile()
#> [1] "/var/folders/0k/bxg5lhr92sq74mb1d446ql540000gp/T//RtmpTi0WqW/filee5b3219c1a8f"
> set.seed(1234); tempfile()
#> [1] "/var/folders/0k/bxg5lhr92sq74mb1d446ql540000gp/T//RtmpTi0WqW/filee5b3f93b386"

Given the random output file name, the *_files directory is created from it.

rmarkdown/R/shiny.R

Lines 240 to 241 in c88635f

# check to see if the output already exists
resource_folder <- knitr_files_dir(output_dest)


Fix

If rmarkdown rendered inside a temp folder within the R session temp folder, then names could be predictable and would not need to worry about clashing.

Ex: Looking at the same <img> tag as above when running rmarkdown::run("index.Rmd") :

<p><img src="index_files/figure-html/unnamed-chunk-12-1.png" width="960" data-figure-id=fig1></p>

index_files is a consistent name, but because there was another temp folder used, then there is no clashing.


Cautions:

  • The temp folder is not deleted until the R session is stopped. (A slow memory leak.)

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@cderv Feel free to merge if it looks good to you, too.

R/shiny.R Outdated Show resolved Hide resolved
@yihui yihui requested a review from cderv May 12, 2021 17:51
Co-authored-by: Yihui Xie <xie@yihui.name>
Copy link
Collaborator

@cderv cderv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it looks good to me too.

I don't know a lot about shinytest so if it fixed the issue, then it is good. Thanks @schloerke !

@cderv cderv changed the title Use a predictable file name within a temp folder. Use a predictable file name within a temp folder May 12, 2021
@cderv cderv merged commit 9e19405 into rstudio:master May 12, 2021
schloerke added a commit to rstudio/shinycoreci that referenced this pull request Jun 2, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants