-
Notifications
You must be signed in to change notification settings - Fork 0
/
how_to_use_in_a_shiny_app.Rmd
94 lines (83 loc) · 2.06 KB
/
how_to_use_in_a_shiny_app.Rmd
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
---
title: "Use highlighter in a Shiny App"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Use highlighter in a Shiny App}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
You can execute the following code to launch the sample Shiny app included with this package and
experience it in action.
```{r eval = FALSE}
shiny::shinyAppFile(system.file("examples/shiny_app.R", package = "highlighter"))
```
The code for the application mentioned above is as follows:
```{r eval = FALSE}
library(shiny)
library(highlighter)
library(stringr)
ui <- fluidPage(
h1("Highlighter example"),
hr(),
div(
style = "padding: 1rem;",
selectizeInput(
inputId = "file",
label = "Select a file",
choices = list.files(
path.package(package = "highlighter"),
recursive = TRUE
) |>
str_subset(pattern = "docs", negate = TRUE) |>
str_subset(pattern = ".css", negate = TRUE)
),
checkboxInput(
inputId = "autoguess",
label = "Auto guess language?",
value = TRUE
),
selectizeInput(
inputId = "language",
label = "Select language",
choices = highlighter::get_available_languages(),
selected = "r"
),
selectizeInput(
inputId = "theme",
label = "Select theme",
choices = highlighter::get_available_themes(),
selected = "tomorrow_night"
),
highlighterOutput("code")
)
)
server <- function(input, output, session) {
output$code <- renderHighlighter({
shiny::req(shiny::isTruthy(input$language))
highlight_file(
file_path = file.path(
path.package(package = "highlighter"),
input$file
),
language = if (input$autoguess) NULL else input$language,
theme = input$theme,
plugins = list(
line_number(
use_line_number = TRUE,
start_from = 3
),
highlight(
range = "7-10,13"
)
)
)
})
}
shinyApp(ui, server)
```