Search code examples
rdocumentationdocument

Running dontrun part of examples within example function


I want to run dontrun part of examples within example function. Tried both run.dontrun=TRUE and run.dontrun = FALSE options but getting the same output. Any thoughts.

install.packages("eda4treeR")

With run.dontrun=TRUE option

library(eda4treeR)
example(
    topic          = "Exam8.2"
  , package        = "eda4treeR"
  , lib.loc        = NULL
  , character.only = c(TRUE, FALSE)[2]
  , give.lines     = c(TRUE, FALSE)[2]
  , local          = c(TRUE, FALSE)[2]
  , type           = c("console", "html")[2]
  , echo           = c(TRUE, FALSE)[1]
  , verbose        = getOption("verbose")
  , setRNG         = c(TRUE, FALSE)[1]
  , ask            = getOption("example.ask")
  , prompt.prefix  = NULL
  , run.dontrun    = c(TRUE, FALSE)[1]
  , run.donttest   = c(TRUE, FALSE)[2]
  )

### ** Examples

library(car)
library(dae)
library(dplyr)
library(emmeans)
library(ggplot2)
library(lmerTest)
library(magrittr)
library(predictmeans)
library(supernova)

data(DataExam8.2)

# Pg.
fm8.2  <-
  lmer(
    formula = dbhmean ~ Repl + Column + Contcompf + Contcompf:Standard +
              (1|Repl:Row ) + (1|Repl:Column ) + (1|Contcompv:Clone)
  , data    = DataExam8.2
    )
fixed-effect model matrix is rank deficient so dropping 5 columns / coefficients
## Not run: 
##D varcomp(fm8.2)
## End(Not run)
anova(fm8.2)
Missing cells for: Contcompf0:Standard0, Contcompf1:StandardUG323, Contcompf1:StandardU6, Contcompf1:StandardPN14, Contcompf1:StandardSSOseed.  
Interpret type III hypotheses with care.
Type III Analysis of Variance Table with Satterthwaite's method
                    Sum Sq Mean Sq NumDF   DenDF F value
Repl                3.2720  0.8180     4  26.467  2.0489
Column              3.1018  0.6204     5  19.545  1.5539
Contcompf           5.3203  5.3203     1  54.905 13.3265
Contcompf:Standard 20.6587  6.8862     3 207.152 17.2488
                            Pr(>F)    
Repl                     0.1162606    
Column                   0.2194719    
Contcompf                0.0005845 ***
Contcompf:Standard 0.0000000004896 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(fm8.2, type = "II", test.statistic = "Chisq")
Analysis of Deviance Table (Type II Wald chisquare tests)

Response: dbhmean
                     Chisq Df Pr(>Chisq)    
Repl                8.1957  4    0.08467 .  
Column              7.7694  5    0.16941    
Contcompf           4.6841  1    0.03044 *  
Contcompf:Standard 51.7463  3  3.392e-11 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
predictmeans(model = fm8.2, modelterm = "Repl")
Warning in Kmatrix(model, modelterm, covariate, prtnum = prtnum):
Missing treatments' combination appeared, predicted means maybe
misleading!
Warning in Kmatrix(model, modelterm): Missing treatments' combination
appeared, predicted means maybe misleading!
$`Predicted Means`
Repl
     1      2      3      4      5 
7.8926 8.2070 8.3429 8.4604 8.5464 

$`Standard Error of Means`
Repl
      1       2       3       4       5 
0.33123 0.33126 0.32992 0.32992 0.32992 

$`Standard Error of Differences`
  Max.SED   Min.SED  Aveg.SED 
0.2239675 0.2167320 0.2196681 

$LSD
 Max.LSD  Min.LSD Aveg.LSD 
 0.44792  0.43345  0.43932 
attr(,"Significant level")
[1] 0.05
attr(,"Degree of freedom")
[1] 60.56

$mean_table
  Repl   Mean      SE       Df LL(95%) UL(95%)
1    1 7.8926 0.33123 60.55892  7.2302  8.5551
2    2 8.2070 0.33126 60.55892  7.5445  8.8695
3    3 8.3429 0.32992 60.55892  7.6831  9.0027
4    4 8.4604 0.32992 60.55892  7.8006  9.1202
5    5 8.5464 0.32992 60.55892  7.8866  9.2062
predictmeans(model = fm8.2, modelterm = "Column")
Warning in Kmatrix(model, modelterm, covariate, prtnum = prtnum):
Missing treatments' combination appeared, predicted means maybe
misleading!

Warning in Kmatrix(model, modelterm, covariate, prtnum = prtnum):
Missing treatments' combination appeared, predicted means maybe
misleading!
$`Predicted Means`
Column
     1      2      3      4      5      6 
8.2214 8.4708 8.3779 7.9721 7.8166 8.7141 

$`Standard Error of Means`
Column
      1       2       3       4       5       6 
0.31662 0.39168 0.39315 0.26648 0.26646 0.31653 

$`Standard Error of Differences`
  Max.SED   Min.SED  Aveg.SED 
0.2714760 0.2102583 0.2373610 

$LSD
 Max.LSD  Min.LSD Aveg.LSD 
 0.54413  0.42143  0.47575 
attr(,"Significant level")
[1] 0.05
attr(,"Degree of freedom")
[1] 54.65

$mean_table
  Column   Mean      SE       Df LL(95%) UL(95%)
1      1 8.2214 0.31662 54.64679  7.5868  8.8561
2      2 8.4708 0.39168 54.64679  7.6857  9.2558
3      3 8.3779 0.39315 54.64679  7.5900  9.1659
4      4 7.9721 0.26648 54.64679  7.4380  8.5063
5      5 7.8166 0.26646 54.64679  7.2825  8.3507
6      6 8.7141 0.31653 54.64679  8.0797  9.3486
library(emmeans)
emmeans(object = fm8.2, specs = ~Contcompf|Standard)
NOTE: A nesting structure was detected in the fitted model:
    Standard %in% Contcompf
Contcompf = 1, Standard = 0:
 emmean    SE   df lower.CL upper.CL
   8.91 0.117 65.9     8.67     9.14

Contcompf = 0, Standard = UG323:
 emmean    SE   df lower.CL upper.CL
   8.97 0.770 55.6     7.43    10.51

Contcompf = 0, Standard = U6:
 emmean    SE   df lower.CL upper.CL
   6.55 0.770 55.5     5.01     8.10

Contcompf = 0, Standard = PN14:
 emmean    SE   df lower.CL upper.CL
   7.70 0.771 55.8     6.16     9.25

Contcompf = 0, Standard = SSOseed:
 emmean    SE   df lower.CL upper.CL
   6.08 0.770 55.5     4.54     7.63

Results are averaged over the levels of: Repl, Column 
Degrees-of-freedom method: kenward-roger 
Confidence level used: 0.95 

With run.dontrun=FALSE option

example(
    topic          = "Exam8.2"
  , package        = "eda4treeR"
  , lib.loc        = NULL
  , character.only = c(TRUE, FALSE)[2]
  , give.lines     = c(TRUE, FALSE)[2]
  , local          = c(TRUE, FALSE)[2]
  , type           = c("console", "html")[2]
  , echo           = c(TRUE, FALSE)[1]
  , verbose        = getOption("verbose")
  , setRNG         = c(TRUE, FALSE)[1]
  , ask            = getOption("example.ask")
  , prompt.prefix  = NULL
  , run.dontrun    = c(TRUE, FALSE)[2]
  , run.donttest   = c(TRUE, FALSE)[2]
  )

### ** Examples

library(car)
library(dae)
library(dplyr)
library(emmeans)
library(ggplot2)
library(lmerTest)
library(magrittr)
library(predictmeans)
library(supernova)

data(DataExam8.2)

# Pg.
fm8.2  <-
  lmer(
    formula = dbhmean ~ Repl + Column + Contcompf + Contcompf:Standard +
              (1|Repl:Row ) + (1|Repl:Column ) + (1|Contcompv:Clone)
  , data    = DataExam8.2
    )
fixed-effect model matrix is rank deficient so dropping 5 columns / coefficients
## Not run: 
##D varcomp(fm8.2)
## End(Not run)
anova(fm8.2)
Missing cells for: Contcompf0:Standard0, Contcompf1:StandardUG323, Contcompf1:StandardU6, Contcompf1:StandardPN14, Contcompf1:StandardSSOseed.  
Interpret type III hypotheses with care.
Type III Analysis of Variance Table with Satterthwaite's method
                    Sum Sq Mean Sq NumDF   DenDF F value
