Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206180

filtering dataframes from JSON inputs

$
0
0

I am trying to filter a data frame based on user input via a JSON file parsed into the script.

Given the following inputs:

#tibble to be filtered:

> database
# A tibble: 11 x 6
   strain genotype floodmedia inductionDelay treatment timePoint
   <lgl>  <chr>    <chr>               <dbl> <chr>     <chr>    
 1 NA     CBF1     SCGal                  15 EtoH      -1       
 2 NA     CBF1     SCGal                  15 Estradiol -1       
 3 NA     CBF1     SCGal                  15 EtoH      15       
 4 NA     CBF1     SCGal                  15 Estradiol 15       
 5 NA     CBF1     SCGal                  15 EtoH      90       
 6 NA     CBF1     SCGal                  15 Estradiol 90       
 7 NA     CBF1     SCGal                  15 EtoH      -1       
 8 NA     CBF1     SCGal                  15 Estradiol -1       
 9 NA     CBF1     SCGal                  15 EtoH      15       
10 NA     CBF1     SCGal                  15 Estradiol 15       
11 NA     CBF1     SCGal                  15 EtoH      90   

#JSON Input:

{
    "timePoint":["15", "-1"],
    "treatment":["EtoH"]
}

I would like to return:

# A tibble: 4 x 6
  strain genotype floodmedia inductionDelay treatment timePoint
  <lgl>  <chr>    <chr>               <dbl> <chr>     <chr>    
1 NA     CBF1     SCGal                  15 EtoH      -1       
2 NA     CBF1     SCGal                  15 EtoH      15       
3 NA     CBF1     SCGal                  15 EtoH      -1       
4 NA     CBF1     SCGal                  15 EtoH      15     

I need the solution to be general in terms of the variable names so that given a tibble with different variable names and a JSON matching those new names, the code would still properly filter the new tibble. As such, I have tried to use a for loop to iterate through the parsed json object, but have been unsuccessful thus far.

#code I have tried

json <- read_json("filepath_to_json_file_shown_above")
query <- database
for(i in names(json)){
  query <- query %>% filter(i %in% json$i)
}

I am using the packages tidyverse/dplyr and jsonlite to do the above tasks, though I do not mind using a different package if that is more useful.

Thank you for any help and I apologize if I have made mistakes in my question. This is my first time using StackOverflow. Please let me know if anything is unclear or incorrect.


Viewing all articles
Browse latest Browse all 206180

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>