library(rvest)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
surveyData <- list()
#Defines URL, I made this easier to loop by using paste0 to add the ID
#to the generic URL
for (i in 1:8) {
  j=i+31280769
  

namePage <- paste0("https://www.ncbi.nlm.nih.gov/biosample/?term=",j)

#testPage variable will store all data from the webpage
#similar to what you see with "Inspect Element"
testPage <- read_html(namePage)

#Since the data we want is in a table, this is a good first step
tableText <- testPage %>% 
    html_node("table") %>%
    html_table()

names(tableText)<-c('Question','Response')
surveyData[[as.character(j)]] <-tableText
}

head(tableText)
## # A tibble: 6 × 2
##   Question              Response         
##   <chr>                 <chr>            
## 1 dominant hand         I am right handed
## 2 environmental medium  feces            
## 3 environmental package human-gut        
## 4 host body habitat     UBERON:feces     
## 5 host body mass index  21.4             
## 6 host body product     UBERON:feces
surveyData
## $`31280770`
## # A tibble: 230 × 2
##    Question              Response         
##    <chr>                 <chr>            
##  1 dominant hand         I am right handed
##  2 environmental medium  feces            
##  3 environmental package human-gut        
##  4 host body habitat     UBERON:feces     
##  5 host body mass index  21.0             
##  6 host body product     UBERON:feces     
##  7 host tissue sampled   UBERON:feces     
##  8 host height           162.0            
##  9 life stage            Adult            
## 10 race                  Hispanic         
## # ℹ 220 more rows
## 
## $`31280771`
## # A tibble: 295 × 2
##    Question              Response         
##    <chr>                 <chr>            
##  1 dominant hand         I am right handed
##  2 environmental medium  feces            
##  3 environmental package human-gut        
##  4 host body habitat     UBERON:feces     
##  5 host body mass index  26.0             
##  6 host body product     UBERON:feces     
##  7 host tissue sampled   UBERON:feces     
##  8 host height           184.0            
##  9 life stage            Adult            
## 10 race                  Hispanic         
## # ℹ 285 more rows
## 
## $`31280772`
## # A tibble: 293 × 2
##    Question              Response        
##    <chr>                 <chr>           
##  1 dominant hand         I am left handed
##  2 environmental medium  feces           
##  3 environmental package human-gut       
##  4 host body habitat     UBERON:feces    
##  5 host body mass index  25.7            
##  6 host body product     UBERON:feces    
##  7 host tissue sampled   UBERON:feces    
##  8 host height           159.0           
##  9 life stage            Adult           
## 10 race                  Hispanic        
## # ℹ 283 more rows
## 
## $`31280773`
## # A tibble: 295 × 2
##    Question              Response        
##    <chr>                 <chr>           
##  1 dominant hand         I am left handed
##  2 environmental medium  feces           
##  3 environmental package human-gut       
##  4 host body habitat     UBERON:feces    
##  5 host body mass index  25.4            
##  6 host body product     UBERON:feces    
##  7 host tissue sampled   UBERON:feces    
##  8 host height           166.0           
##  9 life stage            Adult           
## 10 race                  Hispanic        
## # ℹ 285 more rows
## 
## $`31280774`
## # A tibble: 230 × 2
##    Question              Response         
##    <chr>                 <chr>            
##  1 dominant hand         I am right handed
##  2 environmental medium  feces            
##  3 environmental package human-gut        
##  4 host body habitat     UBERON:feces     
##  5 host body mass index  20.4             
##  6 host body product     UBERON:feces     
##  7 host tissue sampled   UBERON:feces     
##  8 host height           158.0            
##  9 life stage            Adult            
## 10 race                  Hispanic         
## # ℹ 220 more rows
## 
## $`31280775`
## # A tibble: 293 × 2
##    Question              Response         
##    <chr>                 <chr>            
##  1 dominant hand         I am right handed
##  2 environmental medium  feces            
##  3 environmental package human-gut        
##  4 host body habitat     UBERON:feces     
##  5 host body mass index  33.3             
##  6 host body product     UBERON:feces     
##  7 host tissue sampled   UBERON:feces     
##  8 host height           154.0            
##  9 life stage            Adult            
## 10 race                  Hispanic         
## # ℹ 283 more rows
## 
## $`31280776`
## # A tibble: 292 × 2
##    Question              Response         
##    <chr>                 <chr>            
##  1 dominant hand         I am right handed
##  2 environmental medium  feces            
##  3 environmental package human-gut        
##  4 host body habitat     UBERON:feces     
##  5 host body mass index  28.9             
##  6 host body product     UBERON:feces     
##  7 host tissue sampled   UBERON:feces     
##  8 host height           159.0            
##  9 life stage            Adult            
## 10 race                  Hispanic         
## # ℹ 282 more rows
## 
## $`31280777`
## # A tibble: 293 × 2
##    Question              Response         
##    <chr>                 <chr>            
##  1 dominant hand         I am right handed
##  2 environmental medium  feces            
##  3 environmental package human-gut        
##  4 host body habitat     UBERON:feces     
##  5 host body mass index  21.4             
##  6 host body product     UBERON:feces     
##  7 host tissue sampled   UBERON:feces     
##  8 host height           156.0            
##  9 life stage            Adult            
## 10 race                  Hispanic         
## # ℹ 283 more rows
responses <- sapply(surveyData, function(x) x[2])
bmi <- sapply(responses, function(x) x[5])
pie(table(bmi), main = "Pie Chart of Participant BMI")

alcohol_types_beercider <- sapply(responses, function(x) x[31])
pie(table(alcohol_types_beercider), main = "Pie Chart of Beer Consumption")