3

rChartsとshinyを実行しているアプリで、この( jsfiddle )ハイチャート機能を複製しようとしています。ユーザー イベント (テキスト ボックスの値の変更) を使用して、既にレンダリングされたプロットにプロット バンドを追加する (または、理想的には前のプロットを削除する/新しいプロットを追加する) 必要があります。xAxis オプションで plotband を使用してプロットを再描画することで機能させることができますが、描画している一部のプロットでは非常に遅くなる可能性があります。

機能が r パッケージにあるかどうかはわかりませんが、関数を実行するために JavaScript にドロップする方法はありますか?

最小限の作業例を次に示します。

サーバー.R

library(shiny)
library(plyr)
library(rCharts)
shinyServer(function(input, output,session){
  output$h1chart <- renderChart({
    h1 <- rCharts::Highcharts$new()
    h1$chart(type = "spline")
    h1$series(data = c(1, 3, 2, 4, 5, 4, 6, 2, 3, 5, NA), dashStyle = "longdash")
    h1$series(data = c(NA, 4, 1, 3, 4, 2, 9, 1, 2, 3, 4), dashStyle = "shortdot")
    h1$legend(symbolWidth = 80)
    h1$xAxis(title = list(text = "Test Plot"),startOnTick=TRUE,min=1,max=9,endOnTick=TRUE,
          plotBands = list(list(from=1.5,to=3.5,color='rgba(68, 170, 213, 0.1)',label=list(text="1",style=list(color="#6D869F"),verticalAlign="bottom"))))
    h1$set(dom="h1chart")
    return(h1)
  })
  output$selectedOut <- renderUI({
    numericInput("selected", "", value=2.5)
  })
  output$windowOut <- renderUI({    
    sliderInput(inputId="window",label="Window size around selected point:",min=1,max=5,value=2)
  })
})#end server

ui.R:

library(shiny)
library(plyr)
library(rCharts)
shinyUI(pageWithSidebar(
  headerPanel(""),
  sidebarPanel(
    wellPanel(
      h6("Change here should update plotband:"),
      uiOutput("selectedOut"),
      uiOutput("windowOut")
    ),
    tags$head(tags$style(type="text/css", ".jslider { max-width: 245px; }"),tags$style(type='text/css', ".well { max-width: 250px; }"),
          tags$style(type='text/css', ".row-fluid .span4 {width: 20%}\n.row-fluid .span8 {width: 75%}"))
  ),
  mainPanel(showOutput("h1chart","highcharts"))  
))

この関数を使用して、数値ボックス/スライダーへの変更をキャプチャできます。

addPB <- reactive({
  center <- as.numeric(input$selected[[1]])
  winHigh <- center+input$window[1]
  winLow <- center-input$window[1] #eventually I would use winLow/winHigh to change the plotband range
  list(winLow=winLow,winHigh=winHigh)
})
observe(print(addPB()$winLow))

そして、それらを実行するためのjavascript関数を構築しようとしましたが、これがjavascriptを使用してhighchartオブジェクトにアクセスする方法であるか、コードを光沢のあるものから実行する方法であるかはわかりません。

#!function(){$('#container').highcharts().xAxis[0].addPlotBand({from: 2,to: 4, color: 'rgba(68, 170, 213, 0.1)', label: 'asdf'}); } !#
4

1 に答える 1

10

これを行うには、Shiny でメッセージ パッシングを使用します。これがどのように機能するかです。スライダーまたは numericInput のデータを変更するたびにsession$sendCustomMessage、json ペイロード (この場合はband.

サーバー側では、メッセージは によって受信されShiny.addCustomMessageHandlerます。既存のバンドにアクセスして削除し、ハンドラーが受け取ったオプションを使用して新しいバンドを作成します。

Shiny でのメッセージ パッシングの詳細な概要については、Mark Heckmann によるこの優れたブログ投稿を読むことをお勧めします。

次のコードを server.R と ui.R に追加します。

# addition to server.R
observe({
    center <- as.numeric(input$selected[[1]])
    winHigh <- center + input$window[1]
    winLow <- center - input$window[1]
    #eventually I would use winLow/winHigh to change the plotband range
    band = list(from = winLow, to = winHigh, color = "rgba(68, 170, 213, 0.1)")
    print(band)
    session$sendCustomMessage(type = "customMsg", band)
})

# addition to ui.R
mainPanel(
  showOutput("h1chart","highcharts"),
  tags$script('Shiny.addCustomMessageHandler("customMsg", function(bandOpts){
     chartXAxis = $("#h1chart").highcharts().xAxis[0]
     chartXAxis.removePlotBand()
     chartXAxis.addPlotBand(bandOpts)
   })')
)
于 2013-11-27T18:08:28.557 に答える