Skip to content

exercise #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
9 changes: 5 additions & 4 deletions exercise-1/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ vehicle <- add_rownames(mtcars, var='car')
# Function to debug:
# Given a number of forward gears and a number of cylinders, what is the
# Name of the car with the best mpg?
BestGearsCyl <- functon(gears, cylinders) {
ret <- vehicles %>%
BestGearsCyl <- function(gears, cylinders) {
ret <- vehicle %>%
filter(gear == gears, cyl == cylinders) %>%
filter(mgp = max(mpg)) %>%
filter(mpg == max(mpg)) %>%
select(car)
return(ret)
}

# Get the best mpg car for 6 cylinder cars with 4 gears
answer <- BestGearsCyl(6, 4)
answer <- BestGearsCyl(cylinders=6, gears=4)
14 changes: 13 additions & 1 deletion exercise-2/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@ setwd('~/Documents/info-201/m10-apis/exercise-2/')


# Write a function that allows you to specify an artist, and returns the top 10 tracks of that artist
GetArtist <- function(artist) {
base <- 'https://api.spotify.com/v1/'
search <- paste("search?", "q=", artist, sep = "")
type <- '&type=artist'
query.url <- paste0(base, search, type)
data <- fromJSON(query.url)
artist.id <- data$artists$items$id[1]
album.query <- paste0(base, 'artists/', artist.id, '/top-tracks?country=US')
top.tracks <- fromJSON(album.query)
return(top.tracks)
}

#result <- GetArtist("adele")


# What are the top 10 tracks by Nelly?

result <- GetArtist('Nelly')



Expand Down
5 changes: 3 additions & 2 deletions exercise-3/exercise.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
### Exercise 3 ###
library(jsonlite)
library(dplyr)
load("nelly_tracks.Rdata")
# Use the `load` function to load in the nelly_tracks file. That should make the data.frame
# `top_nelly` available to you


# Use the `flatten` function to flatten the data.frame -- note what differs!

flatten(top.nelly)

# Use your `dplyr` functions to get the number of the songs that appear on each albumt

number.songs <- filter(flatten(top.nelly), album.album_type == 'album') %>% select(track_number)


# Bonus: perform both of the steps above in one line (one statement)
33 changes: 19 additions & 14 deletions exercise-4/exercise.r
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
# Install and load the jsonlite package

library(jsonlite)
library(dplyr)
# Make a variable base.url that has the same base url from the omdb documentation.
# (Hint: visit https://www.omdbapi.com/ to find the base url)

base.url <- "http://www.omdbapi.com/?"
# Make a variable called movie that has the names of your favorite movie

movie <- "Interstellar"
# Make a variable called move.no.spaces that holds movie where all the spaces
# are replaced with the '+' character

move.no.spaces <- gsub(" ", "+", movie)
# Make a variable called "parameters" that holds a string with the parameters
# to pass to the API. View the OMDb documentation to see which parameters
# are available. Remember to separate parameters with the '&' character.

parameters <- paste0("t=", move.no.spaces, "&r=json")
# Make a variable called request that is a string of a request URL made up of the base URL
# and the parameters string

request <- paste0(base.url, parameters)
# Use fromJSON to retrieve JSON data from the omdb api using your request URL.
# Store the result in a variable called movie.data

movie.data <- fromJSON(request)
# Make movie_data into a data frame using as.data.frame

movie.data <- as.data.frame(movie.data) %>% flatten()
# Write a function called Director that accepts a data frame of movie info and returns
# A vector of strings that states a movie and the director of said movie.

Director <- function(movie.info) {
director <- movie.info$Director
name <- movie.info$Title
return(paste(name, "is directed by", director))
}
# Call Director with your favorite movie, and assign it to the variable movie.director

movie.director <- Director(movie.data)
# Bonus

# Knowing the director of on movie is okay, but it'd be great to know the directors of different
# movies.

# Start by making a vecotr of movies and save it to the variable movie.list

movie.list <- c("Interstallar", "Saw", "Kill Bill")
# Remove spaces from each movie title

movie.list <- gsub(" ", "+", movie.list)
# Prepare this list to be passed in as parameters for the API

parameter <- paste0("t=", movie.list, "&r=json")
# Create API URL request and assign it to the variable api.request

api.request <- fromJSON()
# For every entry in the vector api.request, APPLY the function fromJSON to make a list of lists
# one entry for each request and assign this to a variable called data.
# (Hint: ?lapply. It's similar a 'for' loop but better!)
Expand Down