如何在R控制台自动完成输入?解决spotifyr包函数重复输入问题
get_artist_audio_features() Hey there! I’ve run into this exact prompt issue with spotifyr before—super annoying when you’re trying to automate workflows. Let’s break down a few solid ways to skip that manual input step:
1. Use the Artist ID Instead of Name (Most Reliable)
The prompt usually pops up because multiple artists might share the same name, so the function asks you to confirm which one you want. If you use the unique Spotify artist ID instead of the name, the function won’t need to prompt you at all—no ambiguity means no extra steps.
First, grab Drake’s Spotify ID (you can find this by checking his Spotify profile’s share link, or using search_spotify() from spotifyr to look it up programmatically). His official ID is 3TVXtAsR1Inumwj472S9r4. Then call the function like this:
library(spotifyr) # No prompt will appear here—direct ID lookup is unambiguous team1 <- get_artist_audio_features(artist_id = "3TVXtAsR1Inumwj472S9r4")
2. Simulate Console Input with withr::with_input()
If you need to stick with using the artist name, you can use the withr package to automatically feed the required input into the prompt. This works by mimicking what you’d type manually in the console.
First install and load withr, then wrap your function call:
library(spotifyr) library(withr) # Automatically supply "Drake" when the prompt appears team1 <- with_input(c("Drake"), { get_artist_audio_features(artist_name = "Drake") })
The c("Drake") part tells with_input() to send that string as the response to any interactive prompt inside the curly braces.
3. Check for a Non-Interactive Parameter (Quick Win)
Before jumping into workarounds, double-check the function’s documentation with ?get_artist_audio_features to see if there’s a built-in parameter to disable interactive prompts. Some functions include an interactive = FALSE flag or similar that skips confirmation steps. If such a parameter exists, using it is the cleanest solution.
内容的提问来源于stack exchange,提问作者J.Q

