gpt4 book ai didi

渲染带有标题/标题和预选行的 Shiny 数据表

转载 作者:行者123 更新时间:2023-12-03 08:45:25 29 4
gpt4 key购买 nike

我正在尝试编写 shiny app 的代码用于绘图xy数据。每个xy点与几个因素相关:

set.seed(1)
data.df <- data.frame(x = rnorm(1000), y = rnorm(1000),
sex = sample(c("F", "M"), 1000, replace = T),
age = sample(c("Y", "O"), 1000, replace = T),
group = sample(c("A", "B", "C", "D"), 1000, replace = T),
stringsAsFactors = F)

design.df <- data.frame(factor.name = c(c(rep("sex",2), rep("age",2), rep("group",4))),
factor.levels = c("F", "M","Y", "O","A", "B", "C", "D"), stringsAsFactors = F)

我想让用户能够对 xy 进行子集化数据 ( data.df ) 基于从 design.df 中选择的多行使用DT::renderDT renderUI内在 server ,其中默认选择是 design.df 的所有行。使用此代码可以正常工作:

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(shiny))
suppressPackageStartupMessages(library(DT))

server <- function(input, output)
{
output$design.idx <- renderUI({
output$design.df <- DT::renderDT(design.df, server = TRUE, selection = list(mode = "multiple", selected = rownames(design.df)[1:nrow(design.df)]))
DT::dataTableOutput("design.df")
})

xy.plot <- reactive({
if(!is.null(input$design.df_rows_selected)){
selected.design.df <- design.df[input$design.df_rows_selected,,drop = FALSE]
selected.idx <- lapply(unique(selected.design.df$factor.name), function(f) which(data.df[,f] %in% dplyr::filter(selected.design.df, factor.name == f)$factor.levels)) %>%
unlist() %>% unique()
plot.df <- data.df[selected.idx,,drop=F]
xy.plot <- suppressWarnings(plotly::plot_ly(marker = list(size = 3), type = 'scatter', mode = "markers", x = plot.df$x, y = plot.df$y, showlegend = FALSE) %>%
plotly::layout(xaxis = list(zeroline = FALSE, showticklabels = FALSE, showgrid = FALSE), yaxis = list(zeroline = FALSE, showticklabels = FALSE, showgrid = FALSE)))
} else{
xy.plot <- NULL
}
return(xy.plot)
})

output$outPlot <- plotly::renderPlotly({
xy.plot()
})
}


ui <- fluidPage(
titlePanel("Results Explorer"),
sidebarLayout(
sidebarPanel(
uiOutput("design.idx")
),
mainPanel(
plotly::plotlyOutput("outPlot")
)
)
)

shinyApp(ui = ui, server = server)

但我还想为渲染的 datatable 有一个标题或说明文字,所以我尝试替换:

output$design.df <- DT::renderDT(design.df, server = TRUE, selection = list(mode = "multiple", selected = rownames(design.df)[1:nrow(design.df)]))

与:

output$design.df <- DT::renderDT(datatable(design.df, caption = "Subset Selection"), server = TRUE, selection = list(mode = "multiple", selected = rownames(design.df)[1:nrow(design.df)]))

在这种情况下selected = rownames(design.df)[1:nrow(design.df)]参数似乎被忽略,默认选择是无行。改变selected = rownames(design.df)[1:nrow(design.df)]selected = 1:nrow(design.df)没有区别。

知道如何将标题或说明文字添加到渲染的表格中,并将所有行作为预选默认值吗?

最佳答案

您必须将选择放入datatable对象中:

output$design.df <- DT::renderDT(
datatable(design.df,
caption = "Subset Selection",
selection = list(mode = "multiple",
selected = rownames(design.df)[1:nrow(design.df)])
),
server = TRUE)

关于渲染带有标题/标题和预选行的 Shiny 数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61644514/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com