【发布时间】:2021-03-04 09:03:41
【问题描述】:
我希望显示具有低-高百分位数的表格以识别极端数据。 我的最终目标是在我的数据库(闪亮)中显示不同 tabPanel 中的每个选项卡( (tab1 with percentile 80) 。 我尝试了很多东西,但没有工作或不好。
为了解释,我给出了一个最少的代码:
- 按过滤器和周期显示数据的箱线图
- 显示箱形图中出现的所有数据的主选项卡
- 显示 20% 的选项卡(无效)
您有什么建议可以解决这个问题并可能简化编程吗?
非常感谢您的贡献
#data
data<- data.frame(stringsAsFactors=FALSE,
id=c("1", "2", "3", "4", "5", "6", "7","8","9"),
um = c("A1234","A1234","C1345","C1345","Z4453","C1345","C1345","Z4453","A1234"),
time= c(1, 12 , 11, 34, 47, 3, 5, 8, 12),
week =c("2020-002","2020-011","2020-011","2020-005","2020-004","2020-005","2020-011","2020-005","2020-006"),
month =c("2020-01","2020-03","2020-03","2020-02","2020-01","2020-02","2020-03","2020-02","2020-02"),
year1 = c("2020","2020","2020","2020","2020","2020","2020","2020","2020"))
library(shiny)
library(dplyr)
library(ggplot2)
library(plotly)
library(DT)
ui<- shinyUI( fluidRow(
column(2,
selectInput(inputId = "dur_um",
label = "UM",
choices = as.character(unique(data$um)),
multiple=TRUE,
width = validateCssUnit(200))),
column(2, selectInput("period",
label="Display by:",
choices = c("Week_"= "week",
"Month_" = "month",
"Year_" = "year1"),
selected = "month",
width = validateCssUnit(100))),
plotOutput("graph1", width = "400px", height = "200px"), br(),
DT::dataTableOutput("tab"), br(),
DT::dataTableOutput("tab_h")) )
server <- function(input, session, output) {
periode<- reactive({
if (input$period == "week") { "week"
} else if (input$period == "month") { "month"
} else { "year1" }
})
datatime <- reactive({
dur_um <- if (is.null(input$dur_um)) unique(data$um) else input$dur_um
data %>%
filter( um %in% dur_um) %>%
group_by_(periode())
})
# global graph
output$graph1 <- renderPlot({
ggplot(datatime(), aes(.data[[periode()]], y =time )) +
geom_boxplot(aes(fill = stage(.data[[periode()]], after_scale = alpha(fill, 0.4))))
})
# Global tab (without filter on percentile)
output$tab <- DT::renderDataTable({
datatable(datatime(),
options = list(
pageLength = 50,
filter = 'top',
class = "hover"))
})
#data to filt on 20 percentile
data_h <- reactive({
dur_um <- if (is.null(input$dur_um)) unique(data$um) else input$dur_um
data %>%
filter( um %in% dur_um) %>%
group_by_(periode()) %>%
summarise(p20 = quantile(time, probs = c(0.20), na.rm = FALSE, type=2),
.groups = "drop") })
#tab with filt on perc
output$tab_h <- DT::renderDataTable({
datatable(data_h(),
options = list(
pageLength = 50,
filter = 'top',
class = "hover")) })
}
shinyApp(ui,server)
【问题讨论】: