金子邦彦研究室研究道具箱と教材オープンデータshiny + HTML

shiny + HTML

前準備

前準備として、shiny パッケージをインストールしておくこと.

R システムで次のように操作する.

install.packages("shiny") 

[image]

Shiny を使ってみる

drawing graph

d3Network を使い tree や graph を HTML や SVG に描画 install.packages("d3Network") [image] https://christophergandrud.github.io/d3Network/#forceDirect に記載 library(d3Network) Source <- c("A", "A", "A", "A", "B", "B", "C", "C", "D") Target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I") NetworkData <- data.frame(Source, Target) # Create graph d3SimpleNetwork(file="hoge.html", NetworkData, height = 300, width = 700, fontsize = 15) [image] [image] library(shiny) shinyUI(fluidPage( # Load D3.js tags$head( tags$script(src = 'http://d3js.org/d3.v3.min.js') ), # Application title titlePanel('d3Network Shiny Example'), p('This is an example of using', a(href = 'https://christophergandrud.github.io/d3Network/', 'd3Network'), 'with', a(href = 'http://shiny.rstudio.com/', 'Shiny', 'web apps.') ), # Sidebar with a slider input for node opacity sidebarLayout( sidebarPanel( sliderInput('slider', label = 'Choose node opacity', min = 0, max = 1, step = 0.01, value = 0.5 ) ), # Show network graph mainPanel( htmlOutput('networkPlot') ) ) )) And the server.R: # Load packages library(RCurl) library(d3Network) # Load data once URL <- "https://raw.githubusercontent.com/christophergandrud/d3Network/master/JSONdata/miserables.json" MisJson <- getURL(URL, ssl.verifypeer = FALSE) # Convert JSON arrays into data frames MisLinks <- JSONtoDF(jsonStr = MisJson, array = "links") MisNodes <- JSONtoDF(jsonStr = MisJson, array = "nodes") # Create individual ID MisNodes$ID <- 1:nrow(MisNodes) #### Shiny #### shinyServer(function(input, output) { output$networkPlot <- renderPrint({ d3ForceNetwork(Nodes = MisNodes, Links = MisLinks, Source = "source", Target = "target", Value = "value", NodeID = "name", Group = "group", width = 400, height = 500, opacity = input$slider, standAlone = FALSE, parentElement = '#networkPlot') }) })

フォームの種類

/tmp/www/index.html の次の部分を書き換えて実行してみる

  <p>
  <form>
    <input type="number" name="n" value="500" min="1" max="1000" />
  </form>
  </p>

jQuery との連携

フォーム部品にフォーカスされたとき色付け

参考Webページ

https://shiny.rstudio.com/tutorial