You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中运行rescale函数时出现unused arguments错误的求助

问题解决:rescale函数冲突导致的参数错误

错误原因

你的代码同时用到了imager包和scales(或关联的dplyr/plyr)包,这两个包都包含rescale函数,但参数定义完全不同:

  • imager::rescale:用于图像亮度/对比度调整,参数为x, s, m,没有from参数
  • scales::rescale:用于数值范围转换,参数为x, to, from,完全匹配你代码中把色相从0-360缩放到0-1的需求

当imager包加载后,rescale会默认调用imager::rescale,直接触发unused argument (from = c(0, 360))错误。你卸载重装plyr/dplyr偶尔生效,只是依赖包加载顺序临时让scales的rescale覆盖了imager的,这种方式完全不稳定。

修复后的代码

直接在rescale前添加明确的包名限定,强制使用scales::rescale,彻底避免函数冲突:

get_colorPal <- function(im, n=8, cs="RGB"){
  tmp <- im %>% 
    image_resize("100") %>% 
    image_quantize(max=n, colorspace=cs) %>%
    magick2cimg() %>%
    RGBtoHSV() %>%
    as.data.frame(wide="c") %>%
    # 显式指定scales包的rescale函数
    mutate(hex=hsv(scales::rescale(c.1, from=c(0,360)), c.2, c.3),
           hue = c.1,
           sat = c.2,
           value = c.3) %>%
    count(hex, hue, sat, value, sort=T) %>% 
    mutate(colorspace = cs)

  return(tmp %>% select(colorspace, hex, hue, sat, value, n))
}

# 示例调用
get_colorPal(im)

注意事项

  • 确保提前安装并加载scales包,未安装的话先运行:install.packages("scales")和library(scales)
  • 这种显式指定包名的写法,彻底摆脱了对包加载顺序的依赖,代码会稳定运行

内容的提问来源于stack exchange,提问作者POGB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:35:36