Skip to content

Commit d66fa39

Browse files
committed
Initial import from reactR/examples
0 parents  commit d66fa39

18 files changed

+7084
-0
lines changed

Diff for: .Rbuildignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^sparklineswidget\.Rproj$
2+
^\.Rproj\.user$

Diff for: .gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
build/
5+
node_modules/

Diff for: .vscode/tasks.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "Build",
8+
"type": "shell",
9+
"command": "yarn run webpack --mode=development",
10+
"group": {
11+
"kind": "build",
12+
"isDefault": true
13+
}
14+
}
15+
]
16+
}

Diff for: DESCRIPTION

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Package: sparklineswidget
2+
Title: What the Package Does (one line, title case)
3+
Version: 0.0.0.9000
4+
Authors@R: person("First", "Last", email = "[email protected]", role = c("aut", "cre"))
5+
Description: What the package does (one paragraph).
6+
Depends: R (>= 3.5.1)
7+
License: What license is it under?
8+
Encoding: UTF-8
9+
LazyData: true
10+
RoxygenNote: 6.1.1
11+
Imports:
12+
reactR

Diff for: NAMESPACE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(curve)
4+
export(reference_line)
5+
export(renderSparklineswidget)
6+
export(sparklineswidget)
7+
export(sparklineswidgetOutput)
8+
export(spots)
9+
import(htmlwidgets)

Diff for: R/sparklineswidget.R

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#' @export
2+
curve <- function(...) {
3+
reactR::React$SparklinesCurve(...)
4+
}
5+
6+
#' @export
7+
spots <- function(...) {
8+
reactR::React$SparklinesSpots(...)
9+
}
10+
11+
#' @export
12+
reference_line <- function(...) {
13+
reactR::React$SparklinesReferenceLine(...)
14+
}
15+
16+
#' <Add Title>
17+
#'
18+
#' <Add Description>
19+
#'
20+
#' @import htmlwidgets
21+
#'
22+
#' @export
23+
sparklineswidget <- function(data, ...) {
24+
htmlwidgets::createWidget(
25+
'sparklineswidget',
26+
reactR::reactData(reactR::component("Sparklines", c(list(data = data, ...)))),
27+
width = NULL,
28+
height = NULL,
29+
package = 'sparklineswidget',
30+
elementId = NULL
31+
)
32+
}
33+
34+
#' Shiny bindings for sparklineswidget
35+
#'
36+
#' Output and render functions for using sparklineswidget within Shiny
37+
#' applications and interactive Rmd documents.
38+
#'
39+
#' @param outputId output variable to read from
40+
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
41+
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
42+
#' string and have \code{'px'} appended.
43+
#' @param expr An expression that generates a sparklineswidget
44+
#' @param env The environment in which to evaluate \code{expr}.
45+
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
46+
#' is useful if you want to save an expression in a variable.
47+
#'
48+
#' @name sparklineswidget-shiny
49+
#'
50+
#' @export
51+
sparklineswidgetOutput <- function(outputId, width = '100%', height = '400px'){
52+
htmlwidgets::shinyWidgetOutput(outputId, 'sparklineswidget', width, height, package = 'sparklineswidget')
53+
}
54+
55+
#' @rdname sparklineswidget-shiny
56+
#' @export
57+
renderSparklineswidget <- function(expr, env = parent.frame(), quoted = FALSE) {
58+
if (!quoted) { expr <- substitute(expr) } # force quoted
59+
htmlwidgets::shinyRenderWidget(expr, sparklineswidgetOutput, env, quoted = TRUE)
60+
}
61+
62+
# Magical
63+
sparklineswidget_html <- function(id, style, class, ...) {
64+
tagList(
65+
reactR::html_dependency_corejs(),
66+
reactR::html_dependency_react(),
67+
reactR::html_dependency_reacttools(),
68+
tags$span(id = id, class = class)
69+
)
70+
}

Diff for: README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# react-sparklines HTMLWidget
2+
3+
This example demonstrates creating an [htmlwidget](https://www.htmlwidgets.org/) wrapper around the [react-sparklines](http://borisyankov.github.io/react-sparklines/) library.
4+
5+
# Building
6+
7+
Because this package includes Javascript source code that requires a compilation step, package installation is in two phases: Javascript tools build the Javascript, and R tools build and install the package. The R package includes the built Javascript files in the `inst/` directory.
8+
9+
## Javascript Build Requirements
10+
11+
Building Javascript should work on macOS, Linux, and Windows. The following tools are necessary regardless of your platform:
12+
13+
- [Node.js](https://nodejs.org/en/)
14+
- [Yarn](https://yarnpkg.com/en/)
15+
16+
## R Build Requirements
17+
18+
You should install the parent `reactR` package if you haven't, as this widget depends on it.
19+
20+
## Development Workflow
21+
22+
After you've installed Node.js and Yarn, run the following command to resolve and download dependencies:
23+
24+
```
25+
yarn install
26+
```
27+
28+
Now, run `yarn` to build `inst/htmlwidgets/sparklineswidget.js`:
29+
30+
```
31+
yarn run webpack --mode=development
32+
```
33+
34+
> To run `yarn webpack` automatically whenever sources change, use the command `yarn run webpack --mode=development --watch`
35+
36+
Now that the Javascript is built, you can install the R package:
37+
38+
```
39+
devtools::document()
40+
devtools::install()
41+
```
42+
43+
Next, install the `colourpicker` and `shinySignals` packages, as they are used in the example app:
44+
45+
```
46+
install.packages("colourpicker")
47+
devtools::install_github("shinySignals")
48+
```
49+
50+
Finally you can try the example app by running [app.R](app.R).

Diff for: app.R

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
library(shiny)
2+
library(sparklineswidget)
3+
library(colourpicker)
4+
# devtools::install_github("hadley/shinySignals")
5+
library(shinySignals)
6+
7+
ui <- fluidPage(
8+
titlePanel("React Sparklines"),
9+
sparklineswidgetOutput('sparklines'),
10+
colourInput("color_curve", "Curve color", "#253e56"),
11+
colourInput("color_spots", "Spots color", "#56b45d")
12+
)
13+
14+
nextWindow <- function(prev = round(runif(100, 0, 10)), t = NULL) {
15+
c(prev[-1], round(runif(1, 0, 10)))
16+
}
17+
18+
server <- function(input, output, session) {
19+
20+
data <- reducePast(fps(1), nextWindow, nextWindow())
21+
22+
output$sparklines <- renderSparklineswidget(
23+
sparklineswidget(data = data(),
24+
curve(color = input$color_curve),
25+
spots(style = list(fill = input$color_spots)),
26+
reference_line(type = "avg")
27+
)
28+
)
29+
}
30+
31+
shinyApp(ui, server)

0 commit comments

Comments
 (0)