如何对不存在的 Web 图像执行错误处理并引发 HTTP 404 错误?

问题描述 投票:0回答:1

我正在尝试创建一个 gt 表,其中包含棒球运动员的裁剪头像,但是没有可供一名球员(Cameron Sisneros)使用的图像,这会导致 HTTP 错误 404。理想情况下,缺少球员图像将默认为裁剪版本小联盟棒球的徽标(包含在下面的代码中)。如果我省略这个玩家,代码运行良好,请参见下图。但是,我想要一个更具可扩展性的解决方案来处理此类错误并显示所有玩家,并为那些没有可用头像的玩家提供后备图像。

library(cropcircles)
library(tidyverse)
library(gt)
library(gtExtras)
 
df_test <- structure(list(player = c("Matt Shaw", "Owen Caissie", 
                                     "Cameron Sisneros"), 
          matchup_batter_id = c(807713L, 683357L, 815133L), 
          HR = c(4L, 4L, 3L)), 
          row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame")) %>%  
   
 # filter(player != 'Cameron Sisneros') %>% 
  mutate(headshot = paste0("https://img.mlbstatic.com/mlb-photos/image/upload/c_fill,g_auto/w_360/v1/people/",matchup_batter_id,"/headshot/milb/current")) %>% 
  mutate(headshot = cropcircles::circle_crop(headshot,
                                border_size = 12,
                                border_colour = "black")
          ) # Ideally an HTTP error 404 would default to cropped image of the league logo
  # League logo found here: https://images.ctfassets.net/iiozhi00a8lc/7eeTdW5zGYe0sW2ZlIle7E/990464d4b5e4e3b3a65cb6c56ce808ea/milb-alt.svg

# Construct gt table
df_test %>% 
  gt() %>% 
  gt_img_rows(headshot, height = 37, img_source = "local") #add cropped headshots 

Example of gt table without photo-less player

r error-handling try-catch gt gtextras
1个回答
0
投票

您可以使用

purrr::possibly()
副词创建
cropcircles::circle_crop()
的修改版本,它返回一些默认值而不是错误。

为了避免在输入列中存在一个错误 URL 时将所有值替换为默认值,您需要一次处理一个 URL,因此例如在

purrr:map_chr()
调用中使用
mutate()
或仅使用
rowwise() 
分组:

library(cropcircles)
library(tidyverse)
library(gt)
library(gtExtras)

# default image
default_headshot <- 
  cropcircles::circle_crop("https://images.ctfassets.net/iiozhi00a8lc/7eeTdW5zGYe0sW2ZlIle7E/990464d4b5e4e3b3a65cb6c56ce808ea/milb-alt.svg",
                           border_size = 12, border_colour = "black")

# return default_headshot in case of an error,
cc_possibly <- possibly(cropcircles::circle_crop, otherwise = default_headshot)


df_test <- structure(list(player = c("Matt Shaw", "Owen Caissie", 
                                     "Cameron Sisneros"), 
                          matchup_batter_id = c(807713L, 683357L, 815133L), 
                          HR = c(4L, 4L, 3L)), 
                     row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame")) %>%  
  mutate(headshot = paste0("https://img.mlbstatic.com/mlb-photos/image/upload/c_fill,g_auto/w_360/v1/people/",matchup_batter_id,"/headshot/milb/current")) %>% 
  # process URLs one by one
  rowwise() %>%
  mutate(headshot = cc_possibly(headshot, border_size = 12, border_colour = "black")) %>%
  # drop rowwise grouping
  ungroup() 

# Construct gt table
df_test %>% 
  gt() %>% 
  gt_img_rows(headshot, height = 37, img_source = "local") %>%
  # for reprex output
  gt_reprex_image()

创建于 2024-09-29,使用 reprex v2.1.1

© www.soinside.com 2019 - 2024. All rights reserved.