RMarkdown:将代码包装成块,但在管道后保持中断
RMarkdown: Wrap code in chunks but keep breaks after pipe
我正在尝试实现可能不兼容的两件事:我希望在我创建 PDF 时,我的 RMarkdown 块中的代码能够在每一行中环绕(换句话说,该行低于页面边缘 运行 秒)。
稍微阅读了一下(包括 here and attempting the styler
package 后,我发现使用 tidy=TRUE
和 tidy.opts = list(width.cutoff=60)
有效(见下文)
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
注意:您可能需要安装 formatR
。
但遗憾的是,这破坏了带有管道的代码格式 %>%
保持管道的格式并让代码 运行 离开页面。
有没有办法正确地做到这一点?以便文本保留在页面上并且结构在一定程度上保持原样?
感谢您的帮助!
返工:
Header 两个:
---
title: "R Notebook"
output: pdf_document
---
为第二张图片选择区块
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
两者的示例代码:
data(mtcars)
library(tidyverse)
variable_1 <- 10
variable_2 <- 50
variable_3 <- 30
variable_4 <- 5
variable_5 <- 100
variable_6 <- 25
variable_7 <- 600
mtcars %>%
mutate(mpg = ifelse(mpg>18,
yes = (variable_1*variable_2)*variable_3 + variable_4 + variable_5 + variable_6 + variable_7,
no = mpg)) %>%
select(mpg,disp) %>%
group_by(mpg) %>%
summarise(mean(mpg)) %>%
ungroup -> df
我还为软件包版本等添加 SessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.0 purrr_0.3.4 readr_1.3.1 tidyr_1.0.2 tibble_3.0.0
[8] ggplot2_3.3.0 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 xfun_0.15 haven_2.2.0 lattice_0.20-41 colorspace_1.4-1 vctrs_0.3.0
[7] generics_0.0.2 htmltools_0.4.0 yaml_2.2.1 rlang_0.4.6 pillar_1.4.3 glue_1.4.1
[13] withr_2.2.0 DBI_1.1.0 dbplyr_1.4.3 modelr_0.1.6 readxl_1.3.1 lifecycle_0.2.0
[19] munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 rvest_0.3.5 evaluate_0.14 knitr_1.28
[25] fansi_0.4.1 broom_0.5.6 Rcpp_1.0.4.6 backports_1.1.6 scales_1.1.0 jsonlite_1.6.1
[31] fs_1.4.1 hms_0.5.3 packrat_0.5.0 digest_0.6.25 stringi_1.4.6 grid_4.0.0
[37] cli_2.0.2 tools_4.0.0 magrittr_1.5 crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.0
[43] xml2_1.3.1 reprex_0.3.0 lubridate_1.7.8 assertthat_0.2.1 rmarkdown_2.3 httr_1.4.1
[49] rstudioapi_0.11 R6_2.4.1 nlme_3.1-147 compiler_4.0.0
虽然我不确定下图是否反映了你想要的,但你可以按如下方式换行:
您可以通过 LaTeX 的 fvextra 包控制 PDF 文件中代码的外观。您可以通过 header-includes
:
调用它并配置它的设置
header-includes:
- |
```{=latex}
\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
showspaces = false,
showtabs = false,
breaklines,
commandchars=\\{\}
}
```
如果你想去除换行中的箭头符号,请添加breaksymbolleft={}
(另请参阅the manual中对breaksymbolleft
的描述):
header-includes:
- |
```{=latex}
\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
breaksymbolleft={},
showspaces = false,
showtabs = false,
breaklines,
commandchars=\\{\}
}
```
这都可以与 tidy = "stlyer"
选项结合使用以同时具有两种功能(断行和使用整齐的样式)。
MWE
---
title: "R Notebook"
output: pdf_document
header-includes:
- |
```{=latex}
\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
breaksymbolleft={},
showspaces = false,
showtabs = false,
breaklines,
commandchars=\\{\}
}
```
---
```{r}
knitr::opts_chunk$set(tidy="styler")
```
```{r}
data(mtcars)
library(tidyverse)
variable_1 <- 10
variable_2 <- 50
variable_3 <- 30
variable_4 <- 5
variable_5 <- 100
variable_6 <- 25
variable_7 <- 600
mtcars %>%
mutate(mpg = ifelse(mpg>18,
yes = (variable_1*variable_2)*variable_3 + variable_4 + variable_5 + variable_6 + variable_7,
no = mpg)) %>%
select(mpg,disp) %>%
group_by(mpg) %>%
summarise(mean(mpg)) %>%
ungroup -> df
```
我正在尝试实现可能不兼容的两件事:我希望在我创建 PDF 时,我的 RMarkdown 块中的代码能够在每一行中环绕(换句话说,该行低于页面边缘 运行 秒)。
稍微阅读了一下(包括 here and attempting the styler
package 后,我发现使用 tidy=TRUE
和 tidy.opts = list(width.cutoff=60)
有效(见下文)
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
注意:您可能需要安装 formatR
。
但遗憾的是,这破坏了带有管道的代码格式 %>%
保持管道的格式并让代码 运行 离开页面。
有没有办法正确地做到这一点?以便文本保留在页面上并且结构在一定程度上保持原样?
感谢您的帮助!
返工:
Header 两个:
---
title: "R Notebook"
output: pdf_document
---
为第二张图片选择区块
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
两者的示例代码:
data(mtcars)
library(tidyverse)
variable_1 <- 10
variable_2 <- 50
variable_3 <- 30
variable_4 <- 5
variable_5 <- 100
variable_6 <- 25
variable_7 <- 600
mtcars %>%
mutate(mpg = ifelse(mpg>18,
yes = (variable_1*variable_2)*variable_3 + variable_4 + variable_5 + variable_6 + variable_7,
no = mpg)) %>%
select(mpg,disp) %>%
group_by(mpg) %>%
summarise(mean(mpg)) %>%
ungroup -> df
我还为软件包版本等添加 SessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.0 purrr_0.3.4 readr_1.3.1 tidyr_1.0.2 tibble_3.0.0
[8] ggplot2_3.3.0 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 xfun_0.15 haven_2.2.0 lattice_0.20-41 colorspace_1.4-1 vctrs_0.3.0
[7] generics_0.0.2 htmltools_0.4.0 yaml_2.2.1 rlang_0.4.6 pillar_1.4.3 glue_1.4.1
[13] withr_2.2.0 DBI_1.1.0 dbplyr_1.4.3 modelr_0.1.6 readxl_1.3.1 lifecycle_0.2.0
[19] munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 rvest_0.3.5 evaluate_0.14 knitr_1.28
[25] fansi_0.4.1 broom_0.5.6 Rcpp_1.0.4.6 backports_1.1.6 scales_1.1.0 jsonlite_1.6.1
[31] fs_1.4.1 hms_0.5.3 packrat_0.5.0 digest_0.6.25 stringi_1.4.6 grid_4.0.0
[37] cli_2.0.2 tools_4.0.0 magrittr_1.5 crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.0
[43] xml2_1.3.1 reprex_0.3.0 lubridate_1.7.8 assertthat_0.2.1 rmarkdown_2.3 httr_1.4.1
[49] rstudioapi_0.11 R6_2.4.1 nlme_3.1-147 compiler_4.0.0
虽然我不确定下图是否反映了你想要的,但你可以按如下方式换行:
您可以通过 LaTeX 的 fvextra 包控制 PDF 文件中代码的外观。您可以通过 header-includes
:
header-includes:
- |
```{=latex}
\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
showspaces = false,
showtabs = false,
breaklines,
commandchars=\\{\}
}
```
如果你想去除换行中的箭头符号,请添加breaksymbolleft={}
(另请参阅the manual中对breaksymbolleft
的描述):
header-includes:
- |
```{=latex}
\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
breaksymbolleft={},
showspaces = false,
showtabs = false,
breaklines,
commandchars=\\{\}
}
```
这都可以与 tidy = "stlyer"
选项结合使用以同时具有两种功能(断行和使用整齐的样式)。
MWE
---
title: "R Notebook"
output: pdf_document
header-includes:
- |
```{=latex}
\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
breaksymbolleft={},
showspaces = false,
showtabs = false,
breaklines,
commandchars=\\{\}
}
```
---
```{r}
knitr::opts_chunk$set(tidy="styler")
```
```{r}
data(mtcars)
library(tidyverse)
variable_1 <- 10
variable_2 <- 50
variable_3 <- 30
variable_4 <- 5
variable_5 <- 100
variable_6 <- 25
variable_7 <- 600
mtcars %>%
mutate(mpg = ifelse(mpg>18,
yes = (variable_1*variable_2)*variable_3 + variable_4 + variable_5 + variable_6 + variable_7,
no = mpg)) %>%
select(mpg,disp) %>%
group_by(mpg) %>%
summarise(mean(mpg)) %>%
ungroup -> df
```