iembry
iembry

Reputation: 944

R - opencpu - Function result not displaying in the HTML

UPDATE: 26 October 2023

The following shows the results of 2 examples using the construction_decimal function:

construction_decimal("2'-0\"", result = "traditional", output = "vector")
# [1] 2

construction_decimal("1'-2 7/16\"", result = "librecad", output = "vector")
# [1] 14.4375

The full function (and other functions) can be viewed at https://gitlab.com/iembry/iemisc/-/blob/master/R/construction_fraction.R.

I am only enclosing the function sans the comments below:

construction_decimal <- function (measurement, result = c("traditional", "librecad"), output = c("vector", "table")) {


inch <- yd <- mm <- cm <- m <- ft <- NULL
# due to NSE notes in R CMD check


result <- result

output <- output


# Check
assert_that(qtest(result, "S==1"), msg = "There is not a result type or more than 1 result type. Please specify either 'traditional' or 'librecad'.")
# only process with enough known variables and provide an error message if the check fails

assert_that(isTRUE(any(c("traditional", "librecad") %in% result)), msg = "The result type has not been identified correctly as either 'traditional' or 'librecad'. Please try again.")
# only process with a specified result and provide a stop warning if not

assert_that(qtest(output, "S==1"), msg = "There is not a output or more than 1 output. Please specify either 'vector' or 'table'.")
# only process with enough known variables and provide an error message if the check fails

assert_that(isTRUE(any(c("vector", "table") %in% output)), msg = "The output has not been identified correctly as either 'vector' or 'table'. Please try again.")
# only process with a specified output and provide a stop warning if not

assert_that(!any(testString(measurement, min.chars = 1, pattern = "[0-9]", ignore.case = TRUE) == FALSE), msg = "measurement is a numeric vector or a character vector without any numeric values. measurement should be a character vector that contains numeric values. Please try again.")
# only process with string values with numbers and provide an error message if the check fails


if (result == "traditional") {

# remove all non-numeric characters at once
change1 <- stri_trim_both(mgsub(measurement, c("-", "[A-Za-z]", "\"", "'"), c(" ", "", "", " ")))

assert_that(all(stri_detect_regex(change1, "[0-9]")), msg = "measurement does not include numeric values in all parts. Please try again.")
# only process with a numeric value present in all parts and provide a stop warning if change1 does not include a numeric value in all parts (measurement in error message since only that parameter has been defined)


# split the string at 2 spaces to have 2 parts
change2 <- stri_split_fixed(change1, " ", n = 2, omit_empty = TRUE)

# trim the left and right white space
change3 <- lapply(change2, stri_trim_both)

# change to a numeric vector
change3_part1 <- as.numeric(change3[[1]][1])

# get the 2nd part of change3
change3_part2 <- change3[[1]][2]

# convert to a numeric vector
change4 <- frac_to_numeric(change3_part2)

# divide by 12
change4_use <- change4 / 12

# get the sum (the final numeric vector)
change <- sum(change3_part1, change4_use)


if (output == "vector") {

return(change)


} else if (output == "table") {

measure_ft <- set_units(change, "ft")

measure_in <- measure_ft

measure_yd <- measure_ft

measure_mm <- measure_ft

measure_cm <- measure_ft

measure_m <- measure_ft

units(measure_in) <- make_units(inch)

units(measure_yd) <- make_units(yd)

units(measure_mm) <- make_units(mm)

units(measure_cm) <- make_units(cm)

units(measure_m) <- make_units(m)


# create data.table for displaying the results
result_table <- data.table(Measurement = c(drop_units(measure_in), drop_units(measure_ft), drop_units(measure_yd), drop_units(measure_mm), drop_units(measure_cm), drop_units(measure_m)), Units = c("in", "ft", "yd", "mm", "cm", "m"))
setnames(result_table, c("Measurement", "Units"))

col.names <- c("Measurement", "Units")


# code block below modified from data.table function
setattr(result_table, "col.names", setnames(result_table, col.names))
setattr(result_table, "class", c("data.table", "data.frame"))
result_table

}


} else if (result == "librecad") {

# remove all non-numeric characters at once
change1 <- stri_trim_both(mgsub(measurement, c("-", "[A-Za-z]", "\"", "'"), c(" ", "", "", " ")))

assert_that(all(stri_detect_regex(change1, "[0-9]")), msg = "measurement does not include numeric values in all parts. Please try again.")
# only process with a numeric value present in all parts and provide a stop warning if change1 does not include a numeric value in all parts (measurement in error message since only that parameter has been defined)


# split the string at 2 spaces to have 2 parts
change2 <- stri_split_fixed(change1, " ", n = 2, omit_empty = TRUE)

# trim the left and right white space
change3 <- lapply(change2, stri_trim_both)

# change to a numeric vector
change3_part1 <- as.numeric(change3[[1]][1])

# get the 2nd part of change3
change3_part2_pre <- change3[[1]][2]


# only 1 number is detected
if(stri_count_regex(change3_part2_pre, "\\d") == 1) {

ifelse(stri_detect_fixed(change3_part2_pre, "\""), change3_part2 <- gsub("\"", "", change3_part2_pre, fixed = TRUE), change3_part2 <- change3_part2_pre) # Source 5 for the gsub

change3_part2 <- as.numeric(change3_part2)

change <- sum(change3_part1 * 12, change3_part2)


# more than 1 number is detected
} else if(stri_count_regex(change3_part2_pre, "\\d") != 1) {

ifelse(stri_detect_fixed(change3_part2_pre, "\""), change3_part2 <- gsub("\"", "", change3_part2_pre, fixed = TRUE), change3_part2 <- change3_part2_pre) # Source 5 for the gsub

change3_part3 <- unlist(strsplit(change3_part2, split = " ", fixed = TRUE))

change3_part3b <- unlist(stri_split_fixed(change3_part3[2], "/", n = 2))

change3_part3b_a <- as.numeric(change3_part3b[1])

change3_part3b_b <- as.numeric(change3_part3b[2])

change <- sum(change3_part1 * 12, as.numeric(change3_part3[1]), change3_part3b_a / change3_part3b_b)

}


if (output == "vector") {

return(change)


} else if (output == "table") {

measure_in <- set_units(change, "inch")

measure_ft <- measure_in

measure_yd <- measure_in

measure_mm <- measure_in

measure_cm <- measure_in

measure_m <- measure_in

units(measure_ft) <- make_units(ft)

units(measure_yd) <- make_units(yd)

units(measure_mm) <- make_units(mm)

units(measure_cm) <- make_units(cm)

units(measure_m) <- make_units(m)


# create data.table for displaying the results
result_table <- data.table(Measurement = c(drop_units(measure_in), drop_units(measure_ft), drop_units(measure_yd), drop_units(measure_mm), drop_units(measure_cm), drop_units(measure_m)), Units = c("in", "ft", "yd", "mm", "cm", "m"))
setnames(result_table, c("Measurement", "Units"))

col.names <- c("Measurement", "Units")


# code block below modified from data.table function
setattr(result_table, "col.names", setnames(result_table, col.names))
setattr(result_table, "class", c("data.table", "data.frame"))
result_table

}

}
}

