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

Code boxes don't run if opened for a second time #598

Closed
czucca opened this issue Oct 13, 2021 · 5 comments · Fixed by #599
Closed

Code boxes don't run if opened for a second time #598

czucca opened this issue Oct 13, 2021 · 5 comments · Fixed by #599

Comments

@czucca
Copy link

czucca commented Oct 13, 2021

Hi there,

I coauthor a teaching package depending on learnr that offers tutorials for students enrolled in our course. We are encountering problems when running the tutorials.

  • Install mypackage depending on learnr and gradethis
  • restart R
  • open any tutorial: it works fine.
  • close the tutorial (in both from browser or from the console)
  • open the same or a new tutorial: CODE BOXES show output directly and the user cannot interact with the tutorial any longer.

In order to explore the origin of the problem

  • I have restarted R.
  • I have installed the latest learnr from GitHub: remotes::install_github("rstudio/learnr")
  • I have installed the latest gradethis from GitHub: remotes::install_github("rstudio/gradethis")
  • I have restarted R again
  • run learnr::run_tutorial("ex-data-basics", "learnr") SAME PROBLEM AS ABOVE
  • learnr::run_tutorial("ex-data-summarise", "learnr") SAME PROBLEM AS ABOVE

System details

These are my system details, but we tried with more than one machine consistently getting the same error.
Output of sessioninfo::session_info():

- Session info -------------------------------------------------------------------
 setting  value                       
 version  R version 4.1.1 (2021-08-10)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_Netherlands.1252    
 ctype    English_Netherlands.1252    
 tz       Europe/Berlin               
 date     2021-10-13                  

- Packages -----------------------------------------------------------------------
 package        * version     date       lib
 assertthat       0.2.1       2019-03-21 [1]
 backports        1.2.1       2020-12-09 [1]
 broom            0.7.9.9000  2021-08-19 [1]
 bslib            0.3.1       2021-10-06 [1]
 cachem           1.0.6       2021-08-19 [1]
 cellranger       1.1.0       2016-07-27 [1]
 checkmate        2.0.0       2020-02-06 [1]
 cli              3.0.1       2021-07-17 [1]
 coda             0.19-4      2020-09-30 [1]
 colorspace       2.0-2       2021-06-24 [1]
 crayon           1.4.1       2021-02-08 [1]
 curl             4.3.2       2021-06-23 [1]
 DBI              1.1.1       2021-01-15 [1]
 dbplyr           2.1.1       2021-04-06 [1]
 digest           0.6.28      2021-09-23 [1]
 dplyr          * 1.0.7       2021-06-18 [1]
 ellipsis         0.3.2       2021-04-29 [1]
 evaluate         0.14        2019-05-28 [1]
 fansi            0.5.0       2021-05-25 [1]
 fastmap          1.1.0       2021-01-25 [1]
 forcats        * 0.5.1       2021-01-27 [1]
 fs               1.5.0       2020-07-31 [1]
 generics         0.1.0       2020-10-31 [1]
 ggplot2        * 3.3.5       2021-06-25 [1]
 glue             1.4.2       2020-08-27 [1]
 gradethis      * 0.2.4.9000  2021-10-13 [1]
 gtable           0.3.0       2019-03-25 [1]
 haven            2.4.3       2021-08-04 [1]
 highr            0.9         2021-04-16 [1]
 hms              1.1.0       2021-05-17 [1]
 htmltools        0.5.2       2021-08-25 [1]
 htmlwidgets      1.5.4       2021-09-08 [1]
 httpuv           1.6.3       2021-09-09 [1]
 httr             1.4.2       2020-07-20 [1]
 igraph           1.2.6       2020-10-06 [1]
 intergraph       2.0-2       2016-12-05 [1]
 jquerylib        0.1.4       2021-04-26 [1]
 jsonlite         1.7.2       2020-12-09 [1]
 knitr            1.36        2021-09-29 [1]
 later            1.3.0       2021-08-18 [1]
 lattice          0.20-44     2021-05-02 [1]
 learnr         * 0.10.1.9016 2021-10-13 [1]
 lifecycle        1.0.1       2021-09-24 [1]
 lubridate        1.8.0       2021-10-07 [1]
 magrittr         2.0.1       2020-11-17 [1]
 markdown         1.1         2019-08-07 [1]
 mime             0.12        2021-09-28 [1]
 modelr           0.1.8       2020-05-19 [1]
 munsell          0.5.0       2018-06-12 [1]
 network          1.17.1      2021-06-14 [1]
 nycflights13   * 1.0.2       2021-04-12 [1]
 pillar           1.6.3       2021-09-26 [1]
 pkgconfig        2.0.3       2019-09-22 [1]
 promises         1.2.0.1     2021-02-11 [1]
 purrr          * 0.3.4       2020-04-17 [1]
 R6               2.5.1       2021-08-19 [1]
 rappdirs         0.3.3       2021-01-31 [1]
 Rcpp             1.0.7       2021-07-07 [1]
 readr          * 2.0.1       2021-08-10 [1]
 readxl           1.3.1       2019-03-13 [1]
 renv             0.14.0      2021-07-21 [1]
 reprex           2.0.1       2021-08-05 [1]
 rlang            0.4.11      2021-04-30 [1]
 rmarkdown        2.11        2021-09-14 [1]
 rprojroot        2.0.2       2020-11-15 [1]
 rstudioapi       0.13        2020-11-12 [1]
 rvest            1.0.1       2021-07-26 [1]
 sass             0.4.0       2021-05-12 [1]
 scales           1.1.1       2020-05-11 [1]
 sessioninfo      1.1.1       2018-11-05 [1]
 shiny          * 1.7.1       2021-10-02 [1]
 SNA4DS           0.11.1      2021-10-13 [1]
 statnet.common   4.5.0       2021-06-05 [1]
 stringi          1.7.5       2021-10-04 [1]
 stringr        * 1.4.0       2019-02-10 [1]
 tibble         * 3.1.5       2021-09-30 [1]
 tidyr          * 1.1.3       2021-03-03 [1]
 tidyselect       1.1.1       2021-04-30 [1]
 tidyverse      * 1.3.1       2021-04-15 [1]
 tzdb             0.1.2       2021-07-20 [1]
 utf8             1.2.2       2021-07-24 [1]
 vctrs            0.3.8       2021-04-29 [1]
 withr            2.4.2       2021-04-18 [1]
 xaringan         0.22        2021-06-23 [1]
 xaringanExtra    0.5.4       2021-08-23 [1]
 xfun             0.26        2021-09-14 [1]
 xml2             1.3.2       2020-04-23 [1]
 xtable           1.8-4       2019-04-21 [1]
 yaml             2.2.1       2020-02-01 [1]
 source                                  
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 Github (tidymodels/broom@3177beb)       
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 Github (rstudio/gradethis@26741c6)      
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 Github (rstudio/learnr@b000739)         
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 Github (SNAnalyst/SNA4DS@ddd6da1)       
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 Github (gadenbuie/xaringanExtra@5e2d80b)
 CRAN (R 4.1.1)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          
 CRAN (R 4.1.0)                          

