Skip to content

Commit 9f3d6dc

Browse files
Update README.md
1 parent 13bd350 commit 9f3d6dc

File tree

1 file changed

+76
-41
lines changed

1 file changed

+76
-41
lines changed

r/README.md

Lines changed: 76 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,86 @@
1-
## How to work with WebForms Core in R (Shiny framework)
1+
## How to work with WebForms Core in R (httpuv library)
22

33
To use WebForms Core, first copy the WebForms class file in this directory to your project. Then create a new View file similar to the one below.
44

55
```r
6-
library(shiny)
7-
8-
ui <- fluidPage(
9-
titlePanel("Using WebForms Core"),
10-
tags$head(
11-
tags$script(src = "/script/web-forms.js")
12-
),
13-
sidebarLayout(
14-
sidebarPanel(
15-
textInput("txt_Name", "Your Name"),
16-
numericInput("txt_FontSize", "Set Font Size", value = 16, min = 10, max = 36),
17-
textInput("txt_BackgroundColor", "Set Background Color"),
18-
actionButton("btn_SetBodyValue", "Click to send data")
19-
),
20-
mainPanel(
21-
uiOutput("response")
6+
library(httpuv)
7+
8+
# Load the WebForms.R script
9+
source("{WebForms.R path}")
10+
11+
# Define the server logic
12+
app <- list(
13+
call = function(req) {
14+
tryCatch({
15+
if (req$REQUEST_METHOD == "POST") {
16+
form_data <- rawToChar(req$rook.input$read())
17+
form_data <- strsplit(form_data, "&")[[1]]
18+
form_data <- setNames(
19+
lapply(form_data, function(x) URLdecode(strsplit(x, "=")[[1]][2])),
20+
sapply(form_data, function(x) strsplit(x, "=")[[1]][1])
2221
)
23-
)
24-
)
2522

26-
server <- function(input, output, session) {
27-
28-
observeEvent(input$btn_SetBodyValue, {
29-
Name <- input$txt_Name
30-
BackgroundColor <- input$txt_BackgroundColor
31-
FontSize <- as.numeric(input$txt_FontSize)
32-
33-
form <- WebForms()
34-
35-
form$setFontSize(InputPlace$tag("form"), FontSize)
36-
form$setBackgroundColor(InputPlace$tag("form"), BackgroundColor)
37-
form$setDisabled(InputPlace$name("btn_SetBodyValue"), TRUE)
38-
39-
form$addTag(InputPlace$tag("form"), "h3")
40-
form$setText(InputPlace$tag("h3"), paste("Welcome", Name, "!"))
41-
42-
output$response <- renderUI({
43-
HTML(form$response())
44-
})
23+
if (!is.null(form_data$btn_SetBodyValue)) {
24+
name <- form_data$txt_Name
25+
font_size <- form_data$txt_FontSize
26+
bg_color <- form_data$txt_BackgroundColor
27+
28+
form <- WebForms()
29+
30+
# Set form properties using WebForms methods
31+
form$SetFontSize("<form>", paste0(font_size))
32+
form$SetBackgroundColor("<form>", bg_color)
33+
form$SetDisabled("(btn_SetBodyValue)", TRUE)
34+
35+
# Add a new tag and set its text
36+
form$AddTag("<form>", "h3")
37+
form$SetText("<h3>", paste0("Welcome ", name, "!"))
38+
39+
# Return a response to the client
40+
return(list(
41+
status = 200L,
42+
headers = list('Content-Type' = 'text/plain'),
43+
body = form$Response()
44+
))
45+
}
46+
}
47+
48+
# If the request is not a POST or the button was not clicked, return the HTML form
49+
return(list(
50+
status = 200L,
51+
headers = list('Content-Type' = 'text/html'),
52+
body = '<!DOCTYPE html>
53+
<html>
54+
<head>
55+
<title>Using WebForms Core</title>
56+
<script type="text/javascript" src="/script/web-forms.js"></script>
57+
</head>
58+
<body>
59+
<form method="POST" action="/">
60+
<label for="txt_Name">Your Name</label>
61+
<input name="txt_Name" id="txt_Name" type="text" />
62+
<br>
63+
<label for="txt_FontSize">Set Font Size</label>
64+
<input name="txt_FontSize" id="txt_FontSize" type="number" value="16" min="10" max="36" />
65+
<br>
66+
<label for="txt_BackgroundColor">Set Background Color</label>
67+
<input name="txt_BackgroundColor" id="txt_BackgroundColor" type="text" />
68+
<br>
69+
<input name="btn_SetBodyValue" type="submit" value="Click to send data" />
70+
</form>
71+
</body>
72+
</html>'
73+
))
74+
}, error = function(e) {
75+
# Print detailed error information
76+
message("An error occurred: ", e$message)
77+
traceback()
4578
})
46-
}
79+
}
80+
)
4781

48-
shinyApp(ui = ui, server = server)
82+
# Start the server
83+
server <- startServer("127.0.0.1", 8080, app)
4984
```
5085

5186
In the upper part of the View file, it is first checked whether the submit button has been clicked or not, if it has been clicked, an instance of the WebForms class is created, then the WebForms methods are called, and then the response method is printed on the screen, and other parts Views are not displayed.
@@ -55,4 +90,4 @@ As you can see, the WebFormsJS script has been added in the header section of th
5590

5691
The latest version of the WebFormsJS script is available through the link below.
5792

58-
https://github.com/elanatframework/Web_forms/blob/elanat_framework/web-forms.js
93+
https://github.com/elanatframework/Web_forms/blob/elanat_framework/web-forms.js

0 commit comments

Comments
 (0)