我对 R 和 Shiny 很陌生,想用 Shiny 创建一个箱线图。这是我的代码
界面部分
ui <- shinyUI(fluidPage(
titlePanel("Shiny app"),
tabsetPanel(
tabPanel(
"Lab data (boxplot)",
titlePanel("Lab data over time"),
sidebarLayout(
sidebarPanel(
selectInput(
"lab_df",
"Choose lab dataframe",
choices = ls(),
selected = "lb"
),
selectInput(
"lab_test_var",
"Choose lab test column",
choices = NULL,
),
selectInput(
"lab_test_lvl",
"Choose lab test performed",
choices = NULL,
),
tags$br(),
),
mainPanel(plotOutput("lab_boxplot"))
)
)
)
))
服务器部分
server <- shinyServer(function(input, output, session) {
lab_df <- reactive({
get(input$lab_df)
})
observe({
col <- colnames(lab_df())
updateSelectInput(session, "lab_test_var", choices = col, selected = "LBTEST")
})
observe({
var <- lab_df()[[input$lab_test_var]]
lvl <- unique(var)
updateSelectInput(session, "lab_test_lvl", choices = lvl)
})
output$lab_boxplot <- renderPlot(ggplot(data=(labelled_dfs[[input$lab_df]] %>% filter(labelled_dfs[[input$lab_df]]$(input$lab_test_var) == input$lab_test_lvl)), aes(group=VISIT, y=LBORRES, x = VISIT)) +
geom_boxplot()
})
我有一个从 SAS 导入的带有包的数据框列表,在我的全局环境中,我的所有数据框都是对象 + 一个列表(labelled_dfs),其中包含带有标签的相同数据框。这是我的实验室数据框的一个示例:这里我的 input$lab_test_var 将是 LAB,我的 input$lab_test_lvl 将是 1(或标记数据框中的“血红蛋白”)所以我想为每个“lab_test_lvl”制作一个箱线图
参观实验室 1 1 2 1 3 1 1 2 2 2 3 2
谢谢您的帮助