[我想使用ggiraph进行交互式绘图,在其中我的onclick会启动一个模态,这是一个弹出窗口,显示与我单击的单个数据点有关的信息。
下面是我的代码。我无法使模式弹出窗口正常工作。我可以弹出一个Modal弹出窗口,但是它是空白的。但是,我可以使用此处提供的示例来使表格进行自我过滤。
https://davidgohel.github.io/ggiraph/articles/offcran/shiny.html
'run_girafe_example(“ DT”)'
我希望此过滤后的表格显示在模式弹出窗口中。可能以更好的格式转置和呈现数据。但是,如果我可以在模态中显示数据,则可以弄清楚以后如何表示它。我只需要弄清楚如何让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)
您需要在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)