I have an R list
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 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 I have? Like a loop?
I am following https://developers.google.com/maps/documentation/javascript/examples/directions-waypoints
One possibility is to use xmlNode()
from the XML package. Here we can also use it with lapply()
in the .children
argument to produce the children nodes in the same 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"
))
If you just want to build the HTML, you can do that with paste(). 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="")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With