-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.R
55 lines (44 loc) · 1.24 KB
/
server.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
#//////////////////////////////////////////////////////////////////////////////
# FILE: server.R
# AUTHOR: David Ruvolo
# CREATED: 2020-01-26
# MODIFIED: 2023-08-12
# PURPOSE: backend for app, starts plumber
# STATUS: stable
# PACKAGES: **renv**
# COMMENTS: NA
#//////////////////////////////////////////////////////////////////////////////
library(plumber)
source("./server/datatable.R")
app <- Plumber$new()
#' mount client (for frontend development, run `yarn build`)
app$mount("/", PlumberStatic$new("./client/dist"))
#* @filter cors
cors <- function(req, res) {
res$setHeader("Access-Control-Allow-Origin", "*")
res$setHeader("Access-Control-Allow-Methods", "*")
res$setHeader("Access-Control-Allow-Headers", "Content-Type")
plumber::forward()
}
#' define endpoint: `/html`
app$handle(
methods = "POST",
path = "/html",
handler = function(req, value = 0) {
val <- as.numeric(value)
if (val == 999) {
data <- palmerpenguins::penguins
} else {
data <- palmerpenguins::penguins[1:val, ]
}
tbl <- as.character(
datatable(
data = data,
id = "palmerpenguins",
caption = "Penguins dataset from the `palmerpenguins` pkg"
)
)
list(html = tbl)
}
)
app$run(port = 8000)