forked from ignacio82/vizdraws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ignacio Martinez
committed
Nov 19, 2017
0 parents
commit d03cb84
Showing
16 changed files
with
17,411 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^README\.Rmd$ | ||
^README-.*\.png$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Package: IMposterior | ||
Title: What the Package Does (one line, title case) | ||
Version: 0.0.0.9000 | ||
Authors@R: person("First", "Last", email = "[email protected]", role = c("aut", "cre")) | ||
Description: What the package does (one paragraph). | ||
Depends: R (>= 3.4.2) | ||
License: What license is it under? | ||
Encoding: UTF-8 | ||
LazyData: true | ||
Imports: dplyr, | ||
tidyr | ||
RoxygenNote: 6.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: No | ||
SaveWorkspace: No | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: UTF-8 | ||
|
||
RnwWeave: Sweave | ||
LaTeX: pdfLaTeX | ||
|
||
AutoAppendNewline: Yes | ||
StripTrailingWhitespace: Yes | ||
|
||
BuildType: Package | ||
PackageUseDevtools: Yes | ||
PackageInstallArgs: --no-multiarch --with-keep.source | ||
PackageRoxygenize: rd,collate,namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(IMposterior) | ||
export(IMposteriorOutput) | ||
export(renderIMposterior) | ||
import(htmlwidgets) | ||
importFrom(magrittr,"%>%") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#' <Add Title> | ||
#' | ||
#' <Add Description> | ||
#' | ||
#' @import htmlwidgets | ||
#' @importFrom magrittr "%>%" | ||
#' | ||
#' @export | ||
IMposterior <- function(x, MME = 0, threshold = 0.75, colors, | ||
width = NULL, height = NULL, | ||
elementId = NULL) { | ||
# Set colors | ||
|
||
if(missing(colors)){ | ||
colors <- c("#e41a1c", "#377eb8", "#4daf4a") | ||
} | ||
|
||
# Calculate the breaks | ||
|
||
breaks <- if(MME!=0){ | ||
c(-Inf, -MME, MME, Inf) | ||
}else c(-Inf, MME, Inf) | ||
|
||
# Calculate density values for input data | ||
dens <- data.frame(density(x, n=2^10, adjust=1)[c("x","y")]) %>% | ||
dplyr::mutate(section = cut(x, breaks=breaks)) %>% | ||
dplyr::group_by(section) %>% | ||
dplyr::mutate(prob = paste0(round(sum(y)*mean(diff(x))*100),"%")) | ||
|
||
# Get probability mass for each level of section | ||
sp <- dens %>% | ||
dplyr::group_by(section, prob) %>% | ||
dplyr::summarise() %>% | ||
dplyr::ungroup() %>% | ||
tidyr::complete(section, fill=list(prob="0%")) | ||
|
||
# forward options using x | ||
opts = list( | ||
x = dens$x, | ||
y = dens$y, | ||
MME = MME, | ||
threshold = threshold, | ||
prob = sp$prob, | ||
colors = colors | ||
) | ||
|
||
# create widget | ||
htmlwidgets::createWidget( | ||
name = 'IMposterior', | ||
opts, | ||
width = width, | ||
height = height, | ||
package = 'IMposterior', | ||
elementId = elementId | ||
) | ||
} | ||
|
||
#' Shiny bindings for IMposterior | ||
#' | ||
#' Output and render functions for using IMposterior within Shiny | ||
#' applications and interactive Rmd documents. | ||
#' | ||
#' @param outputId output variable to read from | ||
#' @param width,height Must be a valid CSS unit (like \code{'100\%'}, | ||
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a | ||
#' string and have \code{'px'} appended. | ||
#' @param expr An expression that generates a IMposterior | ||
#' @param env The environment in which to evaluate \code{expr}. | ||
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This | ||
#' is useful if you want to save an expression in a variable. | ||
#' | ||
#' @name IMposterior-shiny | ||
#' | ||
#' @export | ||
IMposteriorOutput <- function(outputId, width = '100%', height = '400px'){ | ||
htmlwidgets::shinyWidgetOutput(outputId, 'IMposterior', width, height, package = 'IMposterior') | ||
} | ||
|
||
#' @rdname IMposterior-shiny | ||
#' @export | ||
renderIMposterior <- function(expr, env = parent.frame(), quoted = FALSE) { | ||
if (!quoted) { expr <- substitute(expr) } # force quoted | ||
htmlwidgets::shinyRenderWidget(expr, IMposteriorOutput, env, quoted = TRUE) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
output: | ||
md_document: | ||
variant: markdown_github | ||
--- | ||
|
||
<!-- README.md is generated from README.Rmd. Please edit that file --> | ||
|
||
```{r, echo = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>", | ||
fig.path = "README-" | ||
) | ||
``` | ||
|
||
# IMposterior | ||
|
||
The goal of IMposterior is to ... | ||
|
||
## Example | ||
|
||
This is a basic example which shows you how to solve a common problem: | ||
|
||
```{r example, eval=FALSE} | ||
library(IMposterior) | ||
set.seed(9782) | ||
x <- rnorm(1000) | ||
IMposterior(x= x, MME=1) | ||
``` | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!-- README.md is generated from README.Rmd. Please edit that file --> | ||
IMposterior | ||
=========== | ||
|
||
The goal of IMposterior is to ... | ||
|
||
Example | ||
------- | ||
|
||
This is a basic example which shows you how to solve a common problem: | ||
|
||
``` r | ||
library(IMposterior) | ||
set.seed(9782) | ||
x <- rnorm(1000) | ||
IMposterior(x= x, MME=1) | ||
``` | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
HTMLWidgets.widget({ | ||
|
||
name: 'IMposterior', | ||
|
||
type: 'output', | ||
|
||
factory: function(el, width, height) { | ||
|
||
// TODO: define shared variables for this instance | ||
|
||
return { | ||
|
||
renderValue: function(opts) { | ||
|
||
console.log("MME: ", opts.MME); | ||
console.log("threshold: ", opts.threshold); | ||
console.log("prob: ", opts.prob); | ||
console.log("colors: ", opts.colors); | ||
|
||
|
||
var margin = {left:50,right:50,top:40,bottom:0}; | ||
|
||
var xMax = opts.x.reduce(function(a, b) { | ||
return Math.max(a, b); | ||
|
||
}); | ||
var yMax = opts.y.reduce(function(a, b) { | ||
return Math.max(a, b); | ||
|
||
}); | ||
var xMin = opts.x.reduce(function(a, b) { | ||
return Math.min(a, b); | ||
|
||
}); | ||
var yMin = opts.y.reduce(function(a, b) { | ||
return Math.min(a, b); | ||
|
||
}); | ||
|
||
|
||
var y = d3.scaleLinear() | ||
.domain([0,yMax]) | ||
.range([height,0]); | ||
|
||
var x = d3.scaleLinear() | ||
.domain([xMin,xMax]) | ||
.range([0,width]); | ||
|
||
|
||
var yAxis = d3.axisLeft(y); | ||
var xAxis = d3.axisBottom(x); | ||
|
||
|
||
var area = d3.area() | ||
.x(function(d,i){ return x(opts.x[i]) ;}) | ||
.y0(height) | ||
.y1(function(d){ return y(d); }); | ||
|
||
var svg = d3.select(el).append('svg').attr("height","100%").attr("width","100%"); | ||
var chartGroup = svg.append("g").attr("transform","translate("+margin.left+","+margin.top+")"); | ||
|
||
|
||
chartGroup.append("path") | ||
.attr("d", area(opts.y)); | ||
|
||
|
||
chartGroup.append("g") | ||
.attr("class","axis x") | ||
.attr("transform","translate(0,"+height+")") | ||
.call(xAxis); | ||
|
||
|
||
}, | ||
|
||
resize: function(width, height) { | ||
|
||
// TODO: code to re-render the widget with a new size | ||
|
||
} | ||
|
||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# (uncomment to add a dependency) | ||
dependencies: | ||
- name: D3 | ||
version: 4 | ||
src: htmlwidgets/lib/D3 | ||
script: d3.v4.js | ||
stylesheet: style.css |
Oops, something went wrong.