1

I am using the Tensorflow package in R, and I need to merge 2 dictionaries to create a single feed.dict.

In python this is simple. What is the best way to get this done in R? For reasons out of my control, I cannot use Python directly.

EDIT

I have come up with the following work around. If anyone has a better solution please post an answer.

mergeDictionaries = function(dict1, dict2){
  list1 = py_to_r(dict1)
  list2 = py_to_r(dict2)
  r_to_py(c(list1, list2))
}
Chechy Levas
  • 2,206
  • 1
  • 13
  • 28

1 Answers1

2

The newly created reticulate package which provides an R interface to python modules can be used in your case. Assuming that you have Python 3 installed, you can run the following code chunk,

py_str = "x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z = dict(list(x.items()) + list(y.items()))"


py_dict = reticulate::py_run_string(code = py_str, local = FALSE, convert = FALSE)
py_dict$z

The result will be,

{'a': 1, 'b': 10, 'c': 11}
lampros
  • 581
  • 5
  • 12