mlr-archive / mlr-tutorial

The mlr package online tutorial
http://mlr-org.github.io/mlr/
20 stars 11 forks source link

Take all code in each tutorial and place it in the bottom #99

Closed SteveBronder closed 6 years ago

SteveBronder commented 7 years ago

Related to #56 . A compromise for 'quick and dirty examples' may be to paste all the code for a given tutorial page at the bottom of the tutorial. So someone can just scroll to the bottom and see how everything comes together without being interrupted by pesky words

jakob-r commented 7 years ago

I could imagine that this could be done automatically. Maybe generate an R file and link to that.

SteveBronder commented 7 years ago

Do you have any links I can read to how the backend for all of this works? I could do this next week.

jakob-r commented 7 years ago

Well there seems to be the function purl you can call on an Rmd file and there is this way to reuse chunks: https://yihui.name/knitr/demo/reference/ Unfortunately to my knowledge makedocs (what we use) does not have an easily modifiable Makefile that we can edit. So I guess my favourite option at the moment would be to just use the possibility to reuse chunks manually and edit the tutorial pages where it makes the most sense. E.g. add a a section Complete Code and then just reference the important chunks there.

berndbischl commented 7 years ago

t. So I guess my favourite option at the moment would be to just use the possibility to reuse chunks manually

lets NOT do this. this will become unmaintainable.

we have travis, right? so you can execute any shell script you want, or R code? before the tutorial is built?

berndbischl commented 7 years ago

i mean, look in travis.yml

install:
  - pip install --user mkdocs
  - pip install --user python-markdown-math
  - Rscript r_packs_install.R

script:
  - ./build

pretty arbitrary scripts are executed. so where is the problem to add another one?

SteveBronder commented 7 years ago

From this stackoverflow page it seems like we may just be able to have a code block at the end of the file that says

```{r test, purl=FALSE}
purl(file_name)
\```

The purl=FALSE is so that we do not end up with recursion

berndbischl commented 7 years ago

From this stackoverflow page it seems like we may just be able to have a code block at the end of the file that says

well that would be just swell

SteveBronder commented 7 years ago

Well golly gee I'll mess around with this today

berndbischl commented 7 years ago

hunky dory

SteveBronder commented 7 years ago

While I'm doing this, it sure would be swell if someone went and reviewed the forecasting base PR

SteveBronder commented 7 years ago

So we can do this by adding the following lines (ignoring the \ because idk how to use `` and have it render here without it) to each .Rmd file. The example below is foradvanced_tune.Rmd`

## Full Code:
```{r, purl=FALSE, echo = FALSE, results='hide'}
knitr::purl("./src/advanced_tune.Rmd", "./full_code_src/advanced_tune.R",
  documentation = 0, quiet = TRUE)
\```

```{r, purl=FALSE, echo = FALSE, comment=NA}
cat(readLines("./full_code_src/advanced_tune.R"),fill = 2)
\```

The first chunk creates a .R file to a new folder full_code_src while the second chunk prints all the code to the file. Note that since the second one is a print knitr does not actually redo all the code twice which is nice.

Before I do this for everything I wanted input on how it should look. Here is a rawgit link to render the example for advanced_tune.

SteveBronder commented 7 years ago

@schiffner thoughts?

PhilippPro commented 7 years ago

I have an idea. Show the code only if it is wished by the user. You can use the code_folding option in rmarkdown.

Here a minimal example (sorry for the > but otherwise it gets not readable here):

> ---
> title: "test file"
> author: "dayne"
> date: "June 10, 2016"
> output: 
>   html_document:
>     code_folding: hide
> ---
> ```{r setup, include=FALSE}
> knitr::opts_chunk$set(echo = TRUE)
> ```
> Here is a basic example.
> 
> ```{r}
> 3 + 4
> ```
SteveBronder commented 7 years ago

Will the code folding option work for just the bottom part? So everything else will be unfolded

schiffner commented 7 years ago

@Stevo15025 : Thanks for your work on this issue and sorry for no commenting earlier. I was away and crazy busy the last two weeks, but I'm trying to catch up with mlr now.

SteveBronder commented 7 years ago

Thanks for your work on this issue and sorry for no commenting earlier.

No problem and no worries!

I personally would like that we provide (links to) the R-files for each tutorial page, mainly because one can easily download them and start to play around.

Sure I believe this would be quite easy since we save the R files in a separate folder

For better orientation if the R code gets longer, it might be nice to keep the chunk headers and separators (i.e. set documentation = 1). We don't have meaningful chunk headers at the moment, but I would be willing to work on that.

Yes I can also do this as I go through. The code does look a bit odd being all mushed together

I like what you did to show the code and how it looks. We can add the call to knit::purl directly to the build file and I think it should be also possible to append the caption (## Full Code) and the second code chunk automatically to each Rmd file there.

This would also be easier to maintain. I'll look into how to do this, though if I can't figure it out I may just go the lazy way and and it to the bottom of each file (since it's like two lines of code)

About the unfolding option: It's certainly nice, but: We use knitr::knit for Rmd -> md and then mkdocs for md -> html. So it's not straightforward to use this rmarkdown option.

I think that is something we can add later if we really want to. I personally do not want to go through adding custom js

SteveBronder commented 7 years ago

See #111