Home > Blockchain >  How do I add the option to connect observations by ID in Shiny?
How do I add the option to connect observations by ID in Shiny?

Time:12-20

I'm working on my Shiny app that visualizes/summarizes PK data. Anyways, I have a small question. I want to add in the option for the user to connect observations by ID in Shiny, so I want them to choose. This could be a single tickbox which would be: "Connect observations by ID', or just a statement like: 'Connect observations by ID:" with boxes as 'Yes' or 'No'. I hope you get what I mean. How do I do this? I have a pretty large code for my app, as I've come a long way already.

Small note, I can't generate a report yet, as the code is not right, but you can just ignore this. Tab 2 is not finished yet, but the base is there.

UI

ui <- fluidPage(

tabsetPanel(tabPanel("Tab 1",
                     
    titlePanel("Shiny App: Concentration vs Time Graphs"),
                     
     sidebarLayout(
        mainPanel("Concentration vs Time graphs", plotOutput(outputId = "plot")),
        sidebarPanel(style = "height:90vh; overflow-y: auto",
                     p("This app is developed to visualize pharmacokinetic data of different antibodies. Please select the data you want to visualize before running the graph. The graph can be reset with the reset button."),
                     strong("1. Filter your data for these following variables:"),
        checkboxInput('checkbox1', 'Filter by study', FALSE),
        conditionalPanel(condition = "input.checkbox1 == 1",
                         selectInput(inputId = "study", label = "Include study:",
                             choices = c("GLP Toxicity" = "GLPTOX", "Dose Range Finding" = "DRF", "Single Dose" = "SD", "Repeat Dose" = "RD"),
                             selected = c("GLPTOX", "DRF", "SD", "RD"),
                             multiple = T)
        ),
        checkboxInput('checkbox2', 'Filter by platform', FALSE),
        conditionalPanel(condition = "input.checkbox2 == 1",
                         selectInput(inputId = "platform", label = "Include platform:",
                                     choices = c("Hexabody", 'Duobody' = "Doubody", "Bispecific"), selected = c("Hexabody", "Doubody", "Bispecific"),
                                     multiple = T)
        ),
        checkboxInput('checkbox3', 'Filter by species', F),
        conditionalPanel(condition = "input.checkbox3 == 1",
                         selectInput(inputId = "species", label = "Include species:",
                                     choices = c("Monkey", 'Mouse'), selected = c('Monkey', 'Mouse'), multiple = T)
        ),
        checkboxInput('checkbox4', 'Filter by administration route', F),
        conditionalPanel(condition = "input.checkbox4 == 1",
                         selectInput(inputId = "route", label = "Include administration route:",
                                     choices = c('Route 1' = "ROUTE1", 'Route 2' = "ROUTE2"), selected = c("ROUTE1", "ROUTE2"),
                                     multiple = T)
        ),
        selectInput(inputId = "x", label = "2. X-axis:", choices = c("Time" = "TIME", "TLD"), selected = "Time"
        ),
        selectInput(inputId = 'column', label = "3. Columns for:", 
                    choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE", "Animal ID" = "ANIMALID"),
                    selected = "DOSEMGKG"
        ),
        conditionalPanel(condition = "input.column == 'DOSEMGKG'",
            selectInput(inputId = 'dose', label = "Choose dose(s):",
                choices = c("0.05", '0.5', "20", '5'), selected = c('0.05', '0.5', '20', '5'), multiple = T
            )
        ),
        selectInput(inputId = 'row', label = "4. Rows for:",
            choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE",  "Animal ID" = "ANIMALID", 
                        "Platform" = "PLATFORM", "Mutation" = "MUTATION"),
            selected = "ABXID"
        ),
        conditionalPanel(condition = "input.row == 'MUTATION'",
            selectInput(inputId = 'mutation', label = "Choose mutation(s):", choices = c('M1', "M2", "M3"), selected = c('M1', "M2", "M3"), multiple = T
            )
        ),
        conditionalPanel(
            condition = "input.row == 'ABXID'",
            selectInput(
                inputId = 'antibody',
                label = "Choose antibody(s):",
                choices = c('Duobody-XXXXX', "Duobody-CD3x5T4"), selected = c('Duobody-XXXXX', 'Duobody-CD3x5T4'), multiple = T
            )
        ),
        selectInput(
            inputId = "group",
            label = "5. Group by:",
            choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE",  "Animal ID" = "ANIMALID",
                        'Administration route' = 'ROUTE'),
            selected = "ANIMALID"
        ),
        sliderInput(
            inputId = 'trange',
            label = "6. Time range:",
            min = 0,
            max = 1704,
            value = c(0, 1704 )
        ),
        actionButton(
            inputId = 'runbutton',
            label = 'Run graph'
        ),
        actionButton(
            inputId = 'resetbutton',
            label = 'Reset graph'
        ),
        downloadButton(outputId = 'report', label = "Generate report"),
        br(),
        br(),
        br(),
        p("----------")
                ))
                     
)),


