How can I prevent R Markdown from turning tabs to spaces in code blocks

I am creating a website using blogdown and I have code blocks in it. The R Markdown files are outputted to Markdown which is then interpreted by Hugo. By default, R Markdown converts tab characters in code blocks to four spaces. I do not want that. Is there a way to prevent this from happening?t

RStudio menu bar Tools | Global Options | Code | Editing | uncheck first box

Is it possible to do this with just R?

Ah. The only way I know to do that is with

cat(paste0(names(iris), "_"), sep = "\t")

as an example

I tried this:

```{r, echo = FALSE}
cat(```shell
lsblk
	sudo wipefs -a /dev/<NAME>
	sudo cfdisk /dev/<NAME>
```, sep = "\t")
```

But it gave me a zero-length variable name error.

Stop. Never use sudo for ANYTHING in an R script. You can brick your OS doing that.

What I meant to do is use that as the code block. I am writing a code block in an R Markdown document and I do not want the tabs in it to become spaces. Let's say I write a code block in R Markdown:

```shell
lsblk |
    grep sda1
```

Where there is a tab character before grep sda1, I do not want that tab character to be replaced with four spaces when in the final output. By default, when the R Markdown file is converted to a Markdown file, the tabs will become four spaces in the Markdown file. I do not want that.

I don't know of any setting within rmarkdown to do that. If I had a compelling need for tab-based whitespace and depending on how much/often I would

  1. Script replace four spaces with &Tab;
  2. Do some custom CSS or
  3. Write a Lua filter for pandoc
1 Like

I am not sure how to use custom CSS to achieve this. And in blogdown, I output to Markdown and then use the Markdown interpreter Goldmark, so Pandoc is not used. I think writing a script to replace four spaces with &Tab; is the best workaround for now. I will also open an issue in the R Markdown GitHub repository which references this topic. Thanks for your help!

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.