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

[FR] preserve spacing between source code lines in a code block #2220

Closed
IndrajeetPatil opened this issue Sep 20, 2021 · 11 comments
Closed

[FR] preserve spacing between source code lines in a code block #2220

IndrajeetPatil opened this issue Sep 20, 2021 · 11 comments
Labels
question general questions - not an issue

Comments

@IndrajeetPatil
Copy link

Description of the issue

When all the source and output blocks from one code chunk are collapsed into a single block, any spaces that were deliberately left to separate different code lines are not preserved in the rendered document.

Source code

```{r, collapse=TRUE}
x <- 1
y <- 2

# addition
x + y

# subtraction
x - y

# multiplication
x * y
```

Current output

x <- 1
y <- 2

# addition
x + y
#> [1] 3
# subtraction
x - y
#> [1] -1
# multiplication
x * y
#> [1] 2

Expected output

x <- 1
y <- 2

# addition
x + y
#> [1] 3

# subtraction
x - y
#> [1] -1

# multiplication
x * y
#> [1] 2
@yihui yihui added the question general questions - not an issue label Sep 20, 2021
@yihui
Copy link
Member

yihui commented Sep 20, 2021

You can use the chunk option strip.white = FALSE (https://yihui.org/knitr/options/). Next time you may ask this type of question on Stack Overflow or RStudio Community. Thanks!

@yihui yihui closed this as completed Sep 20, 2021
@IndrajeetPatil
Copy link
Author

Apologies for violating the issue guidelines.

I am surprised I missed the existence of this chunk option when I've read that webpage so many times by now. 😓

@yihui
Copy link
Member

yihui commented Sep 20, 2021

No worries! That's very normal. I miss information under my nose sometimes, too.

@cderv
Copy link
Collaborator

cderv commented Sep 20, 2021

I think you may have missed it also because this is a side effect of a change in knitr 1.34 which was just released to CRAN https://github.com/yihui/knitr/releases/tag/v1.34

The chunk option strip.white = TRUE used to work only when the chunk option collapse = FALSE. Now the two options are independent, i.e., strip.white also works when collapse = TRUE (thanks, @kbvernon, #2011).

  • strip.white = TRUE is the default in knitr
  • Previously, it had no impact on chunk with collapse = TRUE. This means chunks where not stripped before collapsing.
  • Now it has - which I believe create the new behavior that requires strip.white = FALSE if you don't want it.

This could probably be qualified as a breaking change and we should probably have communicated this way in our NEWS file to indicated how to put back previous behavior. 😅

Example below:

Using version 1.33

# renv::install("[email protected]")
packageVersion("knitr")
#> [1] '1.33'
content <- c(
  "```{r, collapse=TRUE}",
  "x <- 1", 
  "y <- 2", 
  "", 
  "# addition", 
  "x + y",
  "",
  "# subtraction",
  "x - y"
)
xfun::raw_string(content)
#> ```{r, collapse=TRUE}
#> x <- 1
#> y <- 2
#> 
#> # addition
#> x + y
#> 
#> # subtraction
#> x - y
res <- knitr::knit(text = content)
xfun::raw_string(res)
#> 
#> ```r
#> x <- 1
#> y <- 2
#> 
#> # addition
#> x + y
#> #> [1] 3
#> 
#> # subtraction
#> x - y
#> #> [1] -1
#> ```

Using 1.34

# renv::install("knitr")
packageVersion("knitr")
#> [1] '1.34'
content <- c(
  "```{r, collapse=TRUE}",
  "x <- 1", 
  "y <- 2", 
  "", 
  "# addition", 
  "x + y",
  "",
  "# subtraction",
  "x - y"
)
xfun::raw_string(content)
#> ```{r, collapse=TRUE}
#> x <- 1
#> y <- 2
#> 
#> # addition
#> x + y
#> 
#> # subtraction
#> x - y
res <- knitr::knit(text = content)
xfun::raw_string(res)
#> 
#> ```r
#> x <- 1
#> y <- 2
#> 
#> # addition
#> x + y
#> #> [1] 3
#> # subtraction
#> x - y
#> #> [1] -1
#> ```

@IndrajeetPatil
Copy link
Author

Yes, please, let's go with this explanation for why I forgot about that option 😅

@yihui
Copy link
Member

yihui commented Sep 20, 2021

I suspected in yihui/knitr#2011 that this might be a breaking change, but I didn't realize a breaking case.

At this point, I feel strip.white = FALSE may be a better default for the case of collapse = TRUE.

@cderv
Copy link
Collaborator

cderv commented Sep 21, 2021

Yes I missed it too. I also think strip.white = FALSE may be a better default for collapse = TRUE

@cderv
Copy link
Collaborator

cderv commented Sep 21, 2021

I tried to tackle this but did not find exactly where to apply a modification.
Do we have a way to detect if an option is currently default value or set on a chunk by the user ? Because unless we revert, I think here we want to change the default strip.white value when collapse = TRUE on a chunk or a globally, unless specify by the user in which case we want to keep stip.white = FALSE.

Is this done by a knitr hook ? Is there another mechanism ?

I created a knitr issue to follow this change

@cderv
Copy link
Collaborator

cderv commented Sep 22, 2021

@IndrajeetPatil we just merged in knitr a change to fix this regression. Thanks for the report!

@IndrajeetPatil
Copy link
Author

Wonderful! Glad that the issue was helpful. :)

@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 Mar 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question general questions - not an issue
Projects
None yet
Development

No branches or pull requests

3 participants