【发布时间】:2021-06-06 20:02:33
【问题描述】:
我在 Shiny 中构建了一个交互式散点图。使用 plotly,我可以选择点组并在绘图旁边的表格中呈现该组的注释。
library(survival)
library(survminer)
mtcars <- get(data("mtcars"))
attach(mtcars)
mtcars$OS <- sample(100, size = nrow(mtcars), replace = TRUE)
mtcars$status <- sample(0:1, size = nrow(mtcars), replace = TRUE)
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Test1", tabName = "test1"),
menuItem("Test2", tabName = "test2"),
menuItem("Test3", tabName = "test3"),
radioButtons("radio", h3("Choose groups"),
choices = list("Group 1" = 1, "Group 2" = 2,
"Group 3" = 3),selected = 1),
actionButton("action", "Reset")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "test1",
fluidRow(
column(6,plotlyOutput("plot")),
column(width = 6, offset = 0,
DT::dataTableOutput("brush"),
tags$head(tags$style("#brush{font-size:11px;}")))
)
)
)
)
)
server <- shinyServer(function(input, output, session) {
output$plot <- renderPlotly({
key <- row.names(mtcars)
p <- ggplot(data=mtcars, aes(x=wt,y=mpg,key=key)) +
geom_point(colour="grey", size=2, alpha=1, stroke=0.5)
ggplotly(p) %>% layout(height = 500, width = 500, dragmode = "select")
})
output$brush <- DT::renderDataTable({
d <- event_data("plotly_selected")
req(d)
DT::datatable(mtcars[unlist(d$key), c("mpg", "cyl", "OS", "status")],
options = list(lengthMenu = c(5, 30, 50), pageLength = 30))
}
)
})
shinyApp(ui, server)
示例: enter image description here
我希望能够选择(套索或矩形)点组,并在表格下方的单独图中显示这些组之间的生存曲线(以及 p 值,如果可能)。例如,用户将选择左侧菜单上的“Group1”,然后勾勒出所需的点组,然后选择“Group 2”并选择第二组点,依此类推。每次选择后,生存曲线出现在表格下方。一旦完成(并且想要重新开始新的比较,用户点击“重置”)。这是一个示例输出:
我真的不知道从哪里开始如何合并它。任何帮助都会很棒,谢谢
【问题讨论】: