2017-06-12 5 views
1

他のオブジェクト(bsModalなど)をバインドするためにwordcloud2パッケージでclickイベントとしてwordcloudの任意の単語をクリックして返すことは可能ですか?例えば、プロットでは、これは光沢のあるセッション内からアクセス可能なオブジェクトを生成し、イベントデータを保持する(例えば、クリック座標)(https://plot.ly/r/shinyapp-linked-click/)ことによって達成される。wordcloud2で光沢のあるクリックイベントを作成することはできますか?

以下の例では、ユーザーがクリックした単語が表示されるように、bsModalをワードクラウドにバインドしたいと考えています。

ui.R

library(shiny) 
shinyUI(fluidPage(
    mainPanel(
     wordcloud2Output("wordcloud") 
    ) 
)) 

server.R

library(shiny) 
library(wordcloud2) 
library(tm) 

shinyServer(function(input, output) { 

    words <- c ("1st", "2nd", "3rd", "4th", "5h", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", 
      "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th") 
    random_words <- sample(words, 500, replace = TRUE) 
    docs <- Corpus(VectorSource(random_words)) 
    dtm <- TermDocumentMatrix(docs) 
    m <- as.matrix(dtm) 
    v <- sort(rowSums(m),decreasing=TRUE) 
    d <- data.frame(word = names(v),freq=v) 

    wordcloud_plot <- wordcloud2(data = d, size = 0.7, shuffle =FALSE, ellipticity = 1, minRotation = -pi/8, maxRotation = pi/8, 
          shape = 'circle') 
    output$wordcloud <- renderWordcloud2(wordcloud_plot) 
}) 

答えて

3

はい、あなたは光沢のあるアプリのUIにはJavaScriptの数行を追加することで、回避策を持つことができます。

次のようにちょうどあなたのUIを変更します。

library(shiny) 
shinyUI(fluidPage(
    mainPanel(
     wordcloud2Output("wordcloud"), 
     tags$script(HTML(
       "$(document).on('click', '#canvas', function() {", 
       'word = document.getElementById("wcSpan").innerHTML;', 
       "Shiny.onInputChange('selected_word', word);", 
       "});" 
      )) 
    ) 
)) 

をこのコードは、あなたがshinyappのサーバ側でinput$selected_wordを経由してアクセスすることができ、新たな入力変数を生成し、あなたが他とwordcloudをバインドするために使用することができますアプリ内のオブジェクト

ホバー機能の値をとるため、入力はword:freqの形式になります。 gsub()を使用すると、次のように頻度とコロンを取り除くことができます。gsub(":.*","",isolate(input$selected_word))

希望すると助かります!

関連する問題