How to Report Final Model First and place all else in Appendix

I am trying to knitr my document/report in a non-standard way? (Maybe?)
I run the analysis chronologically (1,2,3, model, result, end).
However, I would like to REPORT the Logistic regression first and all else as an appendix.

For example: Complete order is:

  1. run and cache analysis (1,2,3, model, result, end), Works well!
  2. switch the order (model, result, 1,2,3, end)
  3. knit report, When I knit report at step #3 I get the error:
Quitting from lines 24-29 (2021-12-02-heart-disease-logistic-regression-report.rmd) 
Error in eval(expr, p) : object 'training_set' not found
Calls: <Anonymous> ... train -> train.formula -> eval.parent -> eval -> eval
Execution halted

Example code:

Appendix 5 - Create Data Partition & Setup Cross-Validation

# Partition data into training and testing sets
set.seed(1000)
index <- createDataPartition(df_clean$TenYearCHD, p = 0.8, list = FALSE)
training_set <- df_clean[index, ]

# Create model, 10X fold CV repeated 5X
tcontrol <- trainControl(method = "repeatedcv",
                         number = 10,
                         repeats = 5)

Model and result code chunk

model_obj <- train(TenYearCHD ~ ., # line 24...
                   data = training_set,
                   trControl = tcontrol,
                   method = "glm",
                   family = "binomial")

summary(model_obj)                          # line 29
```

Can you not just hide all the code chunks prior to your model and result code chunk (as well as any output, messages, etc. produced in prior chunks)? You can then reference the code chunks hidden in the main part of the report in the appendix to have them show up there.

I did not try that. I will now.

But then again that is a lot of copy-paste...

No need to copy-paste; you can reuse code chunks elsewhere in the document. For more details on this, check 14.1 Reuse code chunks | R Markdown Cookbook (especially section 14.1.2); basically, you give your code chunks labels, and then you can reference these in new code chunks by providing the relevant label in a new chunk later in the document (and showing the contents by changing to echo = TRUE.)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.