1

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

下面是我的代码。我无法让模式弹出窗口正常工作。我可以得到一个模态弹出窗口,但它是空白的。但是,我可以使用此处提供的示例让表格自行过滤。

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

'run_girafe_example(“DT”)'

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

任何帮助,将不胜感激 :)

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)
4

1 回答 1

2

您需要拨打DT::renderDataTable内部modalDialog电话:

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)

于 2020-02-28T06:59:56.497 回答