Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Before_chapter_script doesn't work? #36

Closed
hadley opened this issue Mar 4, 2016 · 11 comments
Closed

Before_chapter_script doesn't work? #36

hadley opened this issue Mar 4, 2016 · 11 comments
Milestone

Comments

@hadley
Copy link
Member

hadley commented Mar 4, 2016

In _bookdown.yaml I have:

before_chapter_script: "_common.R"

and in _common.R I have:

knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  cache = TRUE
)

but the chunk options don't seem to have any affect. What am I missing?

@yihui yihui closed this as completed in cfde0e8 Mar 4, 2016
@yihui
Copy link
Member

yihui commented Mar 4, 2016

You didn't miss anything. I just didn't implement it for the case new_session = TRUE. Done now.

@hadley
Copy link
Member Author

hadley commented Mar 4, 2016

After this change I'm getting:

Error in insert_chapter_script(config, "before") : 
  object 'config' not found
Calls: <Anonymous> -> render_new_session -> insert_chapter_script
Execution halted

@yihui
Copy link
Member

yihui commented Mar 4, 2016

Ah, fixed now

@yihui yihui modified the milestone: v0.1 Jul 12, 2016
@dereckmezquita
Copy link

dereckmezquita commented Oct 18, 2020

I know this is an old post; but I'm having this same issue. My before chapter script is not executing. I have the following set up:

  • _bookdown.yml
new_session: yes
language:
  ui:
    chapter_name: "Chapter "
delete_merged_file: true
before_chapter_script: "_common.R"
  • _common.R
knitr::opts_chunk$set(echo = TRUE, fig.width = 7, fig.height = 7, fig.align = "center")

I've tried everything to fix it to no avail. I would appreciate any help you may extend.

Might it possibly be the fact that I am using a "new_session: yes" for a K-M build mode? I saw that it was an issue when "new_session = TRUE"? I tried setting to TRUE/true in the yml and nothing.

@cderv
Copy link
Collaborator

cderv commented Oct 19, 2020

Hi @dereckdemezquita,

I can't reproduced. This is working fine for me using the demo book.
I added a _common.R file with

knitr::opts_chunk$set(echo = FALSE)

and after building the book, all chunk source are correctly not shown.

Can you try that too ?

Also can you ask on the RStudio community or in a new issue, instead of an old issue ? It will be easier to continue discussion. Thank you.

@hadley
Copy link
Member Author

hadley commented Oct 19, 2020

@dereckdemezquita I don't recommend using before_chapter_script — because it literally inserts that code into your .Rmd file, it messes up line numbers making debugging very painful. It's easier to just copy and paste a standard block at the front of every chapter.

@cderv
Copy link
Collaborator

cderv commented Oct 19, 2020

it literally inserts that code into your .Rmd file

I think this will only do that if new_session: no, i.e Merge and Knit approach. For new_session: yes, i.e Knit and Merge approach, it will the source the script before calling render() on each file.

It's easier to just copy and paste a standard block at the front of every chapter.

If one wants to follow this way, I would advice to use a child document, or import the _common.R file as chunk content to avoid copy pasting the same pieces of code and keep it all in the same place. Example:

```{r, include=FALSE, echo=FALSE, code = xfun::read_utf8('_common.R')}
```

or

```{r, child="_common.Rmd"}
```

it messes up line numbers making debugging very painful.

Easier debug of Rmarkdown project seems a topic to invest in.

@yihui
Copy link
Member

yihui commented Oct 19, 2020

@hadley That's no longer true, and @cderv was correct. The scripts will be source()d instead of being inserted into Rmd. This change was made in bookdown 0.18: https://github.com/rstudio/bookdown/releases/tag/v0.18.

Re: debugging, the issue is tracked in #620. That's something I definitely want to improve, and I'm pretty sure it can be improved (e.g. when an error occurs, it can automatically navigate to the relevant lines in the original Rmd file).

@hadley
Copy link
Member Author

hadley commented Oct 19, 2020

@yihui I didn't know that because you don't blog about it 😄 Regardless, I've now been burned by that behaviour, so I'm unlikely to use before_chapter_script again in the future.

@yihui
Copy link
Member

yihui commented Oct 19, 2020

@hadley Sorry, but I didn't know you were burned. Claus and Bob reported that this issue bothered them, so I made the change for them, but I had no idea how many more users were burned...

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants