Rでコマンド:htmlファイルにボタンを描写するコマンドの紹介

Rの解析に役に立つ記事
スポンサーリンク

githubで紹介されていた、htmlファイルにボタンを描写するコマンドの紹介です。コマンドにはButtonMaker、DivMaker、TileMakerが収録されています。

・TileMaker
 https://github.com/mexindian/TileMaker

各コマンド内容が参考になるので紹介します。

実行コマンドはwindows 7およびOS X 10.11.2のR version 3.2.3で確認しています。

スポンサーリンク

実行コマンド

詳細はコメント、コマンドのヘルプを確認してください。

#コマンドの読み込み
source("https://raw.githubusercontent.com/mexindian/TileMaker/master/tilemaker.R")

#ボタンを作成:ButtonMakerコマンド
Button1 <- ButtonMaker(Color = 2, Value = "&#12363;&#12425;&#12384;&#12395;", Subtitle = "&#12363;&#12425;&#12384;&#12395;")
Button2 <- ButtonMaker(Color = 3, Value = "&#12356;&#12356;&#12418;&#12398;", Subtitle = "&#12356;&#12356;&#12418;&#12398;")
Button3 <- ButtonMaker(Color = 4, Value = 429, Subtitle = "16/03/01&#12398;&#32207;&#35352;&#20107;&#25968;")
Button4 <- ButtonMaker(Color = 5, Size = 1, Value = 1, Subtitle = "&#27598;&#26085;&#26356;&#26032;&#35352;&#20107;&#25968;")
#&#25351;&#23450;&#12375;&#12383;&#12508;&#12479;&#12531;&#20869;&#23481;&#12391;div&#35201;&#32032;&#12434;&#20316;&#25104;:DivMaker&#12467;&#12510;&#12531;&#12489;
Div1 <- DivMaker(Title = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;", Buttons = paste(Button1, Button2))
Div2 <- DivMaker(Title = "&#12394;&#12435;&#12392;&#12363;&#12289;&#27598;&#26085;&#26356;&#26032;&#20013;", Buttons = paste(Button3, Button4))

#&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12408;html&#12501;&#12449;&#12452;&#12523;&#12434;&#20986;&#21147;:TileMaker&#12467;&#12510;&#12531;&#12489;
TileMaker(MainTitle = "", Divs = paste(Div1, Div2), FileName = "TEST.html")

#&#20986;&#21147;&#12375;&#12383;html&#12501;&#12449;&#12452;&#12523;&#12434;&#12502;&#12521;&#12454;&#12470;&#12391;&#34920;&#31034;:browseURL
browseURL("TEST.html")

#&#12467;&#12540;&#12489;&#20869;&#23481;&#12434;&#32057;&#20171;
#ButtonMaker
function(Color=1,Size=4,Value,Subtitle="",Link="",Icon="", Units="",
         Target=0,ThresholdHigh=0,ThresholdLow=0, Hover=""){
  ## colors
  colorList = c("success",  "warning", "danger", "info", "primary", "default")
  
  ## sizes:
  SizeList = c("xs","sm","md","lg")
  
  ## for icons, goto http://getbootstrap.com/components/
  
  paste(paste('<',
              if(Link !=""){paste('a href="',Link,'" role="button" ',sep='')} else{'button'},
              ' type="button" class="btn ',sep=''),
        if(Target ==0){
          paste('btn-',colorList[Color],sep='')
        } else {
          Perc <- Value/Target *100
          if(Perc > ThresholdHigh){
            'btn-success'
          } else if(Perc< ThresholdLow){
            'btn-danger'
          } else {
            'btn-warning'
          }
        },
        paste(' btn-', SizeList[Size],
              '"',
              if(Hover !=""){paste(' title="',Hover,'" ')},
              '><h1>',sep=''),
        if(Icon !=""){paste(' <span class="',Icon,'" aria-hidden="true"></span> ',sep='')},
        if(Units == ""){Value} else {paste(Value,Units,sep="")},
        '</h1>',
        Subtitle,
        if(Link !=""){'</a>'} else{'</button>'},
        sep="")
}

#DivMaker
function(Title="",Buttons){
  paste('<div class="container"><h2>',
        Title,
        '</h2>',
        Buttons,
        '</div>',sep="")
}

#TileMaker
function(MainTitle="",Divs,FileName,ShowDate=FALSE,localCSS=FALSE){
  cat('<!DOCTYPE html><html lang="en"><head>
      <meta name="viewport" content="width=device-width, initial-scale=1">',
      if(localCSS==TRUE){'<link rel="stylesheet" href="bootstrap.min.css">'
      } else {'<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">'},
      '</head><body><h1>',
      MainTitle,
      '</h1>',
      if(ShowDate){paste('<h2>Report Date: ',Sys.Date(),'</h2>',sep="")},
      Divs,
      '</body></html>',
      file=FileName,sep="")
}

出力結果

TileMaker

少しでも、あなたの解析が楽になりますように!!

タイトルとURLをコピーしました