{"action":"created","issue":{"url":"https://api.github.com/repos/smbache/loggr/issues/13","labels_url":"https://api.github.com/repos/smbache/loggr/issues/13/labels{/name}","comments_url":"https://api.github.com/repos/smbache/loggr/issues/13/comments","events_url":"https://api.github.com/repos/smbache/loggr/issues/13/events","html_url":"https://github.com/smbache/loggr/issues/13","id":80995940,"number":13,"title":"Easier message generation","user":{"login":"Mullefa","id":4085817,"avatar_url":"https://avatars.githubusercontent.com/u/4085817?v=3","gravatar_id":"","url":"https://api.github.com/users/Mullefa","html_url":"https://github.com/Mullefa","followers_url":"https://api.github.com/users/Mullefa/followers","following_url":"https://api.github.com/users/Mullefa/following{/other_user}","gists_url":"https://api.github.com/users/Mullefa/gists{/gist_id}","starred_url":"https://api.github.com/users/Mullefa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mullefa/subscriptions","organizations_url":"https://api.github.com/users/Mullefa/orgs","repos_url":"https://api.github.com/users/Mullefa/repos","events_url":"https://api.github.com/users/Mullefa/events{/privacy}","received_events_url":"https://api.github.com/users/Mullefa/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-05-26T14:46:59Z","updated_at":"2015-05-26T19:32:36Z","closed_at":null,"body":"Maybe this needs consideration outside the development of `loggr`, but it would be great if the logging functions supported string interpolation e.g.\r\n\r\n```R\r\n...\r\nuser_name = \"Alice\"\r\nlog_info(\"User $user_name has logged in\")\r\n```\r\n\r\nor failing that:\r\n\r\n```R\r\n...\r\nuser_name = \"Alice\"\r\nlog_info(\"User $user_name has logged in\", user_name = user_name)\r\n```"},"comment":{"url":"https://api.github.com/repos/smbache/loggr/issues/comments/105642739","html_url":"https://github.com/smbache/loggr/issues/13#issuecomment-105642739","issue_url":"https://api.github.com/repos/smbache/loggr/issues/13","id":105642739,"user":{"login":"smbache","id":6285210,"avatar_url":"https://avatars.githubusercontent.com/u/6285210?v=3","gravatar_id":"","url":"https://api.github.com/users/smbache","html_url":"https://github.com/smbache","followers_url":"https://api.github.com/users/smbache/followers","following_url":"https://api.github.com/users/smbache/following{/other_user}","gists_url":"https://api.github.com/users/smbache/gists{/gist_id}","starred_url":"https://api.github.com/users/smbache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smbache/subscriptions","organizations_url":"https://api.github.com/users/smbache/orgs","repos_url":"https://api.github.com/users/smbache/repos","events_url":"https://api.github.com/users/smbache/events{/privacy}","received_events_url":"https://api.github.com/users/smbache/received_events","type":"User","site_admin":false},"created_at":"2015-05-26T19:32:36Z","updated_at":"2015-05-26T19:32:36Z","body":"How about introducing a utility function say `string_fill` ala in the example below (function def first, then a small example). Not sure if this should be default behavior and/or optional though. Will think some more. Also, function name and description may not be optimal...\r\n\r\n```R\r\n#' Fill String with Named Values\r\n#'\r\n#' This function substitutes name placeholders with the values found in the\r\n#' specified environment. Placeholders are names enclosed in backticks, e.g. \r\n#' `name`. It is possible to specify formats as with \\code{\\link{sprintf}}, e.g. \r\n#' `value%.2f`; here the name is \\code{value} and the format is \\code{%.2f}.\r\n#'\r\n#' @details It is not possible to use e.g. `name[1]` or other forms of indexing.\r\n#'   \r\n#' @param string A character string with name placeholders.\r\n#' @param env An environment where to search for the values.\r\n#'\r\n#' @return A character string\r\nstring_fill <- function(string, env = parent.frame())\r\n{\r\n  # Look for names enclosed by backticks, e.g. `name`. Ignore doubles, ``.\r\n  pattern    <- \"(?<!`)`(?!`)(.*?)((?<!`)`(?!`))\"\r\n  match_data <- gregexpr(pattern, string, perl = TRUE)\r\n  matches    <- regmatches(string, match_data)[[1]]\r\n  \r\n  # Extract the names\r\n  names      <- gsub(\"(%.*)|`\", \"\", matches)\r\n  \r\n  # Fetch the values from the given environment.\r\n  values     <- lapply(gsub(\"(%.*)|`\", \"\", matches), get, \r\n                       env = env, inherits = TRUE)\r\n  \r\n  # Extract formats; default to %s if not given.\r\n  formats    <- vapply(strsplit(gsub(\"`\", \"\", matches), \"%\"), \r\n                       function(m) if (length(m) == 1) \"s\" else m[length(m)], \r\n                       character(1))\r\n  \r\n  # Create the replacements from the values and formats\r\n  replacements <- mapply(sprintf, paste0(\"%\", formats), values)\r\n  \r\n  # Perform the replacement\r\n  regmatches(string, match_data) <- list(replacements)\r\n  \r\n  return(string)\r\n}\r\n\r\nuser_name <- \"Stefan\"\r\naccount   <- 1337\r\nammount   <- 66.6\r\n\r\nmsg <- \r\n  string_fill(\"User `user_name` has account `account%d` with $`ammount%.2f`.\")\r\n\r\nprint(msg)\r\n[1] \"User Stefan has account 1337 with $66.60.\"\r\n```"}}