首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法使用ggiraph和onclick在R中的Modal窗口中显示过滤后的数据表?

有没有办法使用ggiraph和onclick在R中的Modal窗口中显示过滤后的数据表?
EN

Stack Overflow用户
提问于 2020-02-28 12:51:10
回答 1查看 896关注 0票数 1

我希望有一个使用ggiraph的交互式绘图,其中我的onclick启动了一个模式,这是一个弹出窗口,显示与我所单击的单个数据点相关的信息。

下面是我的代码。我无法使模式弹出窗口正常工作。我可以让一个Modal弹出窗口出现,但它是空白的。但是,我可以使用这里提供的一个示例让表自己进行过滤。

https://davidgohel.github.io/ggiraph/articles/offcran/shiny.html

'run_girafe_example("DT")‘

我想让这个过滤表出现在一个模态弹出窗口中。可能会将数据转置,并以更好的格式呈现。但是,如果我可以将数据呈现在Modal中,我就可以在以后弄清楚如何表示它。我只需要弄清楚如何让Modal显示过滤后的数据表!

如有任何帮助,我们将不胜感激:)

代码语言:javascript
复制
library(ggiraph)
library(ggplot2)
library(tidyverse)
library(htmltools)
library(DT)
library(shinyBS)
library(shinydashboard)

theme_set(theme_minimal())

data <- mtcars

ui <- fluidPage(

  fluidRow(
    column(width=12,
           h4("click a point, the data table will be filtered...")
    )
  ),

  fluidRow(
    column(width=12,
           ggiraphOutput("fixedplot")
    )
  )
  ,

  fluidRow(
    column(width=12,
           includeScript(path = "set_search_val.js"),
           DT::dataTableOutput("modaltable")
    )
    )
)

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

  output$fixedplot <-renderGirafe({
    data$label <- gsub(pattern = "'", " ", row.names(data) )
    data$onclick <- paste0("set_search_val(\"", data$label, "\");")

    gg <- ggplot(data = data,
                 mapping = aes(x=wt, y=mpg,
                               tooltip = label,
                               data_id = label, 
                               onclick = onclick 
                               ) 
                 ) +
      geom_point_interactive()

    girafe(code = print (gg), 
           options = list(
             opts_selection(type = "single")
             )
           )
    })

  observeEvent(input$fixedplot_selected,{
    showModal(modalDialog(
      tags$caption("Table"),
      tableOutput("modaltable")
    ))
  }
  )

  output$modaltable <- DT::renderDataTable({
    car_data <- data[,1:7]
    DT::datatable(car_data, escape = FALSE)
  })
}

shinyApp(ui = ui, server = server)
EN

回答 1

Stack Overflow用户

发布于 2020-02-28 14:59:56

您需要在modalDialog内部调用DT::renderDataTable

代码语言:javascript
复制
library(ggiraph)
library(ggplot2)
library(tidyverse)
library(htmltools)
library(DT)
library(shinyBS)
library(shinydashboard)

theme_set(theme_minimal())

data <- mtcars

ui <- fluidPage(

  fluidRow(
    column(width=12,
           h4("click a point, the data table will be filtered...")
    )
  ),

  fluidRow(
    column(width=12,
           ggiraphOutput("fixedplot")
    )
  )
  ,

  fluidRow(
    column(width=12,
           includeScript(path = "set_search_val.js"),
           DT::dataTableOutput("modaltable")
    )
  )
)

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

  output$fixedplot <-renderGirafe({
    data$label <- gsub(pattern = "'", " ", row.names(data) )
    data$onclick <- paste0("set_search_val(\"", data$label, "\");")

    gg <- ggplot(data = data,
                 mapping = aes(x=wt, y=mpg,
                               tooltip = label,
                               data_id = label, 
                               onclick = onclick 
                 ) 
    ) +
      geom_point_interactive()

    girafe(code = print (gg), 
           options = list(
             opts_selection(type = "single")
           )
    )
  })

  observeEvent(input$fixedplot_selected,{
    showModal(modalDialog(
      tags$caption("Table"),
      DT::renderDataTable({
        car_data <- data[,1:7]
        DT::datatable(car_data, escape = FALSE)
      })
    ))
  }
  )

  output$modaltable <- DT::renderDataTable({
    car_data <- data[,1:7]
    DT::datatable(car_data, escape = FALSE)
  })
}

shinyApp(ui = ui, server = server)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60445453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档