Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
^LICENSE\.md$
^README\.Rmd$
^data-raw$
^dev$
^\.github$
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ Imports:
DT,
dygraphs,
ggplot2 (>= 3.0.0),
htmltools,
magrittr,
plotly,
stats,
utils
Suggests:
Suggests:
hexbin,
MASS,
testthat
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.3.3
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ export(random_ggplot)
export(random_ggplotly)
export(random_image)
export(random_lm)
export(random_mock)
export(random_print)
export(random_table)
export(random_text)
importFrom(DT,datatable)
importFrom(attempt,stop_if_all)
importFrom(attempt,stop_if_not)
importFrom(dygraphs,dygraph)
importFrom(ggplot2,aes)
importFrom(ggplot2,coord_flip)
importFrom(ggplot2,facet_grid)
importFrom(ggplot2,geom_area)
importFrom(ggplot2,geom_bar)
importFrom(ggplot2,geom_bin2d)
importFrom(ggplot2,geom_boxplot)
Expand All @@ -42,6 +45,9 @@ importFrom(ggplot2,stat)
importFrom(ggplot2,theme_minimal)
importFrom(ggplot2,xlim)
importFrom(ggplot2,ylim)
importFrom(htmltools,HTML)
importFrom(htmltools,tagList)
importFrom(htmltools,tags)
importFrom(magrittr,"%>%")
importFrom(plotly,ggplotly)
importFrom(stats,HoltWinters)
Expand Down
9 changes: 8 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# shinipsum 0.0.0.9000

