-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-live.R
44 lines (34 loc) · 1.01 KB
/
05-live.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
# TODO: Use websockets to refresh the HTML page when R server is restarted.
# TODO: Detect if the script is run from nodemon or not.
# devtools::load_all()
library(ambhtmx)
live_path <- tryCatch(
{this.path::this.path()},
error = function(e) return("")
)
#' Starting the app
counter <- 0
c(app, context, operations) %<-% ambhtmx(live = live_path)
#' Main page of the app
app$get("/", \(req, res){
html <- render_page(
page_title = "ambiorix + htmx example",
main = div(
style = "margin: 100px",
h1("ambhtmx live hot realoading example"),
p(id = "counter", glue("Counter is set to {counter}")),
button(
"+1",
`hx-post`="/increment", `hx-target`="#counter", `hx-swap`="innerHTML"
)
)
)
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(open = FALSE)