Home Comments Thread
New Thread

3 Comments

giscus-bot giscus-bot 2022-12-17 03:59:05
Guest *TC Zhang* @ 2018-09-19 00:07:16 originally posted:

"...when you don’t want to distract your readers with program code ...An appendix will be the natural place to go for these code." In my case, code_folding: hide is more natural.

And that cool hacker series sound particularly interesting, they need to happen.

yihui yihui 2022-12-17 03:59:06

Alex was talking about generating PDFs for his homework assignments, so code folding won't work. If the output format can be HTML, you are certainly right that folding the code chunks will be nicer.

Originally posted on 2018-09-19 02:47:14

giscus-bot giscus-bot 2022-12-17 03:59:06
Guest *Alex Hayes* @ 2018-09-19 21:42:05 originally posted:

Learned a ton from this post and updated the template I've been using!

yihui yihui 2022-12-17 03:59:07

Glad to know the post was useful to you! And of course, thank you for sharing the original trick, without which I wouldn't start writing this post.

Originally posted on 2018-09-19 21:51:21

QuantAkt QuantAkt 2024-01-08 09:27:24

Looks great, but this does not seem to work, if you would like the listing of a Python script? I tried the above commands, simply replacing where applicable ".Rmd" with the relevant ".py" but neither approach produces a listing. Just empty space. Shouldn't this be literal quoting, or is there some interpretation of the code involved?

yihui yihui 2024-01-08 16:58:03

I'm not sure what you tried exactly. It will be easier for me to help if you could provide a minimal reproducible example. Here is mine:

---
title: Python code in appendix
---

```{r, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{python, chunk-a}
1 + 1
```

```{python, chunk-b}
2 + 2
```

```{python, ref.label=knitr::all_labels(engine == 'python'), echo = T, eval = F}
```
QuantAkt QuantAkt 2024-01-10 08:28:40

Of course, you are right. Here are my attempts. I did not include the additional file "boring_function.py" but its contents should be clear?

---
title: "inclusiveexample"
---
# define stuff
```{r}
library(reticulate)
```

```{r}
source_python("boring_function.py")

# Boring function is defined as
# def boring_function(x):
#    return(x + 1)

```

# do stuff
```{python}
tmp = boring_function(1)
print(tmp)
```

# Appendix

For completeness' sake all boring functions should be listed here.
Ideally the listing should be formatted exactly like this "native" definition:
```{python fundef}
def boring_function(x):
    return(x + 1)
```

Does nothing
```{python code = readLines(knitr::purl("boring_function.py", documentation = 1)), echo = T, eval = F}

```

Also does nothing
```{python, child="boring_function.py", eval=F}

```

Does something but not the right thing respectively not for the right reasons
```{python, ref.label=knitr::all_labels(engine == 'python'), echo = T, eval = F}

```

This is the best I could manage this far. But the formatting is off.
```{r, echo=FALSE, results='asis'}
res <- knitr::knit_child('boring_function.py', quiet = TRUE)
cat(res, sep = '\n')
```

QuantAkt QuantAkt 2024-01-10 08:34:52

Just to be on the safe side:

I want to include the listing of an external python file within an Rmarkdown document. This listing should be formatted in the same way as a "native" definition, i.e. a definition made within the Rmarkdown doc, not in an external file which is sourced.

yihui yihui 2024-01-10 14:51:32

I think this should be what you were looking for:

First, list the python file:

```{python, file='boring_function.py', echo=T, eval=F}
```

Then list the rest of python code chunks:

```{python, ref.label=knitr::all_labels(engine == 'python'), echo=T, eval=F}
```
QuantAkt QuantAkt 2024-01-11 08:02:36

That easy! Great!!

Sign in to join the discussion

Sign in with GitHub