Skip to content

Commit e42194c

Browse files
experiment with htmlwidget, improve README.md, and create some examples
1 parent d7a3a04 commit e42194c

File tree

13 files changed

+357
-5
lines changed

13 files changed

+357
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.Rhistory
33
.RData
44
.Ruserdata
5+
inst/doc

DESCRIPTION

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ BugReports: https://github.com/timelyportfolio/vueR/issues
2424
License: MIT + file LICENSE
2525
LazyData: TRUE
2626
Imports:
27-
htmltools
27+
htmltools,
28+
htmlwidgets (>= 0.6.0)
2829
Suggests:
29-
htmlwidgets (>= 0.6.0),
30+
knitr,
3031
rmarkdown,
31-
shiny,
32-
knitr
32+
shiny
3333
RoxygenNote: 5.0.1
3434
VignetteBuilder: knitr

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

NAMESPACE

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(html_dependency_vue)
4+
export(renderVue)
5+
export(vue)
6+
export(vueOutput)
7+
import(htmlwidgets)
48
importFrom(htmltools,htmlDependency)

R/vue.R

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#' 'Vue.js' 'htmlwidget'
2+
#'
3+
#' Use 'Vue.js' with the convenience and flexibility of 'htmlwidgets'.
4+
#' \code{vue} is a little different from other 'htmlwidgets' though
5+
#' since it requires specification of the HTML tags/elements separately.
6+
#'
7+
#' @import htmlwidgets
8+
#'
9+
#' @export
10+
#' @example ./inst/examples/vue_widget_examples.R
11+
#' @return vue htmlwidget
12+
13+
vue <- function(app = list(), width = NULL, height = NULL, elementId = NULL) {
14+
15+
# forward options using x
16+
x = app
17+
18+
# create widget
19+
hw <- htmlwidgets::createWidget(
20+
name = 'vue',
21+
x,
22+
width = width,
23+
height = height,
24+
package = 'vueR',
25+
elementId = elementId
26+
)
27+
28+
hw$dependencies <- list(
29+
html_dependency_vue(offline=TRUE)
30+
)
31+
32+
hw
33+
}
34+
35+
#' Shiny bindings for vue
36+
#'
37+
#' Output and render functions for using vue within Shiny
38+
#' applications and interactive Rmd documents.
39+
#'
40+
#' @param outputId output variable to read from
41+
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
42+
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
43+
#' string and have \code{'px'} appended.
44+
#' @param expr An expression that generates a vue
45+
#' @param env The environment in which to evaluate \code{expr}.
46+
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
47+
#' is useful if you want to save an expression in a variable.
48+
#'
49+
#' @name vue-shiny
50+
#'
51+
#' @export
52+
vueOutput <- function(outputId, width = '100%', height = '400px'){
53+
htmlwidgets::shinyWidgetOutput(outputId, 'vue', width, height, package = 'vueR')
54+
}
55+
56+
#' @rdname vue-shiny
57+
#' @export
58+
renderVue <- function(expr, env = parent.frame(), quoted = FALSE) {
59+
if (!quoted) { expr <- substitute(expr) } # force quoted
60+
htmlwidgets::shinyRenderWidget(expr, vueOutput, env, quoted = TRUE)
61+
}

README.Rmd

