Skip to content

Commit 1835833

Browse files
committed
str_view html widget
1 parent ccf9bce commit 1835833

File tree

8 files changed

+98
-1
lines changed

8 files changed

+98
-1
lines changed

DESCRIPTION

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ Imports:
1818
magrittr
1919
Suggests:
2020
testthat,
21-
knitr
21+
knitr,
22+
htmltools,
23+
htmlwidgets
2224
VignetteBuilder: knitr

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export(str_to_lower)
3636
export(str_to_title)
3737
export(str_to_upper)
3838
export(str_trim)
39+
export(str_view)
3940
export(str_wrap)
4041
export(word)
4142
import(stringi)

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# stringr 1.0.0.9000
22

3+
* `str_view()` creates an HTML widget to display regular expression
4+
matches (#96).
5+
36
* The replacement to `perl()` is `regex()` not `regexp()` (#61).
47

58
# stringr 1.0.0

R/view.R

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' View HTML rendering of regular expression match.
2+
#'
3+
#' @inheritParams str_detect
4+
#' @export
5+
#' @examples
6+
#' str_view(c("abc", "def", "fgh"), "[aeiou]")
7+
#' str_view(c("abc", "def", "fgh"), "^")
8+
str_view <- function(x, pattern) {
9+
loc <- str_locate(x, pattern)
10+
11+
# How to do escaping? Need to update x and loc
12+
13+
has_match <- !is.na(loc[, "start"])
14+
str_sub(x[has_match], loc[has_match, , drop = FALSE]) <-
15+
paste0("<span class='match'>", str_sub(x[has_match], loc[has_match, , drop = FALSE]), "</span>")
16+
17+
bullets <- htmltools::HTML(str_c(
18+
"<ul>\n",
19+
str_c(" <li>", x, "</li>", collapse = "\n"),
20+
"\n</ul>"
21+
))
22+
23+
htmlwidgets::createWidget("str_view", list(html = bullets),
24+
sizingPolicy = htmlwidgets::sizingPolicy(knitr.figure = FALSE, defaultHeight = NA),
25+
package = "stringr")
26+
}

inst/htmlwidgets/lib/str_view.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.str_view ul, .str_view li {
2+
list-style: none;
3+
padding: 0;
4+
margin: 0.5em 0;
5+
font-family: monospace;
6+
}
7+
8+
.str_view .match {
9+
border: 1px solid #ccc;
10+
background-color: #eee;
11+
}

inst/htmlwidgets/str_view.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
HTMLWidgets.widget({
2+
3+
name: 'str_view',
4+
5+
type: 'output',
6+
7+
initialize: function(el, width, height) {
8+
},
9+
10+
renderValue: function(el, x, instance) {
11+
el.innerHTML = x.html;
12+
},
13+
14+
resize: function(el, width, height, instance) {
15+
}
16+
17+
});

inst/htmlwidgets/str_view.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencies:
2+
- name: str_view
3+
version: 0.1.0
4+
src: htmlwidgets/lib/
5+
stylesheet: str_view.css

man/str_view.Rd

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/view.R
3+
\name{str_view}
4+
\alias{str_view}
5+
\title{View HTML rendering of regular expression match.}
6+
\usage{
7+
str_view(x, pattern)
8+
}
9+
\arguments{
10+
\item{pattern}{Pattern to look for.
11+
12+
The default interpretation is a regular expression, as described
13+
in \link[stringi]{stringi-search-regex}. Control options with
14+
\code{\link{regex}()}.
15+
16+
Match a fixed string (i.e. by comparing only bytes), using
17+
\code{\link{fixed}(x)}. This is fast, but approximate. Generally,
18+
for matching human text, you'll want \code{\link{coll}(x)} which
19+
respects character matching rules for the specified locale.
20+
21+
Match character, word, line and sentence boundaries with
22+
\code{\link{boundary}()}. An empty pattern, "", is equivalent to
23+
\code{boundary("character")}.}
24+
}
25+
\description{
26+
View HTML rendering of regular expression match.
27+
}
28+
\examples{
29+
str_view(c("abc", "def", "fgh"), "[aeiou]")
30+
str_view(c("abc", "def", "fgh"), "^")
31+
}
32+

0 commit comments

Comments
 (0)