Forem

Cover image for Welcome WebForms Core Technology to R: A New Era Begins
Elanat Framework
Elanat Framework

Posted on

Welcome WebForms Core Technology to R: A New Era Begins

Continuing the development of WebForms Core technology, we went to the R programming language and aligned the WebForms.R class with version 1.6 of the WebFormsJS library. The WebForms class on the server and the WebFormsJS library on the client constitute the WebForms Core technology.

At Elanat, we will provide WebForms Core technology to all web-based programming languages. Of course, there are a lot of programming languages, some of which are obsolete, some of which are obscure, and some of which are not used on the web, so our effort is to support Webforms Core technology in more than 99% of programming languages ​​​​that are also used on the web.

WebForms Core is an ambitious technology that eliminates the need for front-end development. In this technology, HTML tags are managed by the server. WebForms Core technology allows you to request the server only once for various events if you do not need server data, and then the same event will perform its action without requesting the server.

The following video shows the functionality of WebForms Core technology.

Download Links

You can download the WebForms class for the R programming language from the link below.

WebForms.R

To download the latest version of WebFormsJS, visit the following link.

Download 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)
Enter fullscreen mode Exit fullscreen mode

The GIF image below shows how the above code works.

WebForms Core Technology in R

Initialization

For the initial GET request, an HTML page is called. This page has a form tag and text and number inputs whose data is sent via the POST method using the submit button.

Change color by cycle

When the button is clicked, it extracts the form-specific fields (txt_backgroundcolor1, txt_interval1, etc.) from form data. It then creates a new instance of WebForms to manipulate the HTML tags using methods like SetBackgroundColor and SetDisabled.

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)

...
body = form$Response()
Enter fullscreen mode Exit fullscreen mode

Finally, the response body is created using the Resopnse method.

Accessing the WebForms class from source is the physical file path of the WebForms.R class.

source("{WebForms.R path}")
Enter fullscreen mode Exit fullscreen mode

Note: To use the WebForms.R class (only in R), you need to install the BH package.
The BH package is installed with the following command:

install.packages("BH")
Enter fullscreen mode Exit fullscreen mode

You can get the sample code in link below.
WebForms.R Update to WebFormsJS 1.6

Conclusion

R is a programming language that is mostly used for statistical computing and data visualization, but it is also used to some extent for web development. The creation of the WebForms class in R demonstrates Elanat's commitment to supporting WebForms Core technology in all programming languages. Using WebForms Core technology, R programmers can manage HTML tags through the server; using this technology leads to ease of web development.

Top comments (0)