Friday, March 11, 2016

New R package: a dictionary with arbitrary keys and values

Coming from Python, the absence of a real dictionary in R has annoyed me for quite some time. Now, I actually needed to use vectors as keys in R:

d <- dict()

d[[1]] <- 42
d[[c(2, 3)]] <- "Hello!"
d[["foo"]] <- "bar"
d[[c(2, 3)]]
d$get("not here", "default")


# [[ ]] gives an error for unknown keys

Under the hood, separate C++ dictionaries (unordered_map) are created for the different types of keys. Using R's flexible types that are reflected in Rcpp (as SEXP), such a dictionary can store both numbers and strings (and other objects) at the same time.

The package is available on GitHub:


Andre Mikulec said...

I am getting this error when I try to install.

> devtools::install_github("mkuhn/dict")
Downloading GitHub repo mkuhn/dict@master
Installing dict
"C:/Users/AnonymousUser/Desktop/R-Portable.3.2.3/App/R-Portable/bin/x64/R" \
--no-site-file --no-environ --no-save --no-restore CMD INSTALL \
"C:/Users/AnonymousUser/Desktop/R-Portable.3.2.3/TEMP/Rtmp0Mt2K5/devtools15244c8e4404/mkuhn-dict-3bfe4d0" \
--library="C:/Users/AnonymousUser/Desktop/R-Portable.3.2.3/R_LIBS_USER" \
--with-keep.source --install-tests

* installing *source* package 'dict' ...
** libs

*** arch - i386
g++ -m32 -I"C:/Users/ANONYM~1/Desktop/R-PORT~1.3/App/R-PORT~1/include" -DNDEBUG `Rscript -e "Rcpp:::CxxFlags()"` `Rscript -e "Rcpp:::Cxx0xFlags()"` -I"C:/Users/AnonymousUser/Desktop/R-Portable.3.2.3/R_LIBS_USER/Rcpp/include" -I"C:/Users/AnonymousUser/De
sktop/R-Portable.3.2.3/HOME/.checkpoint/2016-01-05/lib/x86_64-w64-mingw32/3.2.3/BH/include" -I"d:/RCompile/r-compiling/local/local323/include" -O2 -Wall -gdwarf-2 -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -m32 -I"C:/Users/ANONYM~1/Desktop/R-PORT~1.3/App/R-PORT~1/include" -DNDEBUG `Rscript -e "Rcpp:::CxxFlags()"` `Rscript -e "Rcpp:::Cxx0xFlags()"` -I"C:/Users/AnonymousUser/Desktop/R-Portable.3.2.3/R_LIBS_USER/Rcpp/include" -I"C:/Users/AnonymousUser/De
sktop/R-Portable.3.2.3/HOME/.checkpoint/2016-01-05/lib/x86_64-w64-mingw32/3.2.3/BH/include" -I"d:/RCompile/r-compiling/local/local323/include" -O2 -Wall -gdwarf-2 -mtune=core2 -c dict.cpp -o dict.o
dict.cpp:19:1: error: expected unqualified-id before 'using'
dict.cpp: In member function 'T Dict::get_or_stop(SEXPREC*&) [with T = Rcpp::Vector<14, Rcpp::PreserveStorage>, SEXP = SEXPREC*]':
dict.cpp:99:5: warning: control reaches end of non-void function [-Wreturn-type]
dict.cpp: In member function 'T Dict::get_or_stop(SEXPREC*&) [with T = SEXPREC*, SEXP = SEXPREC*]':
dict.cpp:99:5: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [dict.o] Error 1
Warning: running command 'make -f "Makevars" -f "C:/Users/ANONYM~1/Desktop/R-PORT~1.3/App/R-PORT~1/etc/i386/Makeconf" -f "C:/Users/ANONYM~1/Desktop/R-PORT~1.3/App/R-PORT~1/share/make/" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD
)' SHLIB="dict.dll" OBJECTS="RcppExports.o dict.o"' had status 2
ERROR: compilation failed for package 'dict'
* removing 'C:/Users/AnonymousUser/Desktop/R-Portable.3.2.3/R_LIBS_USER/dict'
Error: Command failed (1)
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] chron_2.3-47

loaded via a namespace (and not attached):
[1] httr_1.0.0 R6_2.1.2 magrittr_1.5 tools_3.2.3 curl_0.9.4
[6] memoise_0.2.1 stringi_1.0-1 stringr_1.0.0 digest_0.6.9 devtools_1.9.1

Andre Mikulec

Michael Kuhn said...

Can you try again with the newest version from GitHub? If it still doesn't work, can you please check your gcc compiler version (not sure how to do that on Windows)?

Unknown said...

I had the same problems installing (the ... in the above post hides dozens of C++ errors), with gcc-4.6.3, which is the latest version in Windows Rtools. The new Rtools for R 3.3.0 will include 4.9.3 if that makes a difference?

Michael Kuhn said...

From what I've read, gcc 4.6 doesn't have good C++11 support. So having 4.9 in the new Rtools version should make a difference.

cellocgw said...

I don't see any man page or help pages. Can you please provide these? For example, I'm not sure what your intended use of d[[c(2,3)]] is. If that's "hello," then what is d[[2]] ?

(There's a reason CRAN rejects packages not fully documented :-) )

Michael Kuhn said...

Eventually, yes. Not being able to do it via roxygen is holding me back, though.

(Sure, and there's a reason I'm posting this on GitHub. ;-) )