-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-counter.R
43 lines (39 loc) · 1008 Bytes
/
01-counter.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
# remotes::install_github("jrosell/ambhtmx", force = TRUE)
# devtools::load_all()
library(ambhtmx)
#' Starting the app
counter <- 0
tryCatch({
c(app, context, operations) %<-% ambhtmx()
},
error = \(e) print(e)
)
#' Main page of the app
app$get("/", \(req, res){
html <- ""
tryCatch(
{
html <- render_page(
page_title = "ambhtmx counter example",
main = div(
style = "margin: 20px",
h1("ambhtmx counter example"),
p(id = "counter", glue("Counter is set to {counter}")),
button(
"+1",
`hx-post`="/increment", `hx-target`="#counter", `hx-swap`="innerHTML"
)
)
)
},
error = \(e) html <- p(e)
)
res$send(html)
})
#' Post call to return the value of the global counter variable
app$post("/increment", \(req, res){
counter <<- counter + 1
res$send(glue("Counter is set to {counter}"))
})
#' Start the app with all the previous defined routes
app$start()