{"action":"created","comment":{"url":"https://api.github.com/repos/PERTS/gymnast/pulls/comments/45940816","id":45940816,"diff_hunk":"@@ -3,6 +3,219 @@\n source(\"~/Sites/gymnast/util.R\", chdir = FALSE)\n source(\"~/Sites/gymnast/util_qualtrics_cleaning.R\", chdir = FALSE)\n \n+\n+############################### rename_columns function  #################################################\n+\n+rename_columns <- function(data, mapping) {\n+  # This function takes a data set and a mapping (which can be a list or data frame)\n+  # with columns \"old_name\" and \"new_name\". It uses util.recode to turn the old names\n+  # into the new names, making sure not to break any R name rules.\n+  \n+  # Patterns for forbidden names\n+  whitespace_pattern <- \"\\\\s\"\n+  starting_w_digit_pattern <- \"^\\\\.?[0-9]\"\n+  naughty_words <- c(\"if\", \"else\", \"repeat\", \"while\", \"function\", \"for\",\n+                     \"in\", \"next\", \"break\", \"TRUE\", \"FALSE\", \"NULL\", \"Inf\",\n+                     \"NaN\", \"NA\", \"NA_integer_\", \"NA_real_\", \"NA_complex_\", \"NA_character_\", NA)\n+  \n+  # if the new names in the mapping match any evil patterns, then complain\n+  if( any(mapping$new_name %in% naughty_words) |\n+      any(grepl(whitespace_pattern, mapping$new_name)) |\n+      any(grepl(starting_w_digit_pattern, mapping$new_name))) {\n+    stop(\"Invalid new names are present among: \",paste(mapping$new_name, collapse =\" \"))\n+  }\n+  \n+  # if old and new names are not the same length, then complain\n+  if(length(mapping$old_name) != length(mapping$new_name)) {\n+    stop(\"Error! Old names and new names are not the same length in your mapping for renaming.\")\n+  }\n+  \n+  names(data) <- util.recode(names(data),\n+                             mapping$old_name,\n+                             mapping$new_name)\n+  \n+  # if new column names in the final data have duplicates, then complain\n+  if (any(duplicated(names(data)))) {\n+    stop(\"Error! Duplicated names in your renamed data. Review your mapping for renaming.\")\n+  }\n+  \n+  return(data)\n+}\n+\n+################################ testing for rename_columns  ################################################\n+\n+######################## TEST 1: handling forbidden names\n+\n+\n+test_forbidden_names <- function() {\n+  \n+  my_mapping <- list(\n+    old_name = c(\"foo\", \"bar\", \"baz\",\"blerp\"),\n+    new_name = c(\"newfoo\", \"newbar\", \"newbaz\", NA)\n+  )\n+  \n+  d <- data.frame(\n+    foo = c(1,2,3,4,5),\n+    bar = c(6,7,8,9,10),\n+    baz = c(11,12,13,14,15),\n+    blerp = c(16,17,18,19,20)\n+  )\n+  \n+  success <- FALSE\n+  \n+  tryCatch(\n+    expr = {\n+      rename_columns(d,my_mapping)\n+    },\n+    error = function(e) {\n+      success <<- TRUE\n+    }\n+  )\n+  return(success)\n+}\n+\n+test_forbidden_names()\n+\n+######################## TEST 2: handling extra data columns that aren't in mapping\n+\n+test_extra_data_columns <- function() {\n+  \n+  my_mapping <- list(\n+    old_name = c(\"foo\"),\n+    new_name = c(\"newfoo\")\n+  )\n+  \n+  d <- data.frame(\n+    foo = c(1,2,3,4,5),\n+    blerp = c(16,17,18,19,20)\n+  )\n+  \n+  expected_output <- data.frame(\n+    newfoo = c(1,2,3,4,5),\n+    blerp = c(16,17,18,19,20)\n+  )\n+  \n+  new_d <- rename_columns(d,my_mapping)\n+  \n+  return(identical(new_d, expected_output))\n+  \n+}\n+\n+test_extra_data_columns()\n+\n+######################## TEST 3: extra mapping columns aren't present in data\n+\n+test_extra_mapping_columns <- function() {\n+  \n+  my_mapping <- list(\n+    old_name = c(\"foo\",\"bar\"),\n+    new_name = c(\"newfoo\",\"newbar\")\n+  )\n+  \n+  d <- data.frame(\n+    foo = c(1,2,3,4,5)\n+  )\n+  \n+  expected_output <- data.frame(\n+    newfoo = c(1,2,3,4,5)\n+  )\n+  \n+  new_d <- rename_columns(d,my_mapping)\n+  \n+  return(identical(new_d, expected_output))\n+  \n+}\n+\n+test_extra_mapping_columns()","path":"util_function_groups/column_operations.R","position":124,"original_position":127,"commit_id":"9173ffdb59ad0368f44691159086e4955d4079e4","original_commit_id":"5d3d845666fe87eac1cf9675220565c81298f4b0","user":{"login":"cmacrander","id":549138,"avatar_url":"https://avatars.githubusercontent.com/u/549138?v=3","gravatar_id":"","url":"https://api.github.com/users/cmacrander","html_url":"https://github.com/cmacrander","followers_url":"https://api.github.com/users/cmacrander/followers","following_url":"https://api.github.com/users/cmacrander/following{/other_user}","gists_url":"https://api.github.com/users/cmacrander/gists{/gist_id}","starred_url":"https://api.github.com/users/cmacrander/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmacrander/subscriptions","organizations_url":"https://api.github.com/users/cmacrander/orgs","repos_url":"https://api.github.com/users/cmacrander/repos","events_url":"https://api.github.com/users/cmacrander/events{/privacy}","received_events_url":"https://api.github.com/users/cmacrander/received_events","type":"User","site_admin":false},"body":"Oh, that's right, b/c you don't know where in their code someone else will source gymnast. Well then I think better than relying on a fancy name is wrapping it in a function:\r\n\r\n```\r\n(function () {\r\n    tests <- c(\r\n        function () print(\"it works!\"),\r\n        function () print(\"so fun!\")\r\n    )\r\n    for (t in tests) {\r\n        t()\r\n    }\r\n})()\r\n```","created_at":"2015-11-26T03:08:12Z","updated_at":"2015-11-26T03:08:12Z","html_url":"https://github.com/PERTS/gymnast/pull/3#discussion_r45940816","pull_request_url":"https://api.github.com/repos/PERTS/gymnast/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/PERTS/gymnast/pulls/comments/45940816"},"html":{"href":"https://github.com/PERTS/gymnast/pull/3#discussion_r45940816"},"pull_request":{"href":"https://api.github.com/repos/PERTS/gymnast/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/PERTS/gymnast/pulls/3","id":51611080,"html_url":"https://github.com/PERTS/gymnast/pull/3","diff_url":"https://github.com/PERTS/gymnast/pull/3.diff","patch_url":"https://github.com/PERTS/gymnast/pull/3.patch","issue_url":"https://api.github.com/repos/PERTS/gymnast/issues/3","number":3,"state":"open","locked":false,"title":"Var renaming","user":{"login":"dkgreene","id":2676911,"avatar_url":"https://avatars.githubusercontent.com/u/2676911?v=3","gravatar_id":"","url":"https://api.github.com/users/dkgreene","html_url":"https://github.com/dkgreene","followers_url":"https://api.github.com/users/dkgreene/followers","following_url":"https://api.github.com/users/dkgreene/following{/other_user}","gists_url":"https://api.github.com/users/dkgreene/gists{/gist_id}","starred_url":"https://api.github.com/users/dkgreene/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkgreene/subscriptions","organizations_url":"https://api.github.com/users/dkgreene/orgs","repos_url":"https://api.github.com/users/dkgreene/repos","events_url":"https://api.github.com/users/dkgreene/events{/privacy}","received_events_url":"https://api.github.com/users/dkgreene/received_events","type":"User","site_admin":false},"body":"This is the function Chris and I developed during the code retreat.","created_at":"2015-11-24T02:52:17Z","updated_at":"2015-11-26T03:08:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fd64e7c7f7d05177eb7cc8c758cd729bc7c5faf4","assignee":{"login":"cmacrander","id":549138,"avatar_url":"https://avatars.githubusercontent.com/u/549138?v=3","gravatar_id":"","url":"https://api.github.com/users/cmacrander","html_url":"https://github.com/cmacrander","followers_url":"https://api.github.com/users/cmacrander/followers","following_url":"https://api.github.com/users/cmacrander/following{/other_user}","gists_url":"https://api.github.com/users/cmacrander/gists{/gist_id}","starred_url":"https://api.github.com/users/cmacrander/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmacrander/subscriptions","organizations_url":"https://api.github.com/users/cmacrander/orgs","repos_url":"https://api.github.com/users/cmacrander/repos","events_url":"https://api.github.com/users/cmacrander/events{/privacy}","received_events_url":"https://api.github.com/users/cmacrander/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/PERTS/gymnast/pulls/3/commits","review_comments_url":"https://api.github.com/repos/PERTS/gymnast/pulls/3/comments","review_comment_url":"https://api.github.com/repos/PERTS/gymnast/pulls/comments{/number}","comments_url":"https://api.github.com/repos/PERTS/gymnast/issues/3/comments","statuses_url":"https://api.github.com/repos/PERTS/gymnast/statuses/9173ffdb59ad0368f44691159086e4955d4079e4","head":{"label":"PERTS:var-renaming","ref":"var-renaming","sha":"9173ffdb59ad0368f44691159086e4955d4079e4","user":{"login":"PERTS","id":7365769,"avatar_url":"https://avatars.githubusercontent.com/u/7365769?v=3","gravatar_id":"","url":"https://api.github.com/users/PERTS","html_url":"https://github.com/PERTS","followers_url":"https://api.github.com/users/PERTS/followers","following_url":"https://api.github.com/users/PERTS/following{/other_user}","gists_url":"https://api.github.com/users/PERTS/gists{/gist_id}","starred_url":"https://api.github.com/users/PERTS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PERTS/subscriptions","organizations_url":"https://api.github.com/users/PERTS/orgs","repos_url":"https://api.github.com/users/PERTS/repos","events_url":"https://api.github.com/users/PERTS/events{/privacy}","received_events_url":"https://api.github.com/users/PERTS/received_events","type":"Organization","site_admin":false},"repo":{"id":46515070,"name":"gymnast","full_name":"PERTS/gymnast","owner":{"login":"PERTS","id":7365769,"avatar_url":"https://avatars.githubusercontent.com/u/7365769?v=3","gravatar_id":"","url":"https://api.github.com/users/PERTS","html_url":"https://github.com/PERTS","followers_url":"https://api.github.com/users/PERTS/followers","following_url":"https://api.github.com/users/PERTS/following{/other_user}","gists_url":"https://api.github.com/users/PERTS/gists{/gist_id}","starred_url":"https://api.github.com/users/PERTS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PERTS/subscriptions","organizations_url":"https://api.github.com/users/PERTS/orgs","repos_url":"https://api.github.com/users/PERTS/repos","events_url":"https://api.github.com/users/PERTS/events{/privacy}","received_events_url":"https://api.github.com/users/PERTS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/PERTS/gymnast","description":"Collection of useful functions for data analysis and visualization.","fork":false,"url":"https://api.github.com/repos/PERTS/gymnast","forks_url":"https://api.github.com/repos/PERTS/gymnast/forks","keys_url":"https://api.github.com/repos/PERTS/gymnast/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PERTS/gymnast/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PERTS/gymnast/teams","hooks_url":"https://api.github.com/repos/PERTS/gymnast/hooks","issue_events_url":"https://api.github.com/repos/PERTS/gymnast/issues/events{/number}","events_url":"https://api.github.com/repos/PERTS/gymnast/events","assignees_url":"https://api.github.com/repos/PERTS/gymnast/assignees{/user}","branches_url":"https://api.github.com/repos/PERTS/gymnast/branches{/branch}","tags_url":"https://api.github.com/repos/PERTS/gymnast/tags","blobs_url":"https://api.github.com/repos/PERTS/gymnast/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PERTS/gymnast/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PERTS/gymnast/git/refs{/sha}","trees_url":"https://api.github.com/repos/PERTS/gymnast/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PERTS/gymnast/statuses/{sha}","languages_url":"https://api.github.com/repos/PERTS/gymnast/languages","stargazers_url":"https://api.github.com/repos/PERTS/gymnast/stargazers","contributors_url":"https://api.github.com/repos/PERTS/gymnast/contributors","subscribers_url":"https://api.github.com/repos/PERTS/gymnast/subscribers","subscription_url":"https://api.github.com/repos/PERTS/gymnast/subscription","commits_url":"https://api.github.com/repos/PERTS/gymnast/commits{/sha}","git_commits_url":"https://api.github.com/repos/PERTS/gymnast/git/commits{/sha}","comments_url":"https://api.github.com/repos/PERTS/gymnast/comments{/number}","issue_comment_url":"https://api.github.com/repos/PERTS/gymnast/issues/comments{/number}","contents_url":"https://api.github.com/repos/PERTS/gymnast/contents/{+path}","compare_url":"https://api.github.com/repos/PERTS/gymnast/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PERTS/gymnast/merges","archive_url":"https://api.github.com/repos/PERTS/gymnast/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PERTS/gymnast/downloads","issues_url":"https://api.github.com/repos/PERTS/gymnast/issues{/number}","pulls_url":"https://api.github.com/repos/PERTS/gymnast/pulls{/number}","milestones_url":"https://api.github.com/repos/PERTS/gymnast/milestones{/number}","notifications_url":"https://api.github.com/repos/PERTS/gymnast/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PERTS/gymnast/labels{/name}","releases_url":"https://api.github.com/repos/PERTS/gymnast/releases{/id}","created_at":"2015-11-19T19:24:51Z","updated_at":"2015-11-19T19:39:07Z","pushed_at":"2015-11-26T02:42:34Z","git_url":"git://github.com/PERTS/gymnast.git","ssh_url":"git@github.com:PERTS/gymnast.git","clone_url":"https://github.com/PERTS/gymnast.git","svn_url":"https://github.com/PERTS/gymnast","homepage":null,"size":358,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"PERTS:master","ref":"master","sha":"3b3f1d42b63b4843cfc946d6ca9be144db4bb30d","user":{"login":"PERTS","id":7365769,"avatar_url":"https://avatars.githubusercontent.com/u/7365769?v=3","gravatar_id":"","url":"https://api.github.com/users/PERTS","html_url":"https://github.com/PERTS","followers_url":"https://api.github.com/users/PERTS/followers","following_url":"https://api.github.com/users/PERTS/following{/other_user}","gists_url":"https://api.github.com/users/PERTS/gists{/gist_id}","starred_url":"https://api.github.com/users/PERTS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PERTS/subscriptions","organizations_url":"https://api.github.com/users/PERTS/orgs","repos_url":"https://api.github.com/users/PERTS/repos","events_url":"https://api.github.com/users/PERTS/events{/privacy}","received_events_url":"https://api.github.com/users/PERTS/received_events","type":"Organization","site_admin":false},"repo":{"id":46515070,"name":"gymnast","full_name":"PERTS/gymnast","owner":{"login":"PERTS","id":7365769,"avatar_url":"https://avatars.githubusercontent.com/u/7365769?v=3","gravatar_id":"","url":"https://api.github.com/users/PERTS","html_url":"https://github.com/PERTS","followers_url":"https://api.github.com/users/PERTS/followers","following_url":"https://api.github.com/users/PERTS/following{/other_user}","gists_url":"https://api.github.com/users/PERTS/gists{/gist_id}","starred_url":"https://api.github.com/users/PERTS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PERTS/subscriptions","organizations_url":"https://api.github.com/users/PERTS/orgs","repos_url":"https://api.github.com/users/PERTS/repos","events_url":"https://api.github.com/users/PERTS/events{/privacy}","received_events_url":"https://api.github.com/users/PERTS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/PERTS/gymnast","description":"Collection of useful functions for data analysis and visualization.","fork":false,"url":"https://api.github.com/repos/PERTS/gymnast","forks_url":"https://api.github.com/repos/PERTS/gymnast/forks","keys_url":"https://api.github.com/repos/PERTS/gymnast/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PERTS/gymnast/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PERTS/gymnast/teams","hooks_url":"https://api.github.com/repos/PERTS/gymnast/hooks","issue_events_url":"https://api.github.com/repos/PERTS/gymnast/issues/events{/number}","events_url":"https://api.github.com/repos/PERTS/gymnast/events","assignees_url":"https://api.github.com/repos/PERTS/gymnast/assignees{/user}","branches_url":"https://api.github.com/repos/PERTS/gymnast/branches{/branch}","tags_url":"https://api.github.com/repos/PERTS/gymnast/tags","blobs_url":"https://api.github.com/repos/PERTS/gymnast/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PERTS/gymnast/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PERTS/gymnast/git/refs{/sha}","trees_url":"https://api.github.com/repos/PERTS/gymnast/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PERTS/gymnast/statuses/{sha}","languages_url":"https://api.github.com/repos/PERTS/gymnast/languages","stargazers_url":"https://api.github.com/repos/PERTS/gymnast/stargazers","contributors_url":"https://api.github.com/repos/PERTS/gymnast/contributors","subscribers_url":"https://api.github.com/repos/PERTS/gymnast/subscribers","subscription_url":"https://api.github.com/repos/PERTS/gymnast/subscription","commits_url":"https://api.github.com/repos/PERTS/gymnast/commits{/sha}","git_commits_url":"https://api.github.com/repos/PERTS/gymnast/git/commits{/sha}","comments_url":"https://api.github.com/repos/PERTS/gymnast/comments{/number}","issue_comment_url":"https://api.github.com/repos/PERTS/gymnast/issues/comments{/number}","contents_url":"https://api.github.com/repos/PERTS/gymnast/contents/{+path}","compare_url":"https://api.github.com/repos/PERTS/gymnast/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PERTS/gymnast/merges","archive_url":"https://api.github.com/repos/PERTS/gymnast/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PERTS/gymnast/downloads","issues_url":"https://api.github.com/repos/PERTS/gymnast/issues{/number}","pulls_url":"https://api.github.com/repos/PERTS/gymnast/pulls{/number}","milestones_url":"https://api.github.com/repos/PERTS/gymnast/milestones{/number}","notifications_url":"https://api.github.com/repos/PERTS/gymnast/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PERTS/gymnast/labels{/name}","releases_url":"https://api.github.com/repos/PERTS/gymnast/releases{/id}","created_at":"2015-11-19T19:24:51Z","updated_at":"2015-11-19T19:39:07Z","pushed_at":"2015-11-26T02:42:34Z","git_url":"git://github.com/PERTS/gymnast.git","ssh_url":"git@github.com:PERTS/gymnast.git","clone_url":"https://github.com/PERTS/gymnast.git","svn_url":"https://github.com/PERTS/gymnast","homepage":null,"size":358,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/PERTS/gymnast/pulls/3"},"html":{"href":"https://github.com/PERTS/gymnast/pull/3"},"issue":{"href":"https://api.github.com/repos/PERTS/gymnast/issues/3"},"comments":{"href":"https://api.github.com/repos/PERTS/gymnast/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/PERTS/gymnast/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/PERTS/gymnast/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/PERTS/gymnast/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/PERTS/gymnast/statuses/9173ffdb59ad0368f44691159086e4955d4079e4"}}}}