Table in R Markdown Chunck

I have two question for r Markdown

1- I am struggling to find a way to create a table in R Markdown to insert some data manually (data not generated in R)

2- Is it possible to show the R code in a chunk without running when knitting the file in PDF? I just want to show it, in fact, if there is a way to run the code but append in appendix that would even be better. So code is not show at the chunk where it has run instead it’s at the end of the file. I hope this make sense.

Many Thanks in advance

Hi @user124578,

Here are some of my thoughts:

  1. I don't know of a pre-set tabular system for data input, but you can certainly collect data using shiny inputs in a shiny app.

  2. To your first point, yes you can add the code chunk options to not evaluate the code, but rather just present it

```{r eval = FALSE, echo = TRUE}
# Here is my code

The second is also achievable. You can have ALL or just some of your code appear in an appendix at the end of the R Markdown file.

output: pdf_document

# Here is my first section

```{r chunk1, echo = FALSE}

# Here is section 2

```{r chunk2, echo = FALSE}

# Code Appendix

```{r ref.label=knitr::all_labels(), echo = TRUE, eval = FALSE}


Many Thanks for this. My first question was about adding static data to an r Markdown file. Sorry this is what I meant. I.e. if i had a table of A=1, B=2...etc. How to create this in a table form. Thanks for your help.

When i specfied a a certain chunk do i need to have {r chunk 1} or just use the defaukt chunk name. Thanks

Hi @user124578,

To your first point, you can output a table in a R Markdown document in several ways. There are many packages that support nice table formatting. The knitr package provides kable. You can add a table like this:

data.frame(a = 1:10, b = LETTERS[1:10]) %>%
  knitr::kable(format = 'latex')

To your second question, you don't need a chunk name, you can just use the default chunk names. However, naming chunks is good practice as it makes it easier to navigate the document, and you can filter chunk labels to only include certain chunks in the code appendix. Having a naming system makes this much easier.

If i have

{r chunk 1}
a = b +a

If i want to call this chunk in the function do i just use this code

{r ref.label=knitr::chunk1, echo = TRUE, eval = FALSE}

that doesn't seem to work.

I am not sure exactly what you mean by "call this chunk in the function". Please clarify.

If you want to run chunk1 (and hide the code), and then show the code for chunk1 in another location in the document, you can do it as follows:

```{r chunk1, echo=FALSE}
a = b + a

```{r ref.label=chunk1, echo=TRUE, eval=FALSE}

If i do this i get error in eval(x,envir=envir)object 'chunk90' not found cals

Sorry I forgot the quotations:

```{r chunk1, echo=FALSE}
a = b + a

```{r ref.label="chunk1", echo=TRUE, eval=FALSE}

thanks that worked! Is there away to give it a number so its easy to refer to it in my writing. like saying appendix 1

My recommendation would be to create an H-level markdown header with a label, and then refer to that label for automatic cross-referencing, try this:

title: "My awesome report"
author: "Me"
    toc: false
    number_sections: false
linkcolor: blue

# Section

Here is a cool histogram, you can find the code for it [here](#appendix). 
The code for this section is found in the [Code Appendix].

```{r chunk1, echo=FALSE}
# Here is some sweet code

# Code Appendix {#appendix}

```{r ref.label="chunk1", echo=TRUE, eval=FALSE}

We labeled the Code Appendix section with a label of #appendix. We can use that to cross-reference that section in our text. You can do that with normal markdown-style linking like: [text shown](#label). You may also just put the full and exact section name in square brackets like: [Code Appendix]. All work.

1 Like

Great. Thanks very much for your help.

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