2015-09-14 55 views
8

我想要獲得交互式縮放以在ggvis中工作,更特別是使用畫筆縮放。從https://github.com/rstudio/ggvis/issues/143判斷,我認爲這應該起作用。無法獲得交互式縮放以使用ggvis

我有以下的光澤和ggvis代碼(是完全可再現):

## ui.R 
library(ggvis) 

shinyUI(fluidRow(
    uiOutput('ui_plot1'), 
    ggvisOutput("graph_plot1") 
)) 

## server.R 
shinyServer(function(input, output, session) { 
    domains <- reactiveValues(x = c(NA, NA), y = c(NA, NA)) 

    zoom_brush = function(items, session, page_loc, plot_loc, ...) { 
    domains$x = c(200, 400) 
    } 

    plot = reactive({ 
    mtcars %>% 
     ggvis(~disp, ~mpg) %>% 
     layer_points() %>% 
     scale_numeric('x', domain = domains$x, clamp = TRUE) %>% 
     handle_brush(zoom_brush) 
    }) %>% bind_shiny('graph_plot1', 'ui_plot1') 
}) 

因此,一旦一個刷被繪製時,反應性的結構域被改變,從而改變的x域scale_numeric。如果仍然有以下挑戰:

  • 裏面zoom_brush我得到的刷的座標,但在像素座標圖的系統不是域座標系。如何將像素轉換爲域尺度?在d3中,我可以簡單地使用範圍來縮放變換函數,但是我沒有看到ggvis(通過vega)如何使用這些函數。
  • handle_brush函數僅支持設置on_move事件處理程序。在這種情況下,如果畫筆完成,我只想觸發縮放,因此畫筆上下文中的onmouseup事件。我擔心現在根本不可能?
  • 只有當我設置clamp = TRUE時,我是否會得到有效的縮放。如果沒有,那麼這個域仍然會顯示出來,只有座標軸被設置爲新的域。有這個簡單的解決辦法嗎?或者,我應該讓數據集成爲反應性的,並基於由畫筆設置的域來對其進行分組?

我運行以下R版本和軟件包版本。

> sessionInfo() 
R version 3.1.1 (2014-07-10) 
Platform: x86_64-apple-darwin10.8.0 (64-bit) 

locale: 
[1] C 

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] ggvis_0.4.1 shiny_0.12.0 

loaded via a namespace (and not attached): 
[1] DBI_0.3.1  R6_2.0.1  Rcpp_0.11.6  assertthat_0.1 digest_0.6.8 dplyr_0.4.1  htmltools_0.2.6 httpuv_1.3.2 
[9] jsonlite_0.9.16 lazyeval_0.1.10 magrittr_1.5 mime_0.3  parallel_3.1.1 tools_3.1.1  xtable_1.7-4 
+0

如果不需要依靠'ggvis'也許[這個例子](http://shiny.rstudio.com/gallery/plot-interaction-zoom.html ) 會工作? – JasonAizkalns

+0

@jason感謝您的反饋。我意識到這個選擇,這個問題專門針對ggvis。 –

回答

1

我認爲你需要子集數據:ggvis似乎還沒有聰明到忽略了規模分。下面server.R工作對我來說:

## server.R 
shinyServer(function(input, output, session) { 

    domains <- reactiveValues(x = c(NA, NA), y = c(NA, NA)) 

    mtcars_reactive <- reactive({ 
    if (anyNA(domains$x)) 
     mtcars 
    else 
     mtcars[mtcars$disp >= domains[["x"]][1] & mtcars$disp <= domains[["x"]][2], ] 
    }) 

    zoom_brush = function(items, page_loc, session, ...) { # plot_loc 
    print(items) 
    message("page_loc") 
    print(page_loc) 
    print(session) 
    domains$x = c(200, 400) 
    } 

    reactive({ 
    mtcars_reactive() %>% 
     ggvis(~disp, ~mpg) %>% 
     layer_points() %>% 
     handle_brush(zoom_brush) 
    }) %>% bind_shiny('graph_plot1', 'ui_plot1') 

})