NewQuartoLayout

Introduction

The API we are working with in this Quarto document is an Open Movie Database, where you can search for specific movie/show titles. When prompted by a specific title, the API will return all the movies/shows that have that title or something similar.

library(httr)
library(jsonlite)
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
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ readr     2.1.5
✔ ggplot2   3.5.2     ✔ stringr   1.5.1
✔ lubridate 1.9.4     ✔ tibble    3.3.0
✔ purrr     1.1.0     ✔ tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter()  masks stats::filter()
✖ purrr::flatten() masks jsonlite::flatten()
✖ dplyr::lag()     masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Connecting the API Endpoint

omdb_endpoint <- "http://www.omdbapi.com/"

This is the base endpoint of the API, before any specifications are made for the movie/show we are looking for.

Searching the Data

search <- "?s=Batman"
type <- "&type=movie"

This first R chunk is the most important and dynamic part of the search process. This is where you can specify the title of the film/show you are searching for. Additionally, you state whether you are looking for a movie or a show. In this case, I have filled in the fields to reflect that I am searching for a movie with the word “Batman” in the title.

*Alternatively, if you were looking for a show, you would simply type “series” instead of “movie” within the “type” specification.

api_key <- "&apikey=a374f200"
json <- "&r=json"
page <- "&page=1"

This second R chunk is less dynamic in the information it specifies. Our API key will remain the same, and for the time being, we only want one page to be returned for each search, and we want the results to be in JSON format.

New, Specific URL

We will now combine all the specifications we made above into one conclusive URL:

omdb_full_url <- paste0(omdb_endpoint, search, api_key, json, type, page)

Prompting this will have R spit out our complete URL that we created above:

omdb_full_url
[1] "http://www.omdbapi.com/?s=Batman&apikey=a374f200&r=json&type=movie&page=1"

Converting URL to Content

omdb_content <- omdb_full_url %>%
  GET() %>%
  content(as = "text", encoding = "UTF-8") %>%
  fromJSON()

By running the R chunk above, we have converted the API columns to text strings to make the data easier to read.

omdb_df <- omdb_content$Search %>% as.data.frame()

Now we have converted our content into a dataframe, essentially providing the information to fill the columns.

View the Dataframe

view(omdb_df)

Running this command will show us the results of our search organized into the columns: Title, Year, imdbID, Type, and Poster.