library(shiny)
library(shinydashboard)
library(rCharts)
library(RCurl)
library(networkD3)
data(MisLinks)
data(MisNodes)
URL <- "https://raw.githubusercontent.com/christophergandrud/d3Network/sankey/JSONdata/energy.json"
Energy <- getURL(URL, ssl.verifypeer = FALSE)
EngLinks <- JSONtoDF(jsonStr = Energy, array = "links")
EngNodes <- JSONtoDF(jsonStr = Energy, array = "nodes")
ui <- dashboardPage(
dashboardHeader(title = "Test"),
dashboardSidebar(
sidebarMenu(
menuItem("A rChart Multibar", tabName = "rChart-multibar"),
menuItem("B networkD3 Sankey", tabName = "networkD3-sankey"),
menuItem("C networkD3 Force", tabName = "networkD3-force")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "rChart-multibar", h2("A rChart")
# comment out the following line, then the Sankey will work
,showOutput("plot_nvd3", "nvd3")
),
tabItem(tabName = "networkD3-sankey",h2("B networkD3 Sankey")
,sankeyNetworkOutput("networkPlot")
),
tabItem(tabName = "networkD3-force",h2("C networkD3 Force")
,forceNetworkOutput("force")
)
)
)
)
server <- function(input, output) {
output$plot_nvd3 = renderChart({
### Works in the browser, but not in the viewer window
hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
nvd3_chart <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male,
type = 'multiBarChart')
nvd3_chart$addParams(width = 800, height = 400, dom = "plot_nvd3")
return(nvd3_chart)
})
output$networkPlot <- renderSankeyNetwork({
sankeyNetwork(Links = EngLinks, Nodes = EngNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
width = 700, fontsize = 12, nodeWidth = 30)
})
output$force <- renderForceNetwork({
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group")
})
}
shinyApp(ui, server)