Repl                3.2720  0.8180     4  26.467  2.0489
Column              3.1018  0.6204     5  19.545  1.5539
Contcompf           5.3203  5.3203     1  54.905 13.3265
Contcompf:Standard 20.6587  6.8862     3 207.152 17.2488
                            Pr(>F)    
Repl                     0.1162606    
Column                   0.2194719    
Contcompf                0.0005845 ***
Contcompf:Standard 0.0000000004896 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(fm8.2, type = "II", test.statistic = "Chisq")
Analysis of Deviance Table (Type II Wald chisquare tests)

Response: dbhmean
                     Chisq Df Pr(>Chisq)    
Repl                8.1957  4    0.08467 .  
Column              7.7694  5    0.16941    
Contcompf           4.6841  1    0.03044 *  
Contcompf:Standard 51.7463  3  3.392e-11 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
predictmeans(model = fm8.2, modelterm = "Repl")
Warning in Kmatrix(model, modelterm, covariate, prtnum = prtnum):
Missing treatments' combination appeared, predicted means maybe
misleading!
Warning in Kmatrix(model, modelterm): Missing treatments' combination
appeared, predicted means maybe misleading!
$`Predicted Means`
Repl
     1      2      3      4      5 
7.8926 8.2070 8.3429 8.4604 8.5464 

$`Standard Error of Means`
Repl
      1       2       3       4       5 
0.33123 0.33126 0.32992 0.32992 0.32992 

$`Standard Error of Differences`
  Max.SED   Min.SED  Aveg.SED 
0.2239675 0.2167320 0.2196681 

$LSD
 Max.LSD  Min.LSD Aveg.LSD 
 0.44792  0.43345  0.43932 
attr(,"Significant level")
[1] 0.05
attr(,"Degree of freedom")
[1] 60.56

$mean_table
  Repl   Mean      SE       Df LL(95%) UL(95%)
1    1 7.8926 0.33123 60.55892  7.2302  8.5551
2    2 8.2070 0.33126 60.55892  7.5445  8.8695
3    3 8.3429 0.32992 60.55892  7.6831  9.0027
4    4 8.4604 0.32992 60.55892  7.8006  9.1202
5    5 8.5464 0.32992 60.55892  7.8866  9.2062
predictmeans(model = fm8.2, modelterm = "Column")
Warning in Kmatrix(model, modelterm, covariate, prtnum = prtnum):
Missing treatments' combination appeared, predicted means maybe
misleading!

Warning in Kmatrix(model, modelterm, covariate, prtnum = prtnum):
Missing treatments' combination appeared, predicted means maybe
misleading!
$`Predicted Means`
Column
     1      2      3      4      5      6 
8.2214 8.4708 8.3779 7.9721 7.8166 8.7141 

$`Standard Error of Means`
Column
      1       2       3       4       5       6 
0.31662 0.39168 0.39315 0.26648 0.26646 0.31653 

$`Standard Error of Differences`
  Max.SED   Min.SED  Aveg.SED 
0.2714760 0.2102583 0.2373610 

$LSD
 Max.LSD  Min.LSD Aveg.LSD 
 0.54413  0.42143  0.47575 
attr(,"Significant level")
[1] 0.05
attr(,"Degree of freedom")
[1] 54.65

$mean_table
  Column   Mean      SE       Df LL(95%) UL(95%)
1      1 8.2214 0.31662 54.64679  7.5868  8.8561
2      2 8.4708 0.39168 54.64679  7.6857  9.2558
3      3 8.3779 0.39315 54.64679  7.5900  9.1659
4      4 7.9721 0.26648 54.64679  7.4380  8.5063
5      5 7.8166 0.26646 54.64679  7.2825  8.3507
6      6 8.7141 0.31653 54.64679  8.0797  9.3486
library(emmeans)
emmeans(object = fm8.2, specs = ~Contcompf|Standard)
NOTE: A nesting structure was detected in the fitted model:
    Standard %in% Contcompf
Contcompf = 1, Standard = 0:
 emmean    SE   df lower.CL upper.CL
   8.91 0.117 65.9     8.67     9.14

Contcompf = 0, Standard = UG323:
 emmean    SE   df lower.CL upper.CL
   8.97 0.770 55.6     7.43    10.51

Contcompf = 0, Standard = U6:
 emmean    SE   df lower.CL upper.CL
   6.55 0.770 55.5     5.01     8.10

Contcompf = 0, Standard = PN14:
 emmean    SE   df lower.CL upper.CL
   7.70 0.771 55.8     6.16     9.25

Contcompf = 0, Standard = SSOseed:
 emmean    SE   df lower.CL upper.CL
   6.08 0.770 55.5     4.54     7.63

Results are averaged over the levels of: Repl, Column 
Degrees-of-freedom method: kenward-roger 
Confidence level used: 0.95

Solution

  • TL;DR tools::example2html was rewritten to satisfy your expectations, and browse_example is offered. Examples will be run and converted to HTML on the fly.

    I can reproduce your problem with:

    library(eda4treeR)
    example(
        topic = "Exam8.2",
        package = "eda4treeR", 
        run.dontrun = c(TRUE, FALSE)[1], 
        type = c("console", "html")[2]
    )
    

    I discovered that the problem you faced only exists if you use the type="html", and for type="console" it works perfectly fine.

    I investigate it further and investigate the internals of the utils::example. For the HTML type output the function accesses the R DB with HTML items like demo/examples/docs for each package. This DB is built with a default setup so the run.dontrun does not impact results. For example, I can access the mice::mice example with http://127.0.0.1:24851/library/mice/Example/mice; you should be able to get the port with port <- tools::startDynamicHelp(NA).

    The R HTML DB is built from static content (Rd files) generated when any package is installed. You can access the Rd BD with pkgRdDB <- tools::Rd_db("eda4treeR"). Later each Rd if converted to HTML (only once) with proper tools function like tools:::example2html("Exam8.1.1", "eda4treeR"). It can be surprising that the base tools package uses knitr dependency in its internals.

    The possible solution requires the update of the tools::example2html function. The run.dontrun argument has to be added and htmltools::browsable is needed to print it. Then you are not printing already rendered files from DB but generating the examples from scratch.

    example2html2 <- function (topic, package, run.dontrun = FALSE, Rhome = "", env = NULL) 
    {
      enhancedHTML <- tools:::config_val_to_logical(Sys.getenv("_R_HELP_ENABLE_ENHANCED_HTML_", 
                                                       "TRUE"))
      if (!enhancedHTML || !requireNamespace("knitr", quietly = TRUE)) {
        utils::example(topic, package = package, character.only = TRUE, 
                       ask = FALSE, run.dontrun = run.dontrun)
        tools:::.code2html_payload_console("example", topic, package, 
                                   enhancedHTML = enhancedHTML, Rhome = Rhome)
      }
      else {
        ecode <- utils::example(topic, package = package, character.only = TRUE, 
                                give.lines = TRUE, run.dontrun = run.dontrun)
        hlines <- grep("^###[ ][^*]", ecode)
        wskip <- which(diff(hlines) != 1)
        if (length(wskip)) 
          hlines <- hlines[seq_len(wskip[1])]
        if (length(hlines)) {
          header.info <- as.list(read.dcf(textConnection(substring(ecode[hlines], 
                                                                   5)))[1, , drop = TRUE])
          ecode <- ecode[-hlines]
        }
        else header.info <- NULL
        tools:::.code2html_payload_browser("example", ecode, topic, package, 
                                   Rhome = Rhome, header.info = header.info, env = env)
      }
    }
    
    browse_example <- function(topic, package, run.dontrun = FALSE) {
      stopifnot(package %in% rownames(installed.packages()))
      stopifnot(is.character(topic))
      stopifnot(is.logical(run.dontrun))
      
      ee <- example2html2(topic, package, run.dontrun = run.dontrun)
      htmltools::browsable(htmltools::HTML(ee$payload))
    }
    
    
    browse_example("Exam8.1.1", "eda4treeR", run.dontrun = FALSE)
    browse_example("Exam8.1.1", "eda4treeR", run.dontrun = TRUE)