Artie Ladie
Artie Ladie

Reputation: 521

Function not working as expected within code, but works perfect in console

I have R function [here is script, here is package] that works perfectly in console, but when I build and load package, something goes wrong

In console environment, I create vector, create function, assign the variable to pass to function, and output is as expected when I execute 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"        
> 

However, when I run the same script, build the package locally and execute ListPalette(listname), I get following

> ListPalette("RanglaPunjab")
NULL

Something is amiss. I thought it might be a silly oversight, but I've been wrangling over this for more than an hour .... please guide.

Upvotes: 0

Views: 60

Answers (1)

Try it without quotes.

ListPalette(RanglaPunjab)

If you want to use the name of the list as a character, you must use 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"  

Upvotes: 1

Related Questions