简体   繁体   English

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

[英]Is there a way to display a filtered data table in a Modal window in R shiny using ggiraph and onclick?

I would like to have an interactive plot using ggiraph in which my onclick initiates a modal which is a pop-up window displaying the information related to the individual data point I have clicked.我想要一个使用 ggiraph 的交互式绘图,其中我的 onclick 启动一个模式,它是一个弹出窗口,显示与我单击的单个数据点相关的信息。

here is my code below.下面是我的代码。 I cannot get the modal popup to work properly.我无法让模态弹出窗口正常工作。 I can get a Modal pop up window to come up, but it is blank.我可以得到一个模态弹出窗口,但它是空白的。 However, I can get the table to filter itself using an example provided here.但是,我可以使用此处提供的示例使表格自行过滤。

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

'run_girafe_example("DT")' 'run_girafe_example("DT")'

What I would like is for this filtered table to be presented in a Modal pop up window.我想要的是在模态弹出窗口中显示这个过滤表。 Possibly with the data transposed and presented in a nicer format.可能将数据转置并以更好的格式呈现。 But If I can get the data to be presented in the Modal, I can figure out how to represent it later.但是如果我能得到要在Modal中呈现的数据,我就可以在以后弄清楚如何表示它。 I just need to figure out how to get the Modal to show the filtered data table in the first place!我只需要弄清楚如何让 Modal 首先显示过滤后的数据表!

any help would be appreciated :)任何帮助,将不胜感激 :)

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)

You need to call DT::renderDataTable inside modalDialog call:您需要在modalDialog调用中调用DT::renderDataTable

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM