Pass R data as input in html?

I have a list of R

list[[1]] [1] 5720 JACKSONBURG RD,TRENTON,OH,45067 [2] 1282 OAKMONT AVE,HAMILTON,OH,45013 [3] 1001 CHASE AVE,HAMILTON,OH,45015 [4] 2266 TWIN OAKS DR,LEBANON,OH,45036 

And I created a google map in javascript with HTML. I use the above address as input (manually) for the value in HTML:

  <select multiple id="waypoints"> <option value="5720 JACKSONBURG RD,TRENTON,OH">1</option> <option value="1282 OAKMONT AVE,HAMILTON,OH,45013 ">2</option> <option value="1001 CHASE AVE,HAMILTON,OH,45015">3</option> <option value="2266 TWIN OAKS DR,LEBANON,OH,45036">4</option> 

Is there a way to generate <option value="XXXXX"> based on the R data that I have? How is the loop? I follow https://developers.google.com/maps/documentation/javascript/examples/directions-waypoints

+5
source share
2 answers

One possibility is to use xmlNode() from the XML package. Here we can also use it with lapply() in the .children argument to create child nodes in one call.

 library(XML) xmlNode( "select multiple", attrs = c(id = "waypoints"), .children = lapply(seq_along(x[[1]]), function(i) { xmlNode("option", i, attrs = c(value = x[[1]][i])) }) ) # <select multiple id="waypoints"> # <option value="5720 JACKSONBURG RD,TRENTON,OH,45067">1</option> # <option value="1282 OAKMONT AVE,HAMILTON,OH,45013">2</option> # <option value="1001 CHASE AVE,HAMILTON,OH,45015">3</option> # <option value="2266 TWIN OAKS DR,LEBANON,OH,45036">4</option> # </select multiple> 

Data:

 x <- list(c("5720 JACKSONBURG RD,TRENTON,OH,45067", "1282 OAKMONT AVE,HAMILTON,OH,45013", "1001 CHASE AVE,HAMILTON,OH,45015", "2266 TWIN OAKS DR,LEBANON,OH,45036" )) 
+3
source

If you just want to create HTML, you can do this with the paste () function. With your sample data (which looks like a list containing a vector)

 x<-list(c("5720 JACKSONBURG RD,TRENTON,OH,45067", "1282 OAKMONT AVE,HAMILTON,OH,45013", "1001 CHASE AVE,HAMILTON,OH,45015", "2266 TWIN OAKS DR,LEBANON,OH,45036")) 

You can do

 paste0("<option value=\"", x[[1]], "\">",seq_along(x[[1]]),"</option>", collapse="") 
+2
source

Source: https://habr.com/ru/post/1238592/


All Articles