Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Providing documentation for the asis engine #74

Open
lauracion opened this issue May 18, 2018 · 15 comments
Open

Providing documentation for the asis engine #74

lauracion opened this issue May 18, 2018 · 15 comments

Comments

@lauracion
Copy link

lauracion commented May 18, 2018

Another of the projects that came up while discussing #42

@jtr13 wrote: "there's one small piece of the puzzle that I doubt would be hard to implement and would make a big difference. That is, having an echo=FALSE option for text, to provide the same flexibility for text in progress as we have with code in progress. I can think of so many uses: the ability for example to create assignments with and without solutions (... there are workarounds using comments in code chunks but that's not the same)."

@zachary-foster
Copy link

Good idea!

I recently got something like this to work using another kind of workaround. I was using knitr hooks to hide the results of a chunk until a button is pressed:

knitr::knit_hooks$set(
  hide_button = function(before, options, envir) {
    if (is.character(options$hide_button)) {
      button_text = options$hide_button
    } else {
      button_text = "Show solution"
    }
    block_label <- paste0("hide_button", options$label)
    if (before) {
      return(paste0(sep = "\n",
                   '<button class="btn btn-danger" data-toggle="collapse" data-target="#', block_label, '"> ', button_text, ' </button>\n',
                   '<div id="', block_label, '" class="collapse">\n'))
    } else {
      return("</div><br />\n")
    }
  }
}

Then you can add something like this to the Rmd:

```{r hide_button = "Show Answer", results = 'asis', echo = FALSE}
cat(
  "The answer."
)
``'

It would be great if I did not have to use cat and results = 'asis', echo = FALSE. Perhaps there is / could be a "plain text" chunk type. Perhaps something like:

```{text  echo = FALSE}
## Something entirely not thought out

I really would rather people not see this yet.
``'

This would also allow for varaibles to determine which parts of the rmd are shown, like:

```{r  include = FALSE}
show_in_progress = TRUE
``'

bla bla 

```{text  echo = show_in_progress}
## Something entirely not thought out

I really would rather people not see this yet.
``'

@zachary-foster
Copy link

zachary-foster commented May 18, 2018

Adding a knitr engine might work:

https://yihui.name/knitr/demo/engines/

@zachary-foster
Copy link

Oh wait, it already exists, you can use the asis chunk type to put markdown in chunks and use echo = FALSE to not include them. I just tried it and it works.

@lauracion
Copy link
Author

That was a fast resolution! Thank you!

@zachary-foster
Copy link

No problem!

@jtr13
Copy link

jtr13 commented May 18, 2018

Thanks... The problem though with asis is that you still need cat() which is a pain. In addition, with math equations you have to double escape the tex stuff, which is difficult. (See here: https://community.rstudio.com/t/echo-false-type-option-for-rmarkdown-text/2384 -- this has been on my mind for a while!)

I have in mind being able to write markdown paragraphs that are included or not, without having to wrap each line in r code.

@zachary-foster
Copy link

zachary-foster commented May 18, 2018

I dont think you need cat. I meant the chunk engine asis, not the chunk option results = "asis". Like so:


# Show

```{asis  echo = FALSE}
## Something entirely not thought out

I really would rather people not see this yet.
``' # (' instead of ` is for github markdown formatting)

# Dont show

```{asis  echo = FALSE}
## Something entirely not thought out

I really would rather people not see this yet.
``'

@jtr13
Copy link

jtr13 commented May 18, 2018

Aha... brilliant! Where can I apply to get back the time I lost on workarounds??? :-) Also, is that documented anywhere? Not here: https://yihui.name/knitr/demo/engines/

@zachary-foster
Copy link

Where can I apply to get back the time I lost on workarounds???

Haha, there is probably a form you can fill out somewhere.

Also, is that documented anywhere?

Not that I saw. I had to look through the source code to find it:

https://github.com/yihui/knitr/blob/dc028f4c9698f84999b53edc5f6f255b29d7e5a2/R/engine.R#L390-L392

@jtr13
Copy link

jtr13 commented May 18, 2018

Ok, so I hereby change this issue to providing documentation for the asis engine!

@apreshill
Copy link

A related tool I was just made aware of from issue #63 is assignr: https://github.com/coatless/assignr

I have not used, but looks promising @jtr13!

@lauracion lauracion changed the title Adding option echo = FALSE for text to R Markdown Providing documentation for the asis engine May 19, 2018
@lauracion
Copy link
Author

Summary: engine asis in knitr can be used to easily exclude from the final report chunks of text that are still in progress. asis needs to be documented.

@maurolepore
Copy link
Member

Thank you @zachary-foster ! It'll be cool to tweet this on #rstats.

image

@yihui
Copy link

yihui commented Jun 5, 2018

It was only documented in two places: 1) the source code in knitr:::eng_asis (in the spirit of "Luke, use the source"); and 2) the knitr book, which is unfortunately not freely available like my other books, but you can still see the relevant text from Google Books: https://books.google.com/books?id=fyIuDwAAQBAJ&lpg=PP1&dq=dynamic%20documents%20with%20r%20and%20knitr&pg=PT145#v=onepage&q=asis%20engine&f=false

There are still many other features of knitr that are not publicly documented, and I plan to address this problem later this year by writing another book that will be free to read.

@lauracion
Copy link
Author

😍 Thank you, @yihui!!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants