1library(shiny)
2
3shinyUI(fluidPage(
4  title = 'Tidy R Code with formatR (Yihui Xie)',
5  helpText(),  # just a placeholder for a little bit top margin
6  sidebarLayout(
7    sidebarPanel(
8      tags$head(
9        tags$script(src = 'shiny-handler.js'),
10        tags$style(type = 'text/css', '.popover {max-width: 100%;}')
11      ),
12      helpText('This Shiny app uses the function', code('tidy_source()'),
13               'in the', a(href = 'https://yihui.org/formatR/', strong('formatR')),
14               sprintf('(>= v%s)', packageVersion('formatR')),
15               'package to reformat R code in the text box on the right.',
16               a(list(icon('hand-o-right'), 'demo'), class = 'btn btn-small btn-info',
17                 onclick = '$("textarea#src").val($("#demo").val()).trigger("change");')),
18      checkboxInput('arg_comment', 'Preserve comments', TRUE),
19      checkboxInput('arg_blank', 'Preserve blank lines', TRUE),
20      checkboxInput('arg_assign', 'Replace = with <-', FALSE),
21      checkboxInput('arg_anl', 'Start function arguments on a new line', FALSE),
22      checkboxInput('arg_brace', 'Put { on a new line', FALSE),
23      checkboxInput('arg_wrap', 'Wrap comments', TRUE),
24      numericInput ('arg_indent', 'Number of spaces for indentation', 4, min = 0),
25      radioButtons('width_type', 'Line width type', c('minimum', 'maximum'), inline = TRUE),
26      numericInput ('arg_width', 'Line width value', 70, min = 20, max = 500),
27      submitButton ('Format My Code', icon('toggle-right'))
28    ),
29    mainPanel(
30      tags$textarea(
31        id = 'src', rows = 20,
32        style = 'width: 99%; font-family: monospace; word-wrap: normal; white-space: pre;',
33        placeholder = 'paste your R code here...'
34      ),
35      tags$textarea(
36        id = 'demo', style = 'display: none;',
37        paste(c(
38          readLines(system.file('format', 'messy.R', package = 'formatR')),
39          if (getRversion() >= '4.1.0') c(
40            '', "# R's native pipe on a single line",
41            'mtcars |> subset(am==0) |> (\\(.) lm(mpg~hp, data=.))()'
42          ),
43          '',
44          '# magrittr pipes on a single line',
45          'mtcars %>% subset(am==0) %>% lm(mpg~hp, data=.)'
46        ), collapse = '\n')
47      )
48    )
49  )
50))
51