Skip to content

Commit

Permalink
tidy and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless committed May 26, 2024
1 parent 7f63626 commit 36906ec
Show file tree
Hide file tree
Showing 35 changed files with 1,954 additions and 316 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: emphatic
Type: Package
Title: Highlight R Output
Version: 0.1.6.9013
Version: 0.1.6.9014
Author: mikefc
Maintainer: mikefc <[email protected]>
Description: User-defined colour highlighting of data.frames and other R output.
Expand All @@ -19,6 +19,7 @@ Suggests:
dplyr,
ggplot2,
tidyr,
purrr,
testthat,
openxlsx
VignetteBuilder: knitr
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# emphatic 0.1.6.9013 2024-05-05
# emphatic 0.1.6.9014 2024-05-26

* Removed `hl_mat()` and associated vignettes.
* function is not exported for now as the selection process is too
Expand Down
5 changes: 4 additions & 1 deletion R/core-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ as_html <- function(x, ..., font_size = NULL, style = list(), complete = FALSE,
#' @return None
#' @export
#' @examples
#' # Will try and spawn a viewer for content
#' \dontrun{
#' # This example will try and spawn an external viewer for HTML content
#' # and is thus marked as 'dontrun' so it does not get executed on CRAN
#' hl_grep(mode, "switch") |>
#' as_html() |>
#' show_html()
#' }
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
show_html <- function(x, viewer = getOption("viewer", utils::browseURL)) {

Expand Down
98 changes: 2 additions & 96 deletions R/core-svg.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ as_svg <- function(x, width = 1200, height = 900, ..., font_size = NULL,




#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Wrap an emphatic object to part of an SVG
#'
Expand Down Expand Up @@ -81,7 +80,6 @@ as_svg_group <- function(x, width = 1200, height = 900, font_size = NULL,


svg_text <- paste(
"",
ifelse(visible, '<g visibility="visible">', '<g visibility="hidden">'),
'<foreignObject width=\"100%\" height=\"100%\">',
'<div xmlns=\"http://www.w3.org/1999/xhtml\">',
Expand All @@ -90,7 +88,6 @@ as_svg_group <- function(x, width = 1200, height = 900, font_size = NULL,
"</foreignObject>",
extra,
"</g>",
"",
sep = "\n"
)

Expand All @@ -102,6 +99,7 @@ as_svg_group <- function(x, width = 1200, height = 900, font_size = NULL,
}



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This helper function creates a \code{<set>} tag to do frame flipping
# animation within an SVG
Expand All @@ -127,6 +125,7 @@ make_animate_tag <- function(i, n, dur = 1, playback, svg_id) {
}



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Wrap multiple emphatic object into an SVG animation
#'
Expand Down Expand Up @@ -226,97 +225,4 @@ as_svg_anim <- function(x, width = 1200, height = 900, duration = 1, playback =
class(svg_text) <- union(c('knit_asis', 'html', 'character'), class(svg_text))

svg_text
}




#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Examples
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (FALSE) {
library(dplyr)
library(tidyr)
m <- matrix(0.1, 10, 10)

w <- 16
h <- 50

create_sinus <- function(xoff, yoff) {
expand.grid(x=1:w, y=1:h) |>
as.data.frame() |>
mutate(val = cos((x - w/2)/w + xoff) + sin((y - h/3)/h + yoff) ) |>
mutate(val = round(val, 3)) |>
spread(x, val) |>
select(-y) |>
setNames(sprintf("% 7i", seq(w))) |>
hl(ggplot2::scale_color_gradient2(), cols = all())
}


groups <- purrr::map2(
cos(seq(0, 2*pi , length.out = 60)),
sin(seq(-2*pi, 2*pi, length.out = 60)),
~create_sinus(.x, .y)
)


as_svg_anim(groups, duration = 0.1, playback = 'click') |>
writeLines("~/Desktop/demo2.svg")

create_sinus(0.3, -1.2)

as_svg_anim(groups, duration = 0.1, playback = 'infinite', browsable = TRUE)


# write_xlsx(create_sinus(0.3, -0.7), "working/sinus.xlsx", opts = hl_opts(text_mode = 'contrast'))



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Consolel print output
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library(emphatic)
library(dplyr)
library(tidyr)

w <- 16
h <- 50

create_sinus <- function(xoff, yoff) {
expand.grid(x=1:w, y=1:h) |>
as.data.frame() |>
mutate(val = cos((x - w/2)/w + xoff) + sin((y - h/3)/h + yoff) ) |>
mutate(val = round(val, 3)) |>
spread(x, val) |>
select(-y) |>
setNames(sprintf("% 7i", seq(w))) |>
hl(ggplot2::scale_color_gradient2(), cols = all())
}
groups <- purrr::map2(
cos(seq(0, 2*pi , length.out = 60)),
sin(seq(-2*pi, 2*pi, length.out = 60)),
function(x, y) {
# cat("\014")
create_sinus(x, y) |>
hl_adjust(full_colour = TRUE, text_mode = 'asis') |>
as.character() |>
cat()
Sys.sleep(0.1)
}
)














}
2 changes: 2 additions & 0 deletions R/core-xlsx.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Write an emphatic data.frame to an Excel workbook
#'
#' Requires \code{openxlsx} package
#'
#' @param x emphatic data.frame object
#' @param xlsx_filename xlsx filename
#' @param colNames Display column names? logical. Default: TRUE
Expand Down
12 changes: 1 addition & 11 deletions R/core.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ escape <- list(
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
collapse = list(
ansi = "\n",
html = "<br/>\n",
html = "<br/>",
latex = "\\\\\n",
typst = "\\\n"
)
Expand Down Expand Up @@ -342,7 +342,6 @@ as.character.emphatic <- function(x, ..., mode = 'ansi') {




#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Private function for converting an emphatic data.frame or matrix to a string
#'
Expand Down Expand Up @@ -589,7 +588,6 @@ as_character_inner <- function(m,




#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Calculate a contrasting text colours for vector of fill colours
#'
Expand Down Expand Up @@ -696,11 +694,3 @@ interp_colour <- function(colour1, colour2, frac) {

rgb(m[,1], m[,2], m[,3])
}








22 changes: 11 additions & 11 deletions R/data-sets.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#'
#' Sourced from a table in Tufte's "Visual and Statistical Thinking"
#'
#' @source \url{http://williamwolff.org/wp-content/uploads/2013/01/tufte-challenger-1997.pdf}
#'
#' @format A data.frame
#' \describe{
#' \item{flight}{Flight number}
Expand All @@ -19,13 +17,6 @@
#' \item{damage}{Damage severity index}
#' \item{date}{Date of launch}
#' }
#'
#' Other references:
#'
#' \itemize{
#' \item{\url{http://williamwolff.org/wp-content/uploads/2013/01/tufte-challenger-1997.pdf}}
#' \item{\url{https://archive.ics.uci.edu/dataset/92/challenger+usa+space+shuttle+o+ring}}
#' }
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"challenger"

Expand All @@ -36,7 +27,16 @@
#' From the 'National Snow and Ice Data Center' \url{https://nsidc.org/data/g02135}
#'
#' @format Matrix of sea ice area, monthly from 1978 to 2020.
#'
#' @source \url{ftp://sidads.colorado.edu/DATASETS/NOAA/G02135/south/monthly/data/}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"sea_ice_area"


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Monthly total rainfall in Centennial Park, Sydney, Australia
#'
#' From the Australian Bureau of Meteorology
#'
#' @format data.frame with each row representing a year, and each column
#' representing a month of that year
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"sydney_rain"
14 changes: 0 additions & 14 deletions R/hl-dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,3 @@ hl <- function(.data, palette,

.data
}


if (FALSE) {
mtcars |>
hl(ggplot2::scale_color_continuous(), cols = hp, show_legend = TRUE) |>
hl(ggplot2::scale_color_continuous(type = 'viridis'), cols = am, show_legend = TRUE) |>
hl(ggplot2::scale_color_gradient(low = 'hotpink', high = 'yellow'), cols = mpg, show_legend = TRUE)
}






6 changes: 0 additions & 6 deletions R/hl-grep.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Colour highlighting a regular expression search
#'
Expand Down Expand Up @@ -124,10 +123,5 @@ hl_grep <- function(x,
)

attr(res, 'options') <- opts


res
}



3 changes: 0 additions & 3 deletions R/hl-mat.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Highlight elements of a matrix or atomic vector
#'
Expand Down Expand Up @@ -139,4 +137,3 @@ hl_mat <- function(.data, colour, selection = NULL, elem = 'fill',

.data
}

29 changes: 0 additions & 29 deletions R/legend.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@




#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Create a single line legend
#'
Expand Down Expand Up @@ -118,29 +115,3 @@ create_legend_string <- function(

paste(ansi_vec, collapse = '')
}


if (FALSE) {

suppressPackageStartupMessages({
library(ggplot2)
})

scale <- scale_colour_viridis_c()
values <- runif(20)
scale$reset()
scale$train(values)

cat(create_legend_string(scale, values, label = "hello"), "\n")


scale <- scale_colour_viridis_d()
values <- sample(c('alpha', 'beta', 'gam', 'epsilon'), 20, T)
scale$reset()
scale$train(values)

cat(create_legend_string(scale, values, label = 'cyl'), "\n")



}
7 changes: 0 additions & 7 deletions R/options.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Set options for printing on the emphatic matrix or data.frame
#'
Expand Down Expand Up @@ -113,16 +112,10 @@ hl_opts <- function(na = getOption("HL_NA", "NA"),
text_contrast <= 1
})



list(
na = na,
full_colour = full_colour,
text_mode = text_mode,
text_contrast = text_contrast
)

}



3 changes: 0 additions & 3 deletions R/utils-index-lookup.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,5 @@ loc_expr_to_ids <- function(.data, expr, axis) {
stop(axis, " specification must not contain NAs:", deparse(ids))
}


ids
}


Loading

0 comments on commit 36906ec

Please sign in to comment.