Writing paper with book down, can I display a table elsewhere in the text than where it was created?

Hi,

I'm writing a paper with bookdown (or at least trying to-its new to me!)
I have a stats chapter where I perform the stats, and another chapter where I discuss the results. Is there a way for me to display the table in the results chapter which I made in the stats chapter? The workaround I've been using is to have a chunk that calls the table I created before...is this the best way?

Thanks!

If you have a code chunk in a previous chapter that builds the table, you can also just refer to that chunk directly using a ref.label option in a chunk. So assuming you have labeled the original chunk that created the table in an earlier chapter, and let's say you called it create-table

```{r ref.label="create-table", echo=FALSE}
```

This isn't much different than your current approach but it might be a bit safer, in the case where you might accidentally overwrite the table object in some intermediate code.

Worth noting, this will only work if you use the default bookdown rendering approach of "merge-and-knit", rather than "knit-and-merge".

3 Likes

Brilliant, this is really helpful! And the safer approach you suggested is great. Can I ask how to check I'm using the default MandK?

Many thanks

Oh and, sorry 1 more: if there are 2 tables in the same chunk, say 'create-table' chunk, can I specify which to show later on when I reference it? Or should I separate out the chunks?

Thanks again!

  1. You will be using M-K by default unless you have set the new_session: yes in the _bookdown.yml file.

  2. Basically, when you use ref.label, the code from the previous code chunk will be "copy and pasted" into the new code chunk and will run the entire chunk (assuming eval isn't FALSE). So it will run everything and it might be wise to split the tables into separate chunks if that is what you need.

1 Like

I cannot thank you enough for your patience! Thank you, this has been a great help

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.