tabsetPanel(tabPanel("Tab 2",
                     
    titlePanel("Tab 2"),
                     
        sidebarLayout(
            mainPanel("Plot #2", plotOutput(outputId = "plot2")),
                sidebarPanel(helpText("Whatever text..."),
                    selectInput(
                    inputId = 't',
                    label = "Example",
                    choices = c("#1", "#2", "#3"),
                    selected = "#1"
                    )
                         )
                     )))
)

Server

server <- function(input, output, session){


observeEvent(input$runbutton, {output$plot <- renderPlot({
    ggplot(data = df %>% filter(STUDYID %in% input$study & ABXID %in% input$antibody & MUTATION %in% input$mutation & PLATFORM %in% input$platform
                                & SPECIES %in% input$species & DOSEMGKG %in% input$dose & ROUTE %in% input$route), 
           aes_string(x = input$x, y = "DV", col = input$group))   xlab("Time")   ylab("Concentration (ug/mL)")  
        geom_point()    facet_grid(get(input$row) ~ get(input$column))   scale_x_continuous(limits = input$trange)   
        scale_color_viridis(discrete = T, option = 'F', begin = 0, end = 0.8)   theme_bw()   scale_y_log10()})})


observeEvent(input$resetbutton, {output$plot <- renderPlot({ NULL })})

output$report <- downloadHandler(filename = "report.pdf", content = function(file){
    tempReport <- file.path(tempdir(), "report.Rmd")
    file.copy("report.Rmd", tempReport, overwrite = T)
    params <- list(n = input$x)
    rmarkdown::render(tempReport, output_file = file, params = params, envir = new.env(parent = globalenv()))
})
}

shinyApp(ui = ui, server = server)

I know that it's something with geom_line(aes(group = "ANIMALID")), but I do not yet know how to make this an option to include/exclude.

CodePudding user response:

Here is a simple app, that has a ggplot2 with some data, and whether the points are to be drawn connected by lines (within relevant groups) is toggleable.

I hope it helps you; your posted code is not reproducible as it uses private data, (and it is not minimal, its a lot of content to look at).

perhaps you can use this example as a base to ask further questions from as you complicate it, or account for relevant differences. but notice how my example is at least reproducible (you can run it; it is based on public, not private data).

library(shiny)
library(tidyverse)

some_data <- distinct(
  iris,
  Species, Petal.Width, Petal.Length
) |>
  group_by(Species, Petal.Width) |>
  summarise(avg_Petal.Length = mean(Petal.Length)) |>
  ungroup()

ui <- fluidPage(
  plotOutput("myplot", width = 400, height = 400),
  checkboxInput("mytog", "line?")
)

server <- function(input, output, session) {
  output$myplot <- renderPlot({
    plot_to_show <-
      ggplot(data = some_data)  
      aes(
        x = Petal.Width,
        y = avg_Petal.Length,
        colour = Species
      )  
      geom_point()
    if (isTruthy(input$mytog)) {
      plot_to_show <- plot_to_show   geom_line()
    }

    plot_to_show
  })
}

shinyApp(ui, server)
  • Related