功能在代码中没有按预期工作,但在控制台中工作得很好

时间:2018-05-28 14:28:40

标签: r

我有R函数[这里是script,这里是package] ,它在控制台中完美运行,但是当我构建并加载包时出现问题

在控制台环境中,我创建向量,创建函数,分配变量以传递给函数,并在执行ListPalette(listname)时按预期输出

> PunjabiPalette <- list (
+     AmritsariKulcha = c("#e3e4d9", "#ebdc9c", "#b3340e", "#67140a", "#2a231d"),
+     CholeBhature = c("#7cab70", "#d9bf9c", "#a04d05", "#995f7e", "#972107"),
+     FieldsOfPunjab = c("#fda726", "#d75b07", "#702e06", "#514617", "#313407"),
+     FieldsOfPunjab2 = c("#9aa5b4", "#516e9c", "#13306a", "#94aa0b", "#a36316"),
+     GoldenTemple = c("#bdcad0", "#5f8abf", "#ffd860", "#d88821", "#672006"),
+     GoldenTemple2 = c("#7d84cb", "#374890","#c2592e", "#fa5102", "#722416"),
+     Pindh = c("#5eb39c", "#1f6562","#2168c2", "#d77e5f", "#5f3e25"),
+     SohniMahiwal = c("#dc6478", "#a9365a", "#f4420e", "#403c61", "#313f42"),
+     HeerRanjha = c("#93dd7d","#3272b6", "#ec9382", "#ab3a40", "#072246"),
+     Gidha = c("#fdea6e", "#4aec6a", "#fb7894", "#f13111", "#2584a0"),
+     Gidha2 = c("#fb9961", "#f13375", "#771341", "#2d3c2f", "#ea263c"),
+     Teej = c("#22325f", "#88ce64", "#fbd234", "#b8091f", "#682f4e"),
+     Phulkari = c("#efa20b", "#04a193", "#14555d","#820203", "#ed2e06"),
+     Phulkari2 = c("#9c1a41", "#42a4e8", "#3a35da", "#ee523c", "#3e167c"),
+     Jutti = c("#460809", "#00699e", "#391b72", "#03471d", "#ba0841"),
+     Jutti2 = c("#e278e5", "#13187e", "#fb6225", "#f23561", "#d2b88f"),
+     Jutti3 = c("#6fa42c", "#db3717", "#051a8d", "#ef38a7", "#202c3d"),
+     Paranda = c("#eaa32b", "#f45d59", "#c33dd2", "#92214c", "#201274")
+ )
> ListPalette <- function(listname){
+     names(listname)
+ }
> listname <- PunjabiPalette
> ListPalette(listname)
 [1] "AmritsariKulcha" "CholeBhature"    "FieldsOfPunjab"  "FieldsOfPunjab2" "GoldenTemple"    "GoldenTemple2"  
 [7] "Pindh"           "SohniMahiwal"    "HeerRanjha"      "Gidha"           "Gidha2"          "Teej"           
[13] "Phulkari"        "Phulkari2"       "Jutti"           "Jutti2"          "Jutti3"          "Paranda"        
> 

但是,当我运行相同的脚本时,在本地构建包并执行ListPalette(listname),我得到以下

> ListPalette("RanglaPunjab")
NULL

有些不对劲。我认为这可能是一个愚蠢的疏忽,但我已经争吵了一个多小时......请指导。

1 个答案:

答案 0 :(得分:1)

不加引号尝试。

ListPalette(RanglaPunjab)

如果要将列表名称用作字符,则必须使用get

ListPalette <- function(listname){
     list <- get(listname)
     names(list)
}
ListPalette("PunjabiPalette")
 [1] "AmritsariKulcha" "CholeBhature"    "FieldsOfPunjab"  "FieldsOfPunjab2" "GoldenTemple"    "GoldenTemple2"   "Pindh"          
 [8] "SohniMahiwal"    "HeerRanjha"      "Gidha"           "Gidha2"          "Teej"            "Phulkari"        "Phulkari2"      
[15] "Jutti"           "Jutti2"          "Jutti3"          "Paranda"