I'm quite new to R and Shiny and wanted to create a boxplot with Shiny. Here is my code
UI part
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 part
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()
})
I have a list of dataframes imported from SAS with package haven, I have in my global environment all of my dataframes as objects + a list (labelled_dfs) containing same dataframes with labels. Here is an example of my lab dataframe : Here my input$lab_test_var would be LAB and my input$lab_test_lvl would be 1 (or "Haemoglobins" in labelled dataframe for example) So I would like to make a boxplot for each "lab_test_lvl"
VISIT LAB 1 1 2 1 3 1 1 2 2 2 3 2
Thank you for your help