2016-10-11 6 views
2

ユーザーがデータテーブルの行名を上書き/クリックするとツールチップやポップオーバーのような情報を追加しようとしているので、現在持っている定義を参照する必要はありません別のtabPanelでここでは作業例です:行名のShinyデータ型のツールチップまたはポップオーバー?

server.R:

library(shiny) 
library(DT) 
library(shinyBS) 

# Define server for the Shiny app 
shinyServer(function(input, output,session) { 

tdata <- as.data.frame(iris) 

# Render table here 
output$mytable <- DT::renderDataTable(DT::datatable(

    tdata[1:5,], 

    options = list(paging = FALSE, searching = FALSE, info = FALSE, sort = FALSE, 
       columnDefs=list(list(targets=1:4, class="dt-right"))), 

    rownames = paste("rowname",1:5), 

    container = htmltools::withTags(table(
     class = 'display', 
     thead(
     tr(lapply(rep(c('ratios','name1', 'name2', 'name3','name4','name5'), 1),th)) 
    ) 
    )) 
)) 

}) # end of shinyServer function 

ui.R:

library(shiny) 
library(DT) 
library(shinyBS) 

shinyUI(
    mainPanel(
     DT::dataTableOutput("mytable") 
    ) 
)  

成功せず、私は以下の議論のトピックを見ていることに注意してください: R shiny mouseover text for table columns、としてよくあるのですが、 DTパッケージオプションの中の何かか、shinyBSパッケージ(bsTooltipのようなもの)を使ったり、HTMLやJSを追加したりしています。 このツールチップ/ポップオーバー機能は、Shinyがデータベース内で自然にサポートしていないようです...!?

答えて

4

このコードは動作しますが、クライアント側モードで動作します。簡単にするために、虹彩データセットの最初の5行を使用しましたが、そのアイデアは明らかです。行名の上にカーソルを置くと、ツールチップが表示されます。

ui.R

library(shiny) 
    library(DT) 
    shinyUI(
      mainPanel(
        DT::dataTableOutput("tbl") 
      ) 
    )  

server.R

library(shiny) 
    library(DT) 
    shinyServer(function(input, output,session) { 
      output$tbl = DT::renderDataTable(
        datatable(iris[1:5, ], callback = JS(" 
            var tips = ['First row name', 'Second row name', 'Third row name', 
            'Fourth row name', 'Fifth row name'], 
            firstColumn = $('#tbl tr td:first-child'); 
            for (var i = 0; i < tips.length; i++) { 
            $(firstColumn[i]).attr('title', tips[i]); 
            }")), server = FALSE) 
    }) 
+0

素晴らしい、その作品!私は列名の例からそのようなものを調理しましたが、私はサーバー= FALSEを忘れてしまいました!あなたの助けてくれてありがとう、バター! – humuskopf

1

あなたのコードで、ホバーにラベルを表示するために使用されるtitle属性が使用されていないため、動作しませんでした。

container = htmltools::withTags(table(
    class = 'display', 
    thead(
    tr(lapply(rep(c('ratios','name1', 'name2', 'name3','name4','name5'), 1),th)) 
) 
)) 
# OUTPUT OF CONTAINER 
#<table class="display"> 
# <thead> 
# <tr> 
#  <th>ratios</th> 
#  <th>name1</th> 
#  <th>name2</th> 
#  <th>name3</th> 
#  <th>name4</th> 
#  <th>name5</th> 
# </tr> 
# </thead> 
#</table> 

私は(title属性を使用して)、次のようにコードを変更し、今では動作するはずです:

ラベルをcolumnLabels <- paste0("label", 1:6)を設定し、その容器のみが変更された以外のものです。

# Render table here 
    output$mytable <- DT::renderDataTable({ 
    columnLabels <- paste0("label", 1:6) 

    DT::datatable(
     tdata[1:5,], 

     options = list(paging = FALSE, searching = FALSE, info = FALSE, sort = FALSE, 
        columnDefs=list(list(targets=1:4, class="dt-right"))), 

     rownames = paste("rowname",1:5), 

     container = htmltools::withTags(table(
     class = 'display', 
     thead(
      #tags$th(title=active_columns[i], colnames(data)[i]) 
      tr(apply(data.frame(colnames=c('ratios','name1', 'name2', 'name3','name4','name5'), labels=columnLabels), 1, 
        function(x) th(title=x[2], x[1]))) 
     ) 
    )) 
    ) 
    }) 
# OUTPUT OF CONTAINER 
#<table class="display"> 
# <thead> 
# <tr> 
#  <th title="label1">ratios</th> 
#  <th title="label2">name1</th> 
#  <th title="label3">name2</th> 
#  <th title="label4">name3</th> 
#  <th title="label5">name4</th> 
#  <th title="label6">name5</th> 
# </tr> 
# </thead> 
#</table> 
+0

この生成列ラベル、ラベルを行ありません。私は成功していないrownamesのために同じことをするためにそれを適応しようとしましたが、上記のValterによって投稿された他の解決策が働いたとして、あまりにも長く試していませんでした。しかし、ありがとう!何かの理由で – humuskopf

+0

私はそれがcolnamesについて考えていた、申し訳ありません。 :) – GyD

関連する問題