2016-09-23 96 views
0

řGugus,子設置與plotly交互數據或googlevis圖表中閃亮的應用

是否有任何的方式在data.table子集和視數據通過點擊交互式plotly或googlevis圖表中有光澤的應用程序?

例如,我想看看在圖片中的突出顯示的行,當我在相關部分單擊由下面的代碼生成plotly圖表上:

library(shiny) 
    library(plotly) 
    library(DT) 
    library(dplyr) 

    shinyApp(
    ui = shinyUI(fluidPage(
     titlePanel("Movie Ratings!"), 

     mainPanel(
     plotlyOutput("chart", width = "100%"), 
     dataTableOutput("DToutput") 
    ) 
    )), 

    server = function(input, output, session) { 

     df <- structure(c(106487,495681,1597442,2452577,2065141,2271925,4735484,3555352,8056040,4321887, 
         2463194,347566,621147,1325727,1123492,800368,761550,1359737,1073726,36,53,141, 
         41538,64759,124160,69942,74862,323543,247236,112059,16595,37028,153249,427642, 
         1588178,2738157,2795672,2265696,11951,33424,62469,74720,166607,404044,426967, 
         38972,361888,1143671,1516716,160037,354804,996944,1716374,1982735,3615225, 
         4486806,3037122,17,54,55,210,312,358,857,350,7368,8443,6286,1750,7367,14092, 
         28954,80779,176893,354939,446792,33333,69911,53144,29169,18005,11704,13363, 
         18028,46547,14574,8954,2483,14693,25467,25215,41254,46237,98263,185986), 
         .Dim=c(19,5),.Dimnames=list(c("1820-30","1831-40","1841-50","1851-60","1861-70", 
                "1871-80","1881-90","1891-00","1901-10","1911-20", 
                "1921-30","1931-40","1941-50","1951-60","1961-70", 
                "1971-80","1981-90","1991-00","2001-06"), 
                c("Europe","Asia","Americas","Africa","Oceania"))) 
     df.m <- melt(df) 
     df.m <- rename(df.m, c(Var1 = "Period", Var2 = "Region")) 


     output$chart <- renderPlotly({ 

     a <- ggplot(df.m, aes(x = Period, y = value/1e+06,fill = Region)) + 
      ggtitle("Migration to the United States by Source Region (1820-2006), In Millions") 
     b <- a + geom_bar(stat = "identity", position = "stack") 

     p <- ggplotly(b) 
     p 
    }) 

     output$DToutput <- renderDataTable({df.m}) 
}) 

enter image description here

最終目的是生成一個應用程序,用戶可以通過應用程序中任何位置的數據和圖表輕鬆進行導航。我有一個類似的應用程序,但用不同的代碼編寫:http://mqasim.me/sw1000/

回答

1

Plotly中的當前點擊事件返回跟蹤中的所有數據,因此將難以分離堆棧中的元素。此外,將需要與plot_ly()建立情節。處理不同頁面上所選表格行的一種方法是不需要分頁。

curveNumber:用於多發性痕跡,信息將被以堆疊方式

this Plotly tutorial參見用於耦合事件和DT的閃亮頁用於選擇行的section 2.3返回。

library(shiny) 
library(plotly) 
library(DT) 
library(dplyr) 
library(reshape2) 

shinyApp(
    ui = shinyUI(fluidPage(
    titlePanel("Movie Ratings!"), 

    mainPanel(
     plotlyOutput("chart", width = "100%"), 
     DT::dataTableOutput("DToutput") 
    ) 
)), 

    server = function(input, output, session) { 

    df <- structure(c(106487,495681,1597442,2452577,2065141,2271925,4735484,3555352,8056040,4321887, 
         2463194,347566,621147,1325727,1123492,800368,761550,1359737,1073726,36,53,141, 
         41538,64759,124160,69942,74862,323543,247236,112059,16595,37028,153249,427642, 
         1588178,2738157,2795672,2265696,11951,33424,62469,74720,166607,404044,426967, 
         38972,361888,1143671,1516716,160037,354804,996944,1716374,1982735,3615225, 
         4486806,3037122,17,54,55,210,312,358,857,350,7368,8443,6286,1750,7367,14092, 
         28954,80779,176893,354939,446792,33333,69911,53144,29169,18005,11704,13363, 
         18028,46547,14574,8954,2483,14693,25467,25215,41254,46237,98263,185986), 
        .Dim=c(19,5),.Dimnames=list(c("1820-30","1831-40","1841-50","1851-60","1861-70", 
                "1871-80","1881-90","1891-00","1901-10","1911-20", 
                "1921-30","1931-40","1941-50","1951-60","1961-70", 
                "1971-80","1981-90","1991-00","2001-06"), 
               c("Europe","Asia","Americas","Africa","Oceania"))) 
    df.m <- melt(df) 
    df.m <- rename(df.m, Period = Var1, Region = Var2) 

    output$chart <- renderPlotly({ 

     plot_ly(data = df.m, x = Period, y = value/1e+06, color = Region, type = "bar", source = "select") %>% 
     layout(title = "Migration to the United States by Source Region (1820-2006), In Millions", 
       xaxis = list(type = "category"), 
       barmode = "stack") 
    }) 

    output$DToutput <- DT::renderDataTable({ 

     datatable(df.m, selection = list(target = "row+column"), 
       options = list(pageLength = nrow(df.m), dom = "ft")) 
    }) 

    proxy = dataTableProxy('DToutput') 

    # highlight rows that are selected on plotly output 
    observe({ 

     event.data = plotly::event_data("plotly_click", source = "select") 

     if(is.null(event.data)) { 
     rowNums <- NULL 
     } else { 
     rowNums <- row.names(df.m[df.m$Period %in% event.data$x,]) 
     } 

     proxy %>% selectRows(as.numeric(rowNums)) 
    })  
    }) 

enter image description here

+0

這是偉大的。有沒有辦法排除未選擇的行或只顯示突出顯示的行? –

+0

是的,我會通過使用'event.data'的輸出對'df.m'進行子集化來處理'dataTableOutput'。 –

+0

請注意,plot_ly現在需要公式符號,所以它會是'plot_ly(data = df.m,x =〜Period,y =〜value/1e + 06,color =〜Region,...'。 –

相關問題