通过selectInput和Plotly选择过滤闪亮的数据表

时间:2019-12-23 21:00:33

标签: r shiny datatables reactive-programming r-plotly

我想同时通过selectInput和在散点图上选择一个区域来过滤数据表行。

因此,如果我在散点图上选择一个区域,则数据表中只会出现与所选点相对应的行。

接下来,如果我使用selectInput过滤了一些行,则散点图中将仅显示这些已过滤的点,而同时在数据表中将仅显示相应的已过滤的行。

此外,当我在selectInput上设置一些过滤器时,我仍然希望能够通过在散点图上选择一个区域来缩小选择范围,以便在数据表中仅出现先由selectInput过滤然后再由选择缩小的行。

我已经简化了本教程https://plot.ly/r/datatable/

为以下格式:

我只能: -通过选择散点上的区域来过滤点和行

我不知道如何使此代码完全具有反应性,并将反应性函数m2()作为参数传递给renderDataTable命令,因为散点图选择中已经有参数。将这些参数“移动”到该反应函数的主体没有用。

谢谢!

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

m <- mtcars %>% 
  tibble::rownames_to_column()

ui <- fluidPage(
  h1("Plotly & DT", align = "center"),

  selectInput("pickvalue", label = "Cyl", m$cyl,
              selected = NULL, multiple = T),

  plotlyOutput("x2"),

  DT::dataTableOutput("x1")

)

server <- function(input, output, session) {


d <- SharedData$new(m, ~rowname)

m2 <- reactive({

  dat2 <- m

  if (!is.null(input$pickvalue)){dat2 <- dat2 %>% filter(cyl %in% input$pickvalue)} 

  return(dat2)

})


output$x2 <- renderPlotly({

       m2() %>%
        plot_ly(x = ~mpg, y = ~disp, mode = "markers") %>%
        highlight("plotly_selected")
  })


  output$x1 <- DT::renderDataTable({

    m2 <- m[d$selection(),]
    dt <- DT::datatable(m)
    if (NROW(m2) == 0) {
      dt
    } else {
     m2
    }
  })



}

shinyApp(ui, server)

0 个答案:

没有答案
相关问题