[1] C:/R/R-4.1.1/library

Describe the problem in detail

After installation, any tutorial correctly runs only one time. After the first run, tutorials are no longer interactive. They directly show output.
image one: tutorial opened for the first time.
image two: tutorial opened for the second time. No longer interactive.

image 1:

image

image 2:
image

@czucca
Copy link
Author

czucca commented Oct 13, 2021

It turns out that it works fine if the learnr library is attached before running the tutorial. However, since the tutorials are meant to be in a package depending on learnr (not always, but they can), it would be nice if you could enable the usage without having the learnr package attached. –

gadenbuie added a commit that referenced this issue Oct 15, 2021
`tutorial_knitr_options()` now prepares a list of knitr options with the knitr options and hooks that are used during the tutorial pre-render. This list is in the same format as the knitr options expected by `rmarkdown::output_format()` so we can set the knitr options directly in the `tutorial` output format.

This ensures that the hooks are set for each `rmarkdown::render()` call. Previously, by only relying on `.onAttach()`, these hooks might be reset at the end of the first `render()` and wouldn't be re-installed for subsequent renders, resulting in the behavior seen in #598. I haven't removed the `.onAttach()` mechanism because it is still useful for catching learnr component usage outside of the `learnr::tutorial()` format.
@SNAnalyst
Copy link

I have the same issue!
Perhaps this might shed some light on what is going wrong. I am using the tutorials from the Biostatistics package as an example, but this happens to me with any package that has learnr tutorials:

Fresh R session:

learnr::run_tutorial("03_Boxplots", "Biostatistics") # runs well
learnr::run_tutorial("03_Boxplots", "Biostatistics") # not interactive
learnr::run_tutorial("00_Introduction", "Biostatistics") # not interactive either

If I restart R and load learnr from the beginning it all works fine

library(learnr)
learnr::run_tutorial("03_Boxplots", "Biostatistics") # runs well
learnr::run_tutorial("03_Boxplots", "Biostatistics") # interactive
learnr::run_tutorial("00_Introduction", "Biostatistics") # interactive

If I restart R session but load learnr after the first tutorial, none of the tutorials after the first one are interactive anymore:

learnr::run_tutorial("03_Boxplots", "Biostatistics") # runs well
library(learnr)
learnr::run_tutorial("03_Boxplots", "Biostatistics") # not interactive
learnr::run_tutorial("00_Introduction", "Biostatistics") # not interactive either

Clicking "Start Over" doesn't solve this.

So, it seems that learn needs to be explicitly attached (which is annoying) AND that needs to happen before any tutorial is run, as any tutorial after the first one is no longer interactive.
It does not matter which package the tutorials come from:

library(learnr)
learnr::run_tutorial("03_Boxplots", "Biostatistics") # runs well
learnr::run_tutorial("ex-data-basics", "learnr") # interactive

# Restart R
learnr::run_tutorial("03_Boxplots", "Biostatistics") # runs well
library(learnr)
learnr::run_tutorial("ex-data-basics", "learnr") # not interactive

# Restart R
learnr::run_tutorial("03_Boxplots", "Biostatistics") # runs well
learnr::run_tutorial("ex-data-basics", "learnr") # not interactive

The quizzes always work, they are not affected by this.

@gadenbuie
Copy link
Member

Thanks @czucca and @SNAnalyst for the reports. The issue is that learnr requires specific knitr hooks to be installed to process the interactive exercise chunks. We've historically installed these when attaching learnr because technically learnr can be used in shiny prerendered html_document apps (and other places), so we need a flexible way to detect that learnr is being used.

Where that approach runs into problems, however, is when running the tutorial interactively, because rmarkdown::render() will clear any hooks that were created inside the tutorial. #599 will fix the issues you've reported, and you can test it out with the code below. I have a few more changes planned for that branch to catch a few additional edge cases.

devtools::install_github("rstudio/learnr@knitr-hooks-in-format")

@czucca
Copy link
Author

czucca commented Oct 15, 2021

@gadenbuie thank you so much for addressing the problem so quickly and smoothly!

@SNAnalyst
Copy link

@gadenbuie Wonderful, it works beautifully, thanks Garrick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants