Skip to content

Commit cf30ac1

Browse files
committed
Tweak sizing policy
Fixes #232
1 parent cf9f836 commit cf30ac1

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# stringr 1.3.0.9000
22

3-
* `str_view()` handles `NA` values more gracefully (#217)
3+
* `str_view()` handles `NA` values more gracefully (#217). I've also
4+
tweaked the sizing policy so hopefully it should work better in notebooks,
5+
while preserving the existing behaviour in knit documents (#232).
46

57
* `str_replace_all()` with a named vector now respects modifier functions (#207)
68

R/view.R

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ str_view <- function(string, pattern, match = NA) {
3636
str_sub(string[has_match], loc[has_match, , drop = FALSE]) <-
3737
paste0("<span class='match'>", str_sub(string[has_match], loc[has_match, , drop = FALSE]), "</span>")
3838

39-
string <- str_replace_na(string)
40-
bullets <- str_c(
41-
"<ul>\n",
42-
str_c(" <li>", string, "</li>", collapse = "\n"),
43-
"\n</ul>"
44-
)
45-
str_view_widget(bullets)
39+
str_view_widget(string)
4640
}
4741

4842
#' @rdname str_view
@@ -68,24 +62,27 @@ str_view_all <- function(string, pattern, match = NA) {
6862
string
6963
})
7064
string <- unlist(string_list)
65+
str_view_widget(string)
66+
}
7167

68+
str_view_widget <- function(lines) {
69+
lines <- str_replace_na(lines)
7270
bullets <- str_c(
7371
"<ul>\n",
74-
str_c(" <li>", string, "</li>", collapse = "\n"),
72+
str_c(" <li>", lines, "</li>", collapse = "\n"),
7573
"\n</ul>"
7674
)
77-
str_view_widget(bullets)
78-
}
75+
html <- htmltools::HTML(bullets)
7976

80-
str_view_widget <- function(html) {
8177
size <- htmlwidgets::sizingPolicy(
8278
knitr.figure = FALSE,
83-
defaultHeight = "auto"
79+
defaultHeight = pmin(10 * length(lines), 300),
80+
knitr.defaultHeight = "100%"
8481
)
8582

8683
htmlwidgets::createWidget(
8784
"str_view",
88-
list(html = htmltools::HTML(html)),
85+
list(html = html),
8986
sizingPolicy = size,
9087
package = "stringr"
9188
)

0 commit comments

Comments
 (0)