awesome-R
awesome-R copied to clipboard
Add WebForms Core technology
WebForms Core technology was created by Elanat. It is a two-way protocol between the WebForms class on the server side and the web-forms.js library on the client side, where processing is done on the client side and the server sends Action Control commands to WebFormsJS.
Example: Using WebForms Core in R
The following code is for a web page that sends color and cycle data to the server, and the server sends commands to the client to change the color of the form tag based on the cycle data.
library(httpuv)
# Load the WebForms.R script
source("{WebForms.R path}")
# Define the server logic
app <- list(
call = function(req) {
tryCatch({
if (req$REQUEST_METHOD == "POST") {
form_data <- rawToChar(req$rook.input$read())
form_data <- strsplit(form_data, "&")[[1]]
form_data <- setNames(
lapply(form_data, function(x) URLdecode(strsplit(x, "=")[[1]][2])),
sapply(form_data, function(x) strsplit(x, "=")[[1]][1])
)
if (!is.null(form_data$Button)) {
bg_color1 <- form_data$txt_BackgroundColor1
interval1 <- form_data$txt_Interval1
bg_color2 <- form_data$txt_BackgroundColor2
interval2 <- form_data$txt_Interval2
form <- WebForms()
# Set form properties using WebForms methods
form$SetBackgroundColor("<form>", bg_color1)
form$AssignInterval(interval1)
form$SetBackgroundColor("<form>", bg_color2)
form$AssignInterval(interval2)
form$SetDisabled("(Button)", TRUE)
# Return a response to the client
return(list(
status = 200L,
headers = list('Content-Type' = 'text/plain'),
body = form$Response()
))
}
}
# If the request is not a POST or the button was not clicked, return the HTML form
return(list(
status = 200L,
headers = list('Content-Type' = 'text/html'),
body = '<!DOCTYPE html>
<html>
<head>
<title>Using WebForms Core</title>
<script type="text/javascript" src="/script/web-forms.js"></script>
</head>
<body>
<h1>WebForms Core Technology in R</h1>
<form method="POST" action="/">
<label for="txt_BackgroundColor1">Set Background Color 1</label>
<input name="txt_BackgroundColor1" id="txt_BackgroundColor1" type="text" /><br><br>
<label for="txt_Interval1">Interval 1</label>
<input name="txt_Interval1" id="txt_Interval1" type="number" value="1" min="1" max="10" /><br><br>
<label for="txt_BackgroundColor2">Set Background Color 2</label>
<input name="txt_BackgroundColor2" id="txt_BackgroundColor2" type="text" /><br><br>
<label for="txt_Interval2">Interval 2</label>
<input name="txt_Interval2" id="txt_Interval2" type="number" value="1" min="1" max="10" /><br><br>
<input name="Button" type="submit" value="Click to send data" />
</form>
</body>
</html>'
))
}, error = function(e) {
# Print detailed error information
message("An error occurred: ", e$message)
traceback()
})
}
)
# Start the server
server <- startServer("127.0.0.1", 8080, app)
The GIF image below shows how the above code works.