cssApplyInNodeSet: Find a set of elements and inside each of them apply a...

Description Usage Arguments Details Value Examples

View source: R/cssApply.R

Description

Consider the following case : on an html page you have information about several people. Several informations are included in a div of class "people", but for some people some information is missing.

Usage

1
cssApplyInNodeSet(doc, path, relPath, fun, prefix = "./", ...)

Arguments

doc

An html document parsed with htmlParse.

path

Character. It CSS path used to identify elements where to search the information

relPath

Character. CSS path used to select elements in the elements selected with "path"

fun

Function to apply to the selected nodes.

prefix

Should be "./" if the first element in relPath has to be the child of the elements selected by "path", or ".//" if it may be any descendent (and not necessarily a direct child).

...

Parameters passed to "fun"

Details

With this function, you can first select all divs of class "people" and then search inside them if the information is available. If not, the function will return a NA value for the person.

Value

If no element in the html document corresponds to "path", the function will return an empty list. Else it will return a list of length equal to the number of elements selected.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
doc <- "<html>
<head></head>
  <body>
  <div id='character1' class='character'>
  <span class='name'>Mike</span>
  <span class='level digit'>10</span>
  </div>
  <div id='character2' class='character'>
  <span class='name'>Stan</span>
  </div>
  </body>
  </html>"

doc <- htmlParse(doc)

# Level of characters
cssApply(doc, ".character>.level", cssNumeric)

# character 2 does not have level, we would want to have a NA value instead of nothing
cssApplyInNodeSet(doc, ".character", ".level", cssNumeric)

cuche27/CSS documentation built on Oct. 20, 2019, 1:09 a.m.