0

I'm trying to filter my data set based on one/multiple inputs for a variable. My data set exists of concentration, time and #study data. Normally, if I would want to filter my data set based on study number(s), I would enter this code:

df %>% filter(study == "1.3" & study == "2.1")

Anyways, I want to create a Shiny app and allow users to visualize data based on a filter. For example, I'd want them to only visualize concentration/time data for a certain study (Toxicity/Single dose/Repeat dose). I'm quite new to Shiny, so I read some information on the function reactive(), but I don't think I understand it good enough yet. But, I want to show you guys my complete code.

df <- MadeUpDataSet
df <- df %>% filter(DV > 0.0533)
#df$DV <- as.numeric(as.character(df$DV))
#df$TIME <- as.numeric(as.character(df$TIME))


# Define UI for application 
ui <- fluidPage(
    
    tabsetPanel(tabPanel("Tab 1",
                         
                         titlePanel("Shiny App: Concentration vs Time Graphs"),
                         
                         sidebarLayout(
                             mainPanel("Concentration vs Time graphs",
                                       plotOutput(outputId = "plot")
                             ),
                             sidebarPanel(helpText("This app is developed to visualize pharmacokinetic data of different antibodies..."),
                                          selectInput(
                                              inputId = "study",
                                              label = "Include study:",
                                              choices = c("GLP Toxicity" = "ME1044-011", "Dose Range Finding", "Single Dose", "Repeat Dose"),
                                              selected = "ME1044-011",
                                              multiple = T
                                          ),
                                          selectInput(
                                              inputId = "x",
                                              label = "X-axis:",
                                              choices = c("Time" = "TIME", "TLD"),
                                              selected = "Time"
                                          ),
                                          selectInput(
                                              inputId = 'column',
                                              label = "Columns for:",
                                              choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE", "Animal ID" = "ANIMALID"),
                                              selected = "DOSEMGKG"
                                          ),
                                          selectInput(
                                              inputId = 'row',
                                              label = "Rows for:",
                                              choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE",  "Animal ID" = "ANIMALID"),
                                              selected = "ABXID"
                                          ),
                                          selectInput(
                                              inputId = "group",
                                              label = "Group by:",
                                              choices = c("Dose mg/kg" = "DOSEMGKG", "Species" = "SPECIES", "Antibody" = "ABXID", "Subspecies" = "SUBSPECIES", "Age" = "AGE",  "Animal ID" = "ANIMALID"),
                                              selected = "ANIMALID"
                                          ),
                                          sliderInput(
                                              inputId = 'trange',
                                              label = "Time range:",
                                              min = 0,
                                              max = 1704,
                                              value = c(0, 1704 )
                                          )
                             ))
                         
    )),
    
    
    
    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"
                                          )
                             )
                         )))
)

# Define server  

server <- function(input, output, session){
    
    **df <- reactive({df %>% filter("STUDYID" == input$study)})**
    
    output$plot <- renderPlot({
        ggplot(data = df, 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) + theme_bw() })
    
}

shinyApp(ui = ui, server = server)

Notes: I'm still working on the second tab, so that can be forgotten. Anyways, I hope that the code is understandable. So I tried to include the code between the ** **. But then I receive this error:

Warning: Error in : You're passing a function as global data. Have you misspelled the data argument in ggplot()

I hope you guys get my question. I'm pretty sure there's a simple answer to this question, but I'm not so familiar with Shiny etc.

AsGi
  • 17
  • 7
  • 3
    *"Normally ... I would enter this code"* is interesting to me, since that code produces an error (for me), and is almost certainly a logical error if it doesn't. I think you need the `%in%` operator, as in `df %>% filter(study %in% c("1.3", "2.1"))`. As for what to do in a shiny, your `filter` is missing the column name, is there a reason you cannot do `reactive({ df %>% filter(study == input$study); })`? This question is missing too much detail to be able to better inform, please [read](https://stackoverflow.com/q/5963269) then [edit] to add a min reprex. Thanks! – r2evans Dec 08 '22 at 13:27
  • 1
    @r2evans-GONAVYBEATARMY sure, sorry, I was in a bit of a hurry. Let me just edit my question. – AsGi Dec 08 '22 at 13:37
  • 2
    I've been there, I understand. Pro tip (if I can attribute that to me): the first 30 minutes your question is on SO are the most important; many times, people look at it briefly and quickly decide whether to attempt it or not. If it is incomplete or confusing, it is often dismissed (too much effort to figure out) and too often they will not return to the question. I believe your best chance for getting an answer is to ask it "well" (e.g., not too big, sample data, code attempted, clear expected output) the first time. That link I sent is a really good one for this topic. Thanks! – r2evans Dec 08 '22 at 13:44

0 Answers0