* Added a `NEWS.md` file to track changes to the package.
* `random_ggplot()` gains a `"ts"` type (alias `"timeseries"`) returning a
time-series oriented plot with a `Date` on the x axis (#4).
* New `random_mock()`: builds a quick "zoning" of a Shiny UI as a row of
bordered, labelled bootstrap columns (#1).
* `random_text()` gains a `var` argument to get visually different slices of
the lorem-ipsum corpus on repeated calls (#3).
* Fixed a `ggplot2` deprecation warning in `random_ggplot("line")`
(`size` -> `linewidth`) (#13).
94 changes: 94 additions & 0 deletions R/Mock.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#' A Mock UI Layout
#'
#' Build a quick "zoning" of a Shiny UI: a row of bootstrap columns, each one
#' drawn as an empty bordered box with a centered label. Handy to sketch the
#' layout of an app before any real content exists.
#'
#' The returned object is a [htmltools::tagList()] (a self-contained CSS `<style>`
#' block plus the columns), so it can be dropped directly inside a
#' `shiny::fluidPage()` / `bslib::page_fluid()` and rendered as is.
#'
#' @param widths integer vector of bootstrap column widths (each between 1 and
#' 12). Its length defines the number of mock zones.
#' @param heights integer vector giving the visual height of each zone, expressed
#' as a number of blank lines. Recycled to the length of `widths`. Default 3.
#' @param labels character vector of zone labels. Recycled to the length of
#' `widths`. When `NULL` (default), short random lorem-ipsum labels are used.
#'
#' @importFrom htmltools tagList tags HTML
#' @importFrom attempt stop_if_not
#'
#' @return a [htmltools::tagList()]
#'
#' @export
#'
#' @examples
#' random_mock(c(4, 8))
#' random_mock(c(6, 6), labels = c("a plot here", "a table here"))
random_mock <- function(widths, heights = 3, labels = NULL) {
stop_if_not(
widths,
~ is.numeric(.x) && length(.x) >= 1L && all(!is.na(.x)) &&
all(.x == as.integer(.x)) && all(.x >= 1L & .x <= 12L),
"`widths` must be a non-empty vector of integers between 1 and 12"
)
stop_if_not(
heights,
~ is.numeric(.x) && length(.x) >= 1L && all(!is.na(.x)) &&
all(.x == as.integer(.x)) && all(.x >= 1L),
"`heights` must be a non-empty vector of integers >= 1"
)

n <- length(widths)
heights <- rep_len(as.integer(heights), n)
if (is.null(labels)) {
labels <- vapply(
seq_len(n),
function(i) {
random_text(nwords = 3, var = i)
},
character(1)
)
}
labels <- rep_len(as.character(labels), n)

cols <- lapply(
seq_len(n),
function(i) {
tags$div(
class = paste0("col-sm-", widths[i], " shinipsum-mock"),
lapply(
seq_len(heights[i]),
function(j) {
tags$p(HTML("&nbsp;"))
}
),
tags$p(class = "shinipsum-mock-label", labels[i])
)
}
)

tagList(
mock_css(),
tags$div(class = "row", cols)
)
}

#' @noRd
mock_css <- function() {
tags$style(HTML("
.shinipsum-mock {
border: 2px solid #333;
margin: 0.5em 0;
padding: 0.5em;
background: repeating-linear-gradient(
45deg, #f6f6f6, #f6f6f6 10px, #efefef 10px, #efefef 20px
);
}
.shinipsum-mock-label {
text-align: center;
font-weight: bold;
margin: 0;
}
"))
}
49 changes: 41 additions & 8 deletions R/Plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#'
#' This function returns a ggplot object, which can be passed to `renderPlot` and `plotOutput`
#'
#' @param type type of the geom. Can be any of "random", "point", "bar", "boxplot","col", "tile", "line", "bin2d", "contour", "density", "density_2d", "dotplot", "hex", "freqpoly", "histogram", "ribbon", "raster", "tile", "violin" and defines the geom of the ggplot. Default is "random", and chooses a random geom for you.
#' @param type type of the geom. Can be any of "random", "point", "bar", "boxplot","col", "tile", "line", "bin2d", "contour", "density", "density_2d", "dotplot", "hex", "freqpoly", "histogram", "ribbon", "raster", "violin", "ts" (alias "timeseries") and defines the geom of the ggplot. Default is "random", and chooses a random geom for you. The "ts" type returns a time-series oriented plot, with a `Date` on the x axis.
#'
#' @importFrom ggplot2 ggplot aes geom_point geom_bar scale_color_viridis_d theme_minimal geom_boxplot labs coord_flip geom_tile geom_line facet_grid geom_col scale_fill_viridis_c
#' @importFrom ggplot2 ggplot aes geom_point geom_bar scale_color_viridis_d theme_minimal geom_boxplot labs coord_flip geom_tile geom_line geom_area facet_grid geom_col scale_fill_viridis_c
#' @importFrom ggplot2 xlim ylim geom_bin2d geom_contour geom_density geom_density_2d geom_dotplot
#' @importFrom ggplot2 geom_hex geom_freqpoly stat geom_histogram geom_ribbon geom_raster geom_violin
#'
Expand All @@ -17,8 +17,11 @@ random_ggplot <- function(type = c("random", "point", "bar",
"line", "bin2d", "contour",
"density", "density_2d", "dotplot",
"hex", "freqpoly", "histogram",
"ribbon", "raster", "tile",
"violin")) {
"ribbon", "raster",
"violin", "ts")) {
if (length(type) == 1L && identical(type, "timeseries")) {
type <- "ts"
}
type_matched <- match.arg(type)

if (type_matched == "random") {
Expand All @@ -33,7 +36,7 @@ random_ggplot <- function(type = c("random", "point", "bar",
"bar" = sample(10:11, 1),
"boxplot" = sample(20:21, 1),
"col" = sample(30:31, 1),
"tile" = sample(40:41, 1),
"tile" = sample(c(40:41, 160:161), 1),
"line" = sample(50:51, 1),
"bin2d" = sample(60:61, 1),
"contour" = sample(70:71, 1),
Expand All @@ -45,8 +48,8 @@ random_ggplot <- function(type = c("random", "point", "bar",
"histogram" = sample(130:131, 1),
"ribbon" = sample(140:141, 1),
"raster" = sample(150:151, 1),
"tile" = sample(160:161, 1),
"violin" = sample(170:171, 1)
"violin" = sample(170:171, 1),
"ts" = sample(180:182, 1)
Comment on lines 49 to +52
)

res <- switch(as.character(r),
Expand Down Expand Up @@ -144,7 +147,7 @@ random_ggplot <- function(type = c("random", "point", "bar",
"50" = list(
ggplot(datasets::women) +
aes(height, weight) +
geom_line(size = 2) +
geom_line(linewidth = 2) +
theme_minimal()
),
"51" = list(
Expand Down Expand Up @@ -299,6 +302,36 @@ random_ggplot <- function(type = c("random", "point", "bar",
ggplot(datasets::iris, aes(Species, Sepal.Length)) +
geom_violin() +
theme_minimal()
),
"180" = list(
ggplot(ggplot2::economics) +
aes(date, unemploy) +
geom_line() +
labs(x = "date", y = "unemployment") +
theme_minimal()
),
"181" = list(
ggplot(
data.frame(
date = seq(
as.Date("1949-01-01"),
by = "month",
length.out = length(datasets::AirPassengers)
),
passengers = as.numeric(datasets::AirPassengers)
)
) +
aes(date, passengers) +
geom_line() +
geom_point(size = 0.8) +
theme_minimal()
),
"182" = list(
ggplot(ggplot2::economics) +
aes(date, psavert) +
geom_area(fill = "#440154FF", alpha = 0.7) +
labs(x = "date", y = "personal savings rate") +
theme_minimal()
)
)
res[[1]]
Expand Down
45 changes: 35 additions & 10 deletions R/Text.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
#' @param nchars number of characters. One of the two params should be left NULL.
#' @param nwords number of words to return. One of the two params should be left NULL.
#' @param offset number of characters or words to offset the result by. Defaults to 0.
#' @param var integer >= 1 selecting a "variant" of the text: `var = 1` (the
#' default) returns the historical result, while higher values return distinct
#' slices of the lorem-ipsum corpus. Handy to get visually different chunks
#' when calling `random_text()` repeatedly (e.g. in a bulleted list). The
#' corpus wraps around when `var` is large.
#'
#' @importFrom attempt stop_if_all
#' @importFrom attempt stop_if_all stop_if_not
#'
#' @return a text
#'
#' @export

random_text <- function(nchars = NULL, nwords = NULL, offset=0){
random_text <- function(nchars = NULL, nwords = NULL, offset = 0, var = 1){
stop_if_all(
c(nchars, nwords),
is.null,
Expand All @@ -19,20 +24,40 @@ random_text <- function(nchars = NULL, nwords = NULL, offset=0){
stop_if_all(
list(nchars, nwords),
~ !is.null(.x),
"You can't chose both nchars and nwords"
"You can't choose both nchars and nwords"
)
Comment on lines 24 to 28
stop_if_not(
var,
~ is.numeric(.x) && length(.x) == 1L && !is.na(.x) &&
.x >= 1L && .x == as.integer(.x),
"`var` must be a single integer >= 1"
)
var <- as.integer(var)

if (!is.null(nchars)){
res <- substr(
shinipsum::lorem,
1+offset,
nchars+offset
)
if (var == 1L) {
res <- substr(
shinipsum::lorem,
1 + offset,
nchars + offset
)
} else {
total <- nchar(shinipsum::lorem)
start <- (offset + (var - 1L) * nchars) %% total
doubled <- paste0(shinipsum::lorem, shinipsum::lorem)
res <- substr(doubled, 1 + start, nchars + start)
}
} else {
res <- paste(shinipsum::lorem_words[1+offset:nwords+offset], collapse = " ")
if (var == 1L) {
res <- paste(shinipsum::lorem_words[1+offset:nwords+offset], collapse = " ")
} else {
n <- length(shinipsum::lorem_words)
start <- (offset + (var - 1L) * nwords) %% n
idx <- ((start + seq_len(nwords) - 1L) %% n) + 1L
res <- paste(shinipsum::lorem_words[idx], collapse = " ")
}
}

substr(res, 1, 1) <- toupper(substr(res, 1, 1))
res
}

2 changes: 1 addition & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ utils::globalVariables(unique(c(
"mpg", "disp", "vs", "wt", "qsec", "cyl",
"Solar.R", "Wind", "Month", "Temp", "Ozone",
"Var1", "Freq",
"date", "unemploy",
"date", "unemploy", "psavert", "passengers",
"waiting", "eruptions", "density",
"year", "month", "median",
"height", "weight",
Expand Down
Loading
Loading