diff --git a/.gitignore b/.gitignore index d44df33..a7305f8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .Rhistory .RData .Ruserdata +inst/doc diff --git a/DESCRIPTION b/DESCRIPTION index 027828e..1ca08a8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,10 +16,13 @@ Imports: miniUI, htmlwidgets Suggests: - dygraphs, - leaflet, - plotly, - xts + dygraphs, + leaflet, + plotly, + xts, + knitr, + rmarkdown License: GPL (>= 2) | file LICENSE LazyData: TRUE RoxygenNote: 5.0.1 +VignetteBuilder: knitr diff --git a/vignettes/manipulateWidgets.Rmd b/vignettes/manipulateWidgets.Rmd new file mode 100644 index 0000000..43e8194 --- /dev/null +++ b/vignettes/manipulateWidgets.Rmd @@ -0,0 +1,220 @@ +--- +title: "Getting started with the manipulateWidget package" +author: "Francois Guillem" +date: "`r Sys.Date()`" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Vignette Title} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +library(manipulateWidget) +``` + +The `manipulateWidget` package is largely inspired by the `manipulate` package from Rstudio. It provides the function ``manipulateWidget` that can be used to create in a very easy and quick way a graphical interface that lets the user modify the data or the parameters of an interactive chart. Technically, the function generates a Shiny gadget, but the user does not even have to know what is Shiny. + +The package also provides the `combineWidgets` function to easily combine multiple interactive charts in a single view. Of course both functions can be used together. + +Here is an example that uses packages `dygraphs` and `plot_ly`. + + + +## Getting started + +The main function of the package is `manipulateWidget`. It takes as argument an expression that generates an interactive chart (and more precisely an htmlwidget object. See http://www.htmlwidgets.org/ if you have never heard about it) and a set of input controls created with functions mwSlider, mwCheckbox... which are used to dynamically change values within the expression. Each time the user modifies the value of a control, the expression is evaluated again and the chart is updated. Consider the following code: + +```{r eval=FALSE} +manipulateWidget( + myPlotFun(country), + country = mwSelect(c("BE", "DE", "ES", "FR")) +) +``` + +It generates a graphical interface with a select input on its left with options "BE", "DE", "ES", "FR". The value of this input is mapped to the variable `country` in the expression. By default, at the beginning the value of `country` will be equal to the first choice of the input. So the function will first execute `myPlotFun("BE")` and the result will be displayed in the main panel of the interface. If the user changes the value to "FR", then the expression `myPlotFun("FR")` is evaluated and the new result is displayed. + +The interface also contains a button "Done". When the user clicks on it, the last chart is returned. It can be stored in a variable, be modified by the user, saved as a html file with saveWidget from package htmlwidgets or converted to a static image file with package `webshot`. + +Of course, one can create as many controls as needed. The interface of the animated example in the introduction was generated with the following code: + +```{r eval=FALSE} +manipulateWidget( + myPlotFun(distribution, range, title), + distribution = mwSelect(choices = c("gaussian", "uniform")), + range = mwSlider(2000, 2100, value = c(2000, 2100), label = "period"), + title = mwText() +) +``` + + +To see all available controls that can be added to the UI, take a look at the list of the functions of the package: + +```{r eval=FALSE} +help(package = "manipulateWidget") +``` + +## Combining widgets + +The `combineWidgets` function gives an easy way to combine interactive charts (like `par(mfrow = c(...))` or `layout` for static plots). To do it, one has simply to pass to the function the widgets to combine. In the next example, we visualize two random time series with dygraphs and combine them. + +```{r combine, warning=FALSE, out.width="100%"} +library(dygraphs) + +plotRandomTS <- function(id) { + dygraph(data.frame(x = 1:10, y = rnorm(10)), main = paste("Random plot", id)) +} + +combineWidgets(plotRandomTS(1), plotRandomTS(2)) +``` + +The functions tries to find the best number of columns and rows. But one can control them with parameters `nrow`and `ncol`. It is also possible to control their relative size with parameters `rowsize` and `colsize`. To achieve complex layouts, it is possible to use nested combined widgets. Here is an example of a complex layout. (Note that this is only a screenshot of the result. In reality, these charts are interactive). + +```{r combine_complex_layout, , out.width="100%"} +combineWidgets( + ncol = 2, colsize = c(2, 1), + plotRandomTS(1), + combineWidgets( + ncol = 1, + plotRandomTS(2), + plotRandomTS(3), + plotRandomTS(4) + ) +) +``` + +Even if the main use of `combineWidgets` is to combine `htmlwidgets`, it can also display text or html tags. It can be useful to include comments in a chart. Moreover it has arguments to add a title and to add some html content in the sides of the chart. + +```{r combine_content, , out.width="100%", out.height=400} +combineWidgets( + plotRandomTS(1), + plotRandomTS(2), + plotRandomTS(3), + plotRandomTS(4), + title = "Four random plots", + header = "Here goes the header content. It can include html code.", + footer = "Here goes the footer content.", + leftCol = "