/
reformatAddin.R
62 lines (49 loc) · 1.58 KB
/
reformatAddin.R
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
#' Reformat a block of code using formatR.
reformatAddin <- function() {
formatRLink <- tags$a(href = "http://yihui.name/formatR/", "formatR")
ui <- miniPage(
includeHighlightJs(),
gadgetTitleBar("Reformat Code"),
miniContentPanel(
h4("Use ", formatRLink, " to reformat code."),
hr(),
stableColumnLayout(
checkboxInput("brace.newline", "Place left braces '{' on a new line?", FALSE),
numericInput("indent", "Indent size: ", 2),
numericInput("width", "Column width: ", 60)
),
uiOutput("document", container = rCodeContainer)
)
)
server <- function(input, output, session) {
# Get the document context.
context <- rstudioapi::getActiveDocumentContext()
reactiveDocument <- reactive({
# Collect inputs
brace.newline <- input$brace.newline
indent <- input$indent
width <- input$width
# Build formatted document
formatted <- formatR::tidy_source(
text = context$contents,
output = FALSE,
width.cutoff = width,
indent = indent,
brace.newline = brace.newline
)$text.tidy
formatted
})
output$document <- renderCode({
document <- reactiveDocument()
highlightCode(session, "document")
document
})
observeEvent(input$done, {
contents <- paste(reactiveDocument(), collapse = "\n")
rstudioapi::setDocumentContents(contents, id = context$id)
invisible(stopApp())
})
}
viewer <- dialogViewer("Reformat Code", width = 1000, height = 800)
runGadget(ui, server, viewer = viewer)
}