+43-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,48 @@ knitr::opts_chunk$set(
1414

1515
[![Travis-CI Build Status](https://travis-ci.org/timelyportfolio/vuer.svg?branch=master)](https://travis-ci.org/timelyportfolio/vueR)[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/vueR)](https://cran.r-project.org/package=vueR)
1616

17+
[Vue.js](https://vuejs.org) is a quiet, very popular JavaScript framework with an impressive set of features, a solid community, and MIT license. Don't tell anybody, but I think I might even like it better than React. With all this, Vue deserves its own set of helpers for `R`, just like [`d3r`](https://github.com/d3r) and [`reactR`](https://github.com/reactR).
18+
19+
`vueR` provides these helpers with its dependency function `html_dependency_vue` and ?htmlwidget?.
20+
21+
22+
### Installation
23+
24+
`vueR` aims to achieve CRAN status, but for now, it only exists on Github.
25+
26+
```
27+
devtools::install_github("timelyportfolio/vueR")
28+
```
29+
30+
### Example
31+
32+
We'll start with a recreation of the simple "Hello World" example from the Vue.js documentation.
33+
34+
```{r eval=FALSE}
35+
library(htmltools)
36+
library(vueR)
37+
38+
browsable(
39+
attachDependencies(
40+
tagList(
41+
tags$div(id="app","{{message}}"),
42+
tags$script(
43+
"
44+
var app = new Vue({
45+
el: '#app',
46+
data: {
47+
message: 'Hello Vue!'
48+
}
49+
});
50+
"
51+
)
52+
),
53+
html_dependency_vue()
54+
)
55+
)
56+
```
57+
58+
1759
### Code of Conduct
1860

19-
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.
61+
I would love for you to participate and help with `vueR`, but please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22
<!-- README.md is generated from README.Rmd. Please edit that file -->
33
[![Travis-CI Build Status](https://travis-ci.org/timelyportfolio/vuer.svg?branch=master)](https://travis-ci.org/timelyportfolio/vueR)[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/vueR)](https://cran.r-project.org/package=vueR)
44

5+
[Vue.js](https://vuejs.org) is a quiet, very popular JavaScript framework with an impressive set of features, a solid community, and MIT license. Don't tell anybody, but I think I might even like it better than React. With all this, Vue deserves its own set of helpers for `R`, just like [`d3r`](https://github.com/d3r) and [`reactR`](https://github.com/reactR).
6+
7+
`vueR` provides these helpers with its dependency function `html_dependency_vue` and ?htmlwidget?.
8+
9+
### Installation
10+
11+
`vueR` aims to achieve CRAN status, but for now, it only exists on Github.
12+
13+
devtools::install_github("timelyportfolio/vueR")
14+
15+
### Example
16+
17+
We'll start with a recreation of the simple "Hello World" example from the Vue.js documentation.
18+
19+
``` r
20+
library(htmltools)
21+
library(vueR)
22+
23+
attachDependencies(
24+
tagList(
25+
tags$div(id="app","{{message}}"),
26+
tags$script(
27+
"
28+
var app = new Vue({
29+
el: '#app',
30+
data: {
31+
message: 'Hello Vue!'
32+
}
33+
});
34+
"
35+
)
36+
),
37+
html_dependency_vue()
38+
)
39+
```
40+
541
### Code of Conduct
642

743
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.

inst/examples/vue_widget_examples.R

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
\dontrun{
2+
library(vueR)
3+
library(htmltools)
4+
5+
# recreate Hello Vue! example
6+
browsable(
7+
tagList(
8+
tags$div(id="app", "{{message}}"),
9+
vue(
10+
list(
11+
el = "#app",
12+
data = list(
13+
message = "Hello Vue!"
14+
)
15+
)
16+
)
17+
)
18+
)
19+
20+
# app2 from Vue.js introduction
21+
browsable(
22+
tagList(
23+
tags$div(id="app-2",
24+
tags$span(
25+
"v-bind:title" = "message",
26+
"Hover your mouse over me for a few seconds to see my dynamically bound title!"
27+
)
28+
),
29+
vue(
30+
list(
31+
el = "#app-2",
32+
data = list(
33+
message = htmlwidgets::JS(
34+
"'You loaded this page on ' + new Date()"
35+
)
36+
)
37+
)
38+
)
39+
)
40+
)
41+
}

inst/htmlwidgets/vue.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
HTMLWidgets.widget({
2+
3+
name: 'vue',
4+
5+
type: 'output',
6+
7+
factory: function(el, width, height) {
8+
9+
var instance;
10+
11+
return {
12+
13+
instance: instance,
14+
15+
renderValue: function(x) {
16+
// if x.el is specified then use it and
17+
// hide our htmlwidget container
18+
if(x.el) {
19+
el.style.display = "none";
20+
} else {
21+
// x.el not specified so use our htmlwidget el
22+
// container for our Vue app but this will
23+
// will probably not work as expected
24+
// since the tag is just a div
25+
x.el = "#" + el.id;
26+
}
27+
28+
instance = new Vue(x);
29+
30+
},
31+
32+
resize: function(width, height) {
33+
34+
// for now ignore resize hopefully without
35+
// much consequence
36+
37+
}
38+
39+
};
40+
}
41+
});

inst/htmlwidgets/vue.yaml

Whitespace-only changes.

man/vue-shiny.Rd

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/vue.Rd

+60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/Intro to vueR.Rmd

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: "Intro to vueR"
3+
author: "Kenton Russell"
4+
date: "`r Sys.Date()`"
5+
output: rmarkdown::html_vignette
6+
vignette: >
7+
%\VignetteIndexEntry{Intro to vueR}
8+
%\VignetteEngine{knitr::rmarkdown}
9+
%\VignetteEncoding{UTF-8}
10+
---
11+
12+
All the awesomeness of [Vue.js](https://vuejs.org) is available in `R` with `vueR`. We will work through some of the examples to give you a feel for both `Vue.js` and how to incorporate this powerful binding framework in your `R` workflows.
13+

0 commit comments

Comments
 (0)