I've been reading a bit about Emacs Lisp and came up with the following solution. This is my very first Lisp function and as such I am not sure whether I've covered all pitfalls. It requires running iESS.
;; insert R library line using helm
(defun helm-ess-R-library-name-insert ()
"Insert 'library(_library-name_)'."
(interactive)
(let
((lib-name (helm :sources (helm-build-in-buffer-source "R-library"
:data (ess-get-words-from-vector
"print(.packages(T), max=1e6)\n"))
:buffer "*R library*")))
(if (not (string= lib-name nil))
(if (string= (message "%s" major-mode) "ess-mode")
(insert (format "library(%s)\n" lib-name))
(ess-eval-linewise (format "library('%s')\n" lib-name))))))
;; re-map the key from ess-load-library
(define-key ess-mode-map (kbd "C-c C-e l") 'helm-ess-R-library-name-insert)