14

I want to dump the contents of a text file inside my R markdown (rmd). I tried using the R command: system("cat a.csv"). This command show the file contents in R, but produces no output when I knit the file in R studio.

2
  • Is there a reason why you don't want to read data into a table in R? Alternatively you can use readLines() to show N lines.
    – statespace
    Mar 25, 2015 at 14:19
  • This file is used in a student paper on reproducible analysis. The data from the CSV file is loaded as part of the analysis. I plan on listing the data in the appendix. I want the user to be able to recreate this table with a simply copying the output paste into their version of the file a.csv. This would require that the file be displayed with no formatting. The table in question is used to replacement values in a data.frame. Such as: chevy, car \n ford, car \n cessna, airplane \n. There are around 120 such lines.
    – JerryKur
    Mar 25, 2015 at 17:54

1 Answer 1

28

You can use either

```{r engine='bash', comment=''}
cat a.csv
```

or

```{r comment=''}
cat(readLines('a.csv'), sep = '\n')
```

The former solution requires Bash. The latter one is pure R.

3
  • It is close, but with either command each line of the output is prefixed with '## '. How can get rid of these characters.
    – JerryKur
    Mar 27, 2015 at 0:46
  • 5
    @JerryKur Have you really tried what I wrote above? I think I have indicated comment='' in my answer.
    – Yihui Xie
    Mar 27, 2015 at 1:50
  • 1
    Thanks again. I did not understand that the comment='' was critical.
    – JerryKur
    Mar 27, 2015 at 4:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.