Skip to content

Commit 8379805

Browse files
authored
Docs site (#20)
* Update * Docs * Rendering of static widget in pkgdown page * Update * Update vignette * Update * Update deploy.yml
1 parent 89e8ccf commit 8379805

17 files changed

+749
-49
lines changed

.github/workflows/deploy.yml

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: R
2+
3+
on: [push, pull_request]
4+
5+
6+
permissions:
7+
contents: read
8+
pages: write
9+
id-token: write
10+
11+
jobs:
12+
pre_deploy:
13+
runs-on: ubuntu-latest
14+
env:
15+
cache-version: 5
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Set up libraries for Ubuntu
19+
run: |
20+
sudo apt-get update
21+
sudo apt-get install -y libsodium-dev libharfbuzz-dev libfribidi-dev libcurl4-openssl-dev texlive-latex-base texlive-fonts-extra pandoc libmagick++-dev libhdf5-dev
22+
- name: Set up R 4.0
23+
uses: r-lib/actions/setup-r@v2
24+
with:
25+
r-version: 4.0
26+
- name: Set CRAN mirror
27+
run: |
28+
cat("\noptions(repos=structure(c(CRAN=\"https://cran.rstudio.com\")))\n", file = "~/.Rprofile", append = TRUE)
29+
shell: Rscript {0}
30+
- name: Get R and OS version
31+
id: get-version
32+
run: |
33+
cat("::set-output name=os-version::", sessionInfo()$running, "\n", sep = "")
34+
cat("::set-output name=r-version::", R.Version()$version.string, "\n", sep = "")
35+
cat("::endgroup::\n")
36+
shell: Rscript {0}
37+
- name: Cache dependencies
38+
id: cache-deps
39+
uses: actions/cache@v2
40+
with:
41+
path: ${{ env.R_LIBS_USER }}/*
42+
key: ${{ hashFiles('DESCRIPTION') }}-${{ steps.get-version.outputs.os-version }}-${{ steps.get-version.outputs.r-version }}-${{ env.cache-version }}-deps
43+
- name: Install dependencies
44+
if: steps.cache-deps.outputs.cache-hit != 'true'
45+
run: |
46+
install.packages(c("devtools", "remotes", "rcmdcheck", "covr"))
47+
remotes::install_deps(dependencies = TRUE)
48+
shell: Rscript {0}
49+
env:
50+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
51+
- name: rcmdcheck
52+
run: |
53+
rcmdcheck::rcmdcheck(
54+
error_on = "error", # TODO: switch back to "warning"
55+
check_dir = "check"
56+
)
57+
shell: Rscript {0}
58+
env:
59+
_R_CHECK_FORCE_SUGGESTS_: false
60+
- name: Run coverage report
61+
run: |
62+
covr::package_coverage()
63+
shell: Rscript {0}
64+
#- name: Downgrade pkgdown
65+
# run: |
66+
# remotes::install_version("pkgdown", "2.0.3")
67+
# shell: Rscript {0}
68+
- name: Build docs
69+
run: |
70+
Rscript -e 'devtools::document(); pkgdown::build_site(new_process = FALSE)'
71+
touch docs/.nojekyll
72+
- uses: actions/upload-pages-artifact@v1
73+
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
74+
with:
75+
path: ./docs
76+
77+
deploy:
78+
runs-on: ubuntu-latest
79+
needs: pre_deploy
80+
environment:
81+
name: github-pages
82+
url: ${{ steps.deployment.outputs.page_url }}
83+
steps:
84+
- name: Deploy to GitHub Pages
85+
id: deployment
86+
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
87+
uses: actions/deploy-pages@v1

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.Rhistory
22
.Rproj.user/
3-
.DS_Store
3+
.DS_Store
4+
docs/

DESCRIPTION

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
Package: anyhtmlwidget
22
Type: Package
3-
Title: What the Package Does (Title Case)
3+
Title: Create interactive widgets using EcmaScript Modules
44
Version: 0.1.0
5-
Author: Who wrote it
6-
Maintainer: The package maintainer <yourself@somewhere.net>
7-
Description: More about what it does (maybe more than one line)
8-
Use four spaces when indenting paragraphs within the Description.
5+
Authors@R: c(
6+
person(
7+
given = "Mark",
8+
family = "Keller",
9+
email = "mark_keller@g.harvard.edu",
10+
role = c("cre", "aut"),
11+
comment = c(ORCID = "0000-0003-3003-874X")
12+
)
13+
)
14+
Description: Anyhtmlwidget brings concepts from
15+
AnyWidget to R.
916
License: MIT + file LICENSE
10-
Encoding: UTF-8
11-
LazyData: false
17+
BugReports: https://github.com/keller-mark/anyhtmlwidget/issues
18+
URL: https://github.com/keller-mark/anyhtmlwidget
19+
Depends: R (>= 4.0.0)
1220
Imports:
13-
htmlwidgets,
14-
R6,
15-
shiny,
16-
httpuv,
17-
jsonlite
21+
htmlwidgets,
22+
R6,
23+
shiny,
24+
httpuv,
25+
jsonlite
26+
Encoding: UTF-8
27+
LazyData: true
28+
Roxygen: list(markdown = TRUE)
1829
RoxygenNote: 7.3.1
30+
Suggests:
31+
testthat,
32+
bslib,
33+
pkgdown,
34+
knitr,
35+
rmarkdown

NAMESPACE

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
exportPattern("^[[:alpha:]]+")
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(AnyHtmlWidget)
4+
export(widgetServer)
5+
export(widgetUI)

R/widget.R

+81-11
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ the_anyhtmlwidget <- function(esm, values = NULL, ns_id = NULL, width = NULL, he
3535
)
3636
}
3737

38-
#' Shiny bindings for anyhtmlwidget
39-
#'
40-
#' Output and render functions for using anyhtmlwidget within Shiny
41-
#' applications and interactive Rmd documents.
38+
#' Internal Shiny UI binding for anyhtmlwidget.
4239
#'
4340
#' @param output_id output variable to read from
4441
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
@@ -55,15 +52,21 @@ anyhtmlwidget_output <- function(output_id, width = '100%', height = '400px'){
5552
htmlwidgets::shinyWidgetOutput(output_id, 'anyhtmlwidget', width, height, package = 'anyhtmlwidget')
5653
}
5754

58-
#' @name anyhtmlwidget-shiny
59-
#' @return The Shiny server output.
55+
#' Internal Shiny server binding for anyhtmlwidget.
6056
#'
6157
#' @keywords internal
6258
render_anyhtmlwidget <- function(expr, env = parent.frame(), quoted = FALSE) {
6359
if (!quoted) { expr <- substitute(expr) } # force quoted
6460
htmlwidgets::shinyRenderWidget(expr, anyhtmlwidget_output, env, quoted = TRUE)
6561
}
6662

63+
#' AnyHtmlWidget
64+
#' @title AnyHtmlWidget Class
65+
#' @docType class
66+
#' @description
67+
#' Class representing a widget.
68+
#'
69+
#' @rdname AnyHtmlWidget
6770
#' @export
6871
AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
6972
lock_objects = FALSE,
@@ -81,10 +84,16 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
8184
.width = NULL,
8285
.height = NULL
8386
),
84-
active = list(
85-
86-
),
87+
active = list(),
8788
public = list(
89+
#' @description
90+
#' Create a new widget instance.
91+
#' @param .esm The EcmaScript module as a string.
92+
#' @param .mode The widget mode.
93+
#' @param .width The widget width. Optional.
94+
#' @param .height The widget height. Optional.
95+
#' @param .commands TODO
96+
#' @param ... All other named arguments will be used to create active bindings on the instance.
8897
initialize = function(.esm, .mode, .width = NA, .height = NA, .commands = NA, ...) {
8998
private$esm <- .esm
9099
private$values <- list(...)
@@ -126,56 +135,100 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
126135
}
127136
self$`.__enclos_env__`$`.__active__` <- active_env
128137
},
138+
#' @description
139+
#' Set a value.
140+
#' @param key The key of the value to set.
141+
#' @param val The new value.
142+
#' @param emit_change Should the on_change handler be called?
129143
set_value = function(key, val, emit_change = TRUE) {
130144
private$values[[key]] <- val
131145
if(emit_change && !is.null(private$change_handler)) {
132146
# Should this only call the callback if the current value is different than the new value?
133147
private$change_handler(key, val)
134148
}
135149
},
150+
#' @description
151+
#' Register a change handler to call if emit_change is TRUE in set_value.
152+
#' @param callback A callback function to register.
136153
on_change = function(callback) {
137154
private$change_handler <- callback
138155
},
156+
#' @description
157+
#' Get a particular value.
158+
#' @param key The key of the value to get.
159+
#' @returns The value.
139160
get_value = function(key) {
140161
return(private$values[[key]])
141162
},
163+
#' @description
164+
#' Get the ESM string.
165+
#' @returns The ESM string.
142166
get_esm = function() {
143167
return(private$esm)
144168
},
169+
#' @description
170+
#' Get all widget values
171+
#' @returns List of values.
145172
get_values = function() {
146173
return(private$values)
147174
},
175+
#' @description
176+
#' Get the widget width.
177+
#' @returns The width.
148178
get_width = function() {
149179
return(private$.width)
150180
},
181+
#' @description
182+
#' Get the widget height.
183+
#' @returns The height.
151184
get_height = function() {
152185
return(private$.height)
153186
},
187+
#' @description
188+
#' Set all values. TODO: is this ever used?
189+
#' @param new_values A list of new values.
154190
set_values = function(new_values) {
155191
private$values <- new_values
156192
},
193+
#' @description
194+
#' Set the widget mode.
195+
#' @param mode The new widget mode.
157196
set_mode = function(mode) {
158197
if(!mode %in% c("static", "gadget", "shiny", "dynamic")) {
159198
stop("Invalid widget mode.")
160199
}
161200
private$mode <- mode
162201
},
202+
#' @description
203+
#' Start the server, if not running.
163204
start_server = function() {
164205
if(is.null(private$server)) {
165206
private$server <- start_server(self, host = private$server_host, port = private$server_port)
166207
}
167208
},
209+
#' @description
210+
#' Stop the server, if running.
168211
stop_server = function() {
169212
if(!is.null(private$server)) {
170213
private$server$stop()
171214
}
172215
},
216+
#' @description
217+
#' Get the server hostname.
218+
#' @return The hostname as a string.
173219
get_host = function() {
174220
return(private$server_host)
175221
},
222+
#' @description
223+
#' Get the server port.
224+
#' @returns The port number.
176225
get_port = function() {
177226
return(private$server_port)
178227
},
228+
#' @description
229+
#' Custom print function for the R6 class.
230+
#' If mode is "shiny", falls back to original R6 print behavior.
231+
#' Otherwise, renders the widget.
179232
print = function() {
180233
if(private$mode == "shiny") {
181234
# If Shiny mode, we just want to use the original R6 print behavior.
@@ -186,6 +239,8 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
186239
self$render()
187240
}
188241
},
242+
#' @description
243+
#' Render the widget.
189244
render = function() {
190245
if(private$mode == "static") {
191246
invoke_static(self)
@@ -200,6 +255,7 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
200255
)
201256
)
202257

258+
#' @keywords internal
203259
invoke_static <- function(w) {
204260
w <- the_anyhtmlwidget(
205261
esm = w$get_esm(),
@@ -210,6 +266,7 @@ invoke_static <- function(w) {
210266
print(w)
211267
}
212268

269+
#' @keywords internal
213270
invoke_dynamic <- function(w) {
214271
w$start_server()
215272
w <- the_anyhtmlwidget(
@@ -223,6 +280,7 @@ invoke_dynamic <- function(w) {
223280
print(w)
224281
}
225282

283+
#' @keywords internal
226284
invoke_gadget <- function(w) {
227285
ui <- shiny::tagList(
228286
anyhtmlwidget_output(output_id = "my_widget", width = '100%', height = '100%')
@@ -261,13 +319,25 @@ invoke_gadget <- function(w) {
261319
shiny::runGadget(ui, server)
262320
}
263321

264-
# Shiny module UI
322+
#' Shiny module UI for anyhtmlwidgets.
323+
#'
324+
#' @param id The output ID.
325+
#' @param width The widget width. Optional.
326+
#' @param height The widget height. Optional.
327+
#'
328+
#' @export
265329
widgetUI <- function(id, width = '100%', height = '400px') {
266330
ns <- shiny::NS(id)
267331
anyhtmlwidget_output(output_id = ns("widget"), width = width, height = height)
268332
}
269333

270-
# Shiny module server
334+
#' Shiny module server for anyhtmlwidgets.
335+
#'
336+
#' @param id The matching output ID used in the Shiny UI.
337+
#' @param w The widget instance.
338+
#' @returns reactiveValues corresponding to the widget's active bindings.
339+
#'
340+
#' @export
271341
widgetServer <- function(id, w) {
272342
ns <- shiny::NS(id)
273343
shiny::moduleServer(

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Bringing core concepts from [anywidget](https://github.com/manzt/anywidget) to R
66
- Access state from JS using the same AnyWidget `model` API.
77
- Bidirectional communication (R <-> JS)
88

9-
Note: this is currently an experiment
10-
119
## Installation
1210

1311
```R

0 commit comments

Comments
 (0)