0

I am creating a multi-section PDF document using R markdown, knitr, and pander. Though I have read previous posts [blog]: Programmatically insert text, headers and lists with R markdown "posts" on this topic, I am still encountering problems.

The function I'm using to insert headers is:

I can only provide a skeletal version of the code, which is actually several hundred lines long:

{r , echo=FALSE, include = TRUE, warning = FALSE, results = 'asis'}
    prt.scale.heading <- function(scale, iter) {
       txt <- "Analysis of "
       # add blank lines as suggested in previous post     
      cat("\n\n") 
      hdr <- paste(txt,scale,"Iteration",iter)
      # show header using the R markdown '#' symbol
       cat('\n#',hdr,'\n')
    }

do_analysis <- function(resp.data, scale, maxChanges = 1, 
                    filename = NULL)  {

    iter <- 0
    while (iter <= maxChanges) {
        prt.scale.heading(scale, iter)
        iter <- iter + 1
    }
}

do_analysis(resp.data = temp, scale.name, maxChanges = 5)
}

When I run my code, instead of seeing formatted section headers, I see the heading in plain text preceded by '#'. Note that all of the text, plots and tables (pander) are placed in one large chunk, and I set the chunk option results = 'asis' (as suggested in a previous post).

Suggestions?

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
Barth
  • 1
  • 2
  • Please show a short example of how you are using this in a chunk. A short, fully reproducible document would be even better. – Gregor Thomas Jan 02 '18 at 18:18
  • I can only provide a skeletal version of the code, which is actually several hundred lines long: – Barth Jan 02 '18 at 18:44
  • Perfect! That's why I used *"short"* twice in my request. We really like **minimal** examples here. If you can demonstrate the problem in less than 20 lines it's great - much easier to debug. – Gregor Thomas Jan 02 '18 at 18:48
  • I can only provide a skeletal version of the code, which is actually several hundred lines long: ```{r , echo=FALSE, include = TRUE, warning = FALSE, results = 'asis'} do_analysis <- function(resp.data, scale, maxChanges = 1, filename = NULL) { iter <- 0 while (iter <= maxChanges) { prt.scale.heading(scale, iter) iter <- iter + 1 } } do_analysis(resp.data = temp, scale.name, maxChanges = 5) ``` } – Barth Jan 02 '18 at 19:06
  • ```{r , echo=FALSE, include = TRUE, results = 'asis'} do_analysis <- function(resp.data, scale, maxChanges = 1, filename = NULL) { iter <- 0 while (iter <= maxChanges) { prt.scale.heading(scale, iter) iter <- iter + 1 } } do_analysis(resp.data = temp, scale.name, maxChanges = 5) ``` } – Barth Jan 02 '18 at 19:08
  • 1
    You'll make it much easier for us to help you if you paste into the body of your question a complete rmarkdown document that includes the code chunk in your comment and any other code necessary to create a complete, self-contained example that sets up all the important components of your issue and that we can work from to find a solution. – eipi10 Jan 02 '18 at 19:09
  • And do please edit it into your question - it's difficult to read multiline code in comments. – Gregor Thomas Jan 02 '18 at 19:12
  • Edits are looking better - but please make sure it's copy/paste/run-able. I pasted the code you posted into a fresh RMD document and I can't run it because `scale.name` and `temp` aren't defined. – Gregor Thomas Jan 02 '18 at 19:33
  • I added the code to the original post – Barth Jan 02 '18 at 19:39
  • Please provide sample values fro `temp` and `scale.name` so it is runnable. – Gregor Thomas Jan 02 '18 at 19:43
  • I have been trying to create a simple example of the issue. Unfortunately, the simple example does not reproduce the error. – Barth Jan 03 '18 at 18:45

0 Answers0