2016-05-18 10 views
1

私はブラウザでhtmlテーブルとして開こうとするRマトリックスを持っています(たぶんhtmlwidgetsパッケージを使用していますか?)。onmouseoverツールチップをR htmlテーブルに追加します(別のdata.frameのセルに基づいています)?

2番目に一致する行列があり、ブラウザテーブルの各セルのロールオーバーツールチップとしてそのセルの内容を追加したいと考えています。

ツールヒントのマトリックスをテーブルに渡す既存の方法があります。ツールチップの適切なon.mouseoverアクションで各セルの内容を自動的に折り返しますか?

セルの内容とマウスオーバーのチップの内容をそれぞれ形成する値とラベルの行列を持つソースオブジェクトを作成するためのコードです。

ncol = 4 # typically rank 1:9 
n = ncol^2 
values = matrix(round(rnorm(n),2), nrow = ncol, byrow = T) 
labs = paste0("r", rep(1:ncol, each = ncol), "c", rep(1:ncol, times = ncol)) 
labels = matrix(labs, nrow = ncol, byrow = T) 
free = matrix(rbinom(n= size, size = 1, prob = .5), nrow = ncol, byrow = T) 

myObj = list(values = values, labels = labels, free = free) 

はon.mouseover、ツールチップは、そのセルに対応するラベルを明らかにし、各セルを埋めるために「値」を用いて、NcoI部位のHTMLテーブル×NcoI部位を開くために、機能が好きであろう。

余分な点については、自由マトリクスの内容を使用して値を表示します。偽値は赤、真は緑になります。

+1

これは間違いありません。中断されていないブロックのコーディング時間を取得したら、今後数日間にわたって何かを試してみるつもりです。あなたは覚えている事例へのリンクがありますか?マトリックスはどれくらいの大きさですか? – timelyportfolio

+0

ありがとう@timelyportfolio!典型的なサイズのサンプル行列を含むように質問が更新されました。 – tim

答えて

3

ここでは、この効果をどのように実現できるかの簡単な例を示します。私はそれをもっと明確にするためにそれを簡単に保つように努めました。より高度な機能やスタイリングを簡単に追加できます。ボーナスポイントのスタイリングを追加しました。ここで

ncol = 4 # typically rank 1:9 
n = ncol^2 
values = matrix(round(rnorm(n),2), nrow = ncol, byrow = T) 
labs = paste0("r", rep(1:ncol, each = ncol), "c", rep(1:ncol, times = ncol)) 
labels = matrix(labs, nrow = ncol, byrow = T) 
free = matrix(rbinom(n= n, size = 1, prob = .5), nrow = ncol, byrow = T) 

myObj = list(values = values, labels = labels, free = free) 


# use formattable, htmlwidgets, and htmltools 
library(formattable) 
library(htmltools) 
library(htmlwidgets) 
# see what formattable gives us 
formattable(myObj$values) 

# now make each of our cells 
# contain information for our tooltip 
m_html <- matrix(
    mapply(
    function(value, label, free){ 
     as.character(tags$span(
     "data-toggle"="tooltip", 
     "title" = paste0(label, ": ", free), 
     formatC(value, format="f", digits=3) 
    )) 
    }, 
    myObj$values, 
    myObj$labels, 
    myObj$free 
), 
    ncol = 4 
) 

browsable(
    attachDependencies(
    tagList(
     onRender(
     as.htmlwidget(formattable(m_html)), 
    " 
    function(el,x){ 
    $(el).find('[data-toggle=\"tooltip\"]').tooltip() 
    } 
    "  
    ) 
    ), 
    shiny::bootstrapLib() 
) 
) 

は別の方法で上記の操作を行うと、また、あなたが提案したスタイリングを追加するための非常に単純な方法です。

# purrr could ease some of this pain 
# but wanted to avoid dependencies 
formattable(
    matrix(
    formatter(
     "span", 
     "data-toggle" = "tooltip", 
     # use the label and free to add a simple title 
     # this can be infinitely styled and refined 
     "title" = mapply(
     function(value,label,free) { 
      paste0(label,": ",free) 
     }, 
     myObj$values, myObj$label, myObj$free 
    ), 
     # color the background of cells based on free 
     "style" = mapply(
     function(value,free) { 
      if(free==1) color = "red" 
      if(free==0) color = "green" 
      paste0("display:block; background:",color,";") 
     }, 
     myObj$values, myObj$free 
    ), 
     # values will be the text in the cells 
     x~x 
    )(myObj$values), 
    # put back in a matrix of ncol=4 
    ncol=4 
) 
) 
+0

ここに別の例が追加されましたhttp://bl.ocks.org/timelyportfolio/78c668d3ba8a46f0570298bc910d1a2e – timelyportfolio

関連する問題