Skip to content

Instantly share code, notes, and snippets.

@mahzad
Forked from timelyportfolio/graph.js
Created March 15, 2013 09:22
Show Gist options
  • Save mahzad/5168567 to your computer and use it in GitHub Desktop.
Save mahzad/5168567 to your computer and use it in GitHub Desktop.
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<script src="http://d3js.org/d3.v2.js"></script>
<script type="text/javascript">var networkOutputBinding = new Shiny.OutputBinding();
$.extend(networkOutputBinding, {
find: function(scope) {
return $(scope).find('.shiny-network-output');
},
renderValue: function(el, data) {
//format nodes object
var nodes = new Array();
for (var i = 0; i < data.names.length; i++){
nodes.push({"name": data.names[i]})
}
var width = 800;
var height = 600;
var lin = data.links
var force = d3.layout.force()
.nodes(nodes)
.links(lin)
.charge(-120)
.linkDistance(30)
.size([width, height])
.start();
//remove the old graph
var svg = d3.select(el).select("svg");
svg.remove();
$(el).html("");
//append a new one
svg = d3.select(el).append("svg");
svg.attr("width", width)
.attr("height", height);
var link = svg.selectAll("line.link")
.data(lin)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll("circle.node")
.data(nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
//.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
}
});
Shiny.outputBindings.register(networkOutputBinding, 'trestletech.networkbinding');
</script>
#almost entirely based on the 02_text and 03_mpg examples provided by RStudio Shiny
#all credit belongs to them
if (!require(PerformanceAnalytics)) {
stop("This app requires the PerformanceAnalytics package. To install it, run 'install.packages(\"PerformanceAnalytics\")'.\n")
}
if (!require(quantmod)) {
stop("This app requires the quantmod package. To install it, run 'install.packages(\"quantmod\")'.\n")
}
# Download data for a stock, if needed
require_symbol <- function(symbol) {
if (!exists(symbol))
getSymbols(symbol, src="FRED")
#getSymbols(symbol, from = "1900-01-01")
}
library(shiny)
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
make_chart <- function(symbol="SP500") {
# get price data if does not exist
require_symbol(symbol)
#would hope not to recalculate each time but for now will leave messy
price.monthly <- to.monthly(get(symbol))[,4]
ret.monthly <- ROC(price.monthly, type="discrete", n=1)
#calculate system returns
systemRet <- merge(
ifelse(lag(price.monthly > runMean(price.monthly, n=input$nmonths), k=1), 1, 0) * ret.monthly,
ret.monthly)
colnames(systemRet) <- c(paste(input$nmonths,"MASys",sep=""), symbol)
charts.PerformanceSummary(systemRet, ylog=TRUE)
}
make_table <- function(symbol="SP500") {
# get price data if does not exist
require_symbol(symbol)
#would hope not to recalculate each time but for now will leave messy
price.monthly <- to.monthly(get(symbol))[,4]
ret.monthly <- ROC(price.monthly, type="discrete", n=1)
#calculate system returns
systemRet <- merge(
ifelse(lag(price.monthly > runMean(price.monthly, n=input$nmonths), k=1), 1, 0) * ret.monthly,
ret.monthly)
colnames(systemRet) <- c(paste(input$nmonths,"MASys",sep=""), symbol)
table.Stats(systemRet)
}
# Generate a plot of the system and buy/hold benchmark given nmonths parameter
# include outliers if requested
output$systemPlot <- reactivePlot(function() {
make_chart()
})
# Generate a summary stats table of the dataset
output$json <- reactivePrint(function() {
make_table()
})
})
#almost entirely based on the 02_text and 03_mpg examples provided by RStudio Shiny
#all credit belongs to them
library(shiny)
# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Shiny Moving Average Parameter Test"),
# Sidebar with controls to select a dataset and specify the number
# of observations to view
sidebarPanel(
numericInput("nmonths", "Number of months for moving average:", 10)
),
# Show a summary of the dataset and an HTML table with the requested
# number of observations
mainPanel(
plotOutput("systemPlot"),
verbatimTextOutput("json")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment