File: formatR.R

package info (click to toggle)
r-cran-formatr 1.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: javascript: 19; sh: 10; makefile: 2
file content (200 lines) | stat: -rw-r--r-- 8,340 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
## ----setup, include=FALSE-------------------------------------------
options(formatR.indent = 4, width = 70)
knitr::opts_chunk$set(tidy = TRUE)

## ----eval=FALSE-----------------------------------------------------
#  install.packages('formatR', repos = 'http://cran.rstudio.com')
#  # or development version
#  options(repos = c(
#    yihui = 'https://yihui.r-universe.dev',
#    CRAN = 'https://cloud.r-project.org'
#  ))
#  install.packages('formatR')

## -------------------------------------------------------------------
library(formatR)
sessionInfo()

## ----example, eval=FALSE, tidy=FALSE--------------------------------
#  ## comments are retained;
#  # a comment block will be reflowed if it contains long comments;
#  #' roxygen comments will not be wrapped in any case
#  1+1
#  
#  if(TRUE){
#  x=1  # inline comments
#  }else{
#  x=2;print('Oh no... ask the right bracket to go away!')}
#  1*3 # one space before this comment will become two!
#  2+2+2    # only 'single quotes' are allowed in comments
#  
#  lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
#  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
#  ## here is a long long long long long long long long long long long long long comment that may be wrapped

## ----example, eval=FALSE, tidy.opts=list(width.cutoff=50)-----------
#  ## comments are retained;
#  # a comment block will be reflowed if it contains long comments;
#  #' roxygen comments will not be wrapped in any case
#  1+1
#  
#  if(TRUE){
#  x=1  # inline comments
#  }else{
#  x=2;print('Oh no... ask the right bracket to go away!')}
#  1*3 # one space before this comment will become two!
#  2+2+2    # only 'single quotes' are allowed in comments
#  
#  lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
#  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
#  ## here is a long long long long long long long long long long long long long comment that may be wrapped

## ----collapse=TRUE--------------------------------------------------
library(formatR)
usage(glm, width = 40)  # can set arbitrary width here
args(glm)

## ----echo=FALSE, results='asis'-------------------------------------
if (ignore_img <- !is.na(Sys.getenv('_R_CHECK_PACKAGE_NAME_', NA))) cat('<!--')

## ----echo=FALSE, results='asis'-------------------------------------
if (ignore_img) cat('\n-->')

## ----comment=NA-----------------------------------------------------
set.seed(123)
tidy_eval(text = c("a<-1+1;a  # print the value", "matrix(rnorm(10),5)"))

## ----eval=FALSE-----------------------------------------------------
#  library(formatR)
#  tidy_eval()
#  # without specifying any arguments, it reads code from clipboard

## ----example, eval=FALSE, echo=5, tidy.opts=list(arrow=TRUE)--------
#  ## comments are retained;
#  # a comment block will be reflowed if it contains long comments;
#  #' roxygen comments will not be wrapped in any case
#  1+1
#  
#  if(TRUE){
#  x=1  # inline comments
#  }else{
#  x=2;print('Oh no... ask the right bracket to go away!')}
#  1*3 # one space before this comment will become two!
#  2+2+2    # only 'single quotes' are allowed in comments
#  
#  lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
#  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
#  ## here is a long long long long long long long long long long long long long comment that may be wrapped

## ----example, eval=FALSE, echo=1:5, tidy.opts=list(blank = FALSE)----
#  ## comments are retained;
#  # a comment block will be reflowed if it contains long comments;
#  #' roxygen comments will not be wrapped in any case
#  1+1
#  
#  if(TRUE){
#  x=1  # inline comments
#  }else{
#  x=2;print('Oh no... ask the right bracket to go away!')}
#  1*3 # one space before this comment will become two!
#  2+2+2    # only 'single quotes' are allowed in comments
#  
#  lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
#  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
#  ## here is a long long long long long long long long long long long long long comment that may be wrapped

## ----example, eval=FALSE, echo=5, tidy.opts=list(indent = 2)--------
#  ## comments are retained;
#  # a comment block will be reflowed if it contains long comments;
#  #' roxygen comments will not be wrapped in any case
#  1+1
#  
#  if(TRUE){
#  x=1  # inline comments
#  }else{
#  x=2;print('Oh no... ask the right bracket to go away!')}
#  1*3 # one space before this comment will become two!
#  2+2+2    # only 'single quotes' are allowed in comments
#  
#  lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
#  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
#  ## here is a long long long long long long long long long long long long long comment that may be wrapped

## ---- args-code, eval=FALSE-----------------------------------------
#  shiny::updateSelectizeInput(session, "foo", label = "New Label",
#                              selected = c("A", "B"), choices = LETTERS,
#                              server = TRUE)

## ---- args-code, eval=FALSE, tidy.opts=list(args.newline=TRUE)------
#  shiny::updateSelectizeInput(session, "foo", label = "New Label",
#                              selected = c("A", "B"), choices = LETTERS,
#                              server = TRUE)

## ---- pipe-code, eval=FALSE, tidy=FALSE-----------------------------
#  mtcars %>% subset(am == 0) %>% lm(mpg~hp, data=.)

## ---- pipe-code, eval=FALSE, tidy=TRUE------------------------------
#  mtcars %>% subset(am == 0) %>% lm(mpg~hp, data=.)

## ----example, eval=FALSE, echo=5, tidy.opts=list(brace.newline = TRUE)----
#  ## comments are retained;
#  # a comment block will be reflowed if it contains long comments;
#  #' roxygen comments will not be wrapped in any case
#  1+1
#  
#  if(TRUE){
#  x=1  # inline comments
#  }else{
#  x=2;print('Oh no... ask the right bracket to go away!')}
#  1*3 # one space before this comment will become two!
#  2+2+2    # only 'single quotes' are allowed in comments
#  
#  lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
#  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
#  ## here is a long long long long long long long long long long long long long comment that may be wrapped

## ----example, eval=FALSE, echo=11:12, tidy.opts=list(wrap = FALSE)----
#  ## comments are retained;
#  # a comment block will be reflowed if it contains long comments;
#  #' roxygen comments will not be wrapped in any case
#  1+1
#  
#  if(TRUE){
#  x=1  # inline comments
#  }else{
#  x=2;print('Oh no... ask the right bracket to go away!')}
#  1*3 # one space before this comment will become two!
#  2+2+2    # only 'single quotes' are allowed in comments
#  
#  lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
#  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
#  ## here is a long long long long long long long long long long long long long comment that may be wrapped

## ----example, eval=FALSE, tidy.opts=list(comment = FALSE, width.cutoff = 50)----
#  ## comments are retained;
#  # a comment block will be reflowed if it contains long comments;
#  #' roxygen comments will not be wrapped in any case
#  1+1
#  
#  if(TRUE){
#  x=1  # inline comments
#  }else{
#  x=2;print('Oh no... ask the right bracket to go away!')}
#  1*3 # one space before this comment will become two!
#  2+2+2    # only 'single quotes' are allowed in comments
#  
#  lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
#  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  # comment after a long line
#  ## here is a long long long long long long long long long long long long long comment that may be wrapped

## ----comment-brace, tidy=FALSE, eval=FALSE--------------------------
#  if (TRUE) {## comments
#  }

## ----comment-brace, eval=FALSE--------------------------------------
#  if (TRUE) {## comments
#  }

## -------------------------------------------------------------------
deparse(parse(text = '1+2-3*4/5 # a comment'))