UPDATE: 25 October 2023

The HTML Web page can be viewed online at https://gitlab.com/iembry/iemisc/-/blob/master/inst/www/construction_decimal.html


I am creating a HTML interface to the construction_decimal function from my iemisc package and everything is working fine except the function result (id = finished) is not showing up when called in the HTML paragraph with the id = finished.

This is the log result from opecpu:

Loading required package: iemisc.opencpu
[2023-10-24 13:18:41] POST /ocpu/library/iemisc.opencpu/R/construction_decimal
[2023-10-24 13:18:41] GET /ocpu/tmp/x0f7f07d877110c/R/.val/json

I have viewed the json result in my local server and it's right.

The following is the JavaScript code to run the R function:

//init this script when the page has loaded
$(document).ready(function(){
$("#submitbutton").on("click", function(){
//disable the button to prevent multiple clicks
$("#submitbutton").attr("disabled", "disabled");

//read the value for 'measurement'
var measurement = $("#measurementfield").val();

//read the value for 'result'
var result = $("#resultfield").val();

//read the value for 'output'
var output = $("#outputfield").val();
    
//perform the request
var req = ocpu.rpc("construction_decimal", {
measurement : measurement,
result : result,
output : output,
}, function(finished){
$("#finished").text(finished.message);
});


//if R returns an error, alert the error message
req.fail(function(){
alert("Server error: " + req.responseText);
});

//after request complete, re-enable the button
req.always(function(){
$("#submitbutton").removeAttr("disabled")
});
});
});

The following shows the HTML for the intended result:

<p id="finished"></p>

Instead of the result being displayed in that paragraph, it's blank, but the result is in the temporary directory.

Is there a better method for the function(finished) JavaScript code to return the intended result?

The JavaScript code is modeled after the appdemo package (https://github.com/rwebapps/appdemo).

The bottom box in the image is the location of the finished paragraph.

enter image description here

Upvotes: 0

Views: 28

Answers (0)

Related Questions