Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignacio Martinez committed Nov 19, 2017
0 parents commit d03cb84
Show file tree
Hide file tree
Showing 16 changed files with 17,411 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
^.*\.Rproj$
^\.Rproj\.user$
^README\.Rmd$
^README-.*\.png$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Rproj.user
.Rhistory
.RData
12 changes: 12 additions & 0 deletions DESCRIPTION
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
21 changes: 21 additions & 0 deletions IMposterior.Rproj
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
7 changes: 7 additions & 0 deletions NAMESPACE
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,"%>%")
84 changes: 84 additions & 0 deletions R/mywidget.R
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)
}
32 changes: 32 additions & 0 deletions README.Rmd
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)
```

![Posterior distribution](./posterior.png)
19 changes: 19 additions & 0 deletions README.md
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)
```

![Posterior distribution](./posterior.png)
83 changes: 83 additions & 0 deletions inst/htmlwidgets/IMposterior.js
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

}

};
}
});
7 changes: 7 additions & 0 deletions inst/htmlwidgets/IMposterior.yaml
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
Loading

0 comments on commit d03cb84

Please sign in to comment.