{"id":21089,"date":"2022-11-22T10:21:00","date_gmt":"2022-11-22T08:21:00","guid":{"rendered":"https:\/\/www.wjst.de\/blog\/?p=21089"},"modified":"2022-11-26T19:26:18","modified_gmt":"2022-11-26T17:26:18","slug":"a-r-function-creating-96-well-plate-sample-assignment","status":"publish","type":"post","link":"https:\/\/www.wjst.de\/blog\/sciencesurf\/2022\/11\/a-r-function-creating-96-well-plate-sample-assignment\/","title":{"rendered":"A R function creating 96 well plate sample assignment"},"content":{"rendered":"<p>Most recently I had to add forgotten data to a SQL server database that was in use only until 2010. Script language was Cold Fusion at that time creating a visual interface to 96 well plates where proband IDs are assigned to plate positions by drop down fields.<\/p>\n<p>R doesn&#8217;t have any good way for formatted data entry. Some shiny apps would be helpful but a bit overkill here. Data export into a javascript framework would be also a more professional solution while I just needed only a quick way to modify my database. <\/p>\n<p>So I came up with some R code producing html code embedding javascript functions that can export the result as SQL code, so a 4 language mixup.<\/p>\n<figure id=\"attachment_21092\" aria-describedby=\"caption-attachment-21092\" style=\"width: 620px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.wjst.de\/blog\/wp-content\/uploads\/2022\/11\/Bildschirmfoto-2022-11-22-um-08.52.57.jpg\" data-rel=\"key-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"key wp-image-21092 size-medium\" src=\"https:\/\/www.wjst.de\/blog\/wp-content\/uploads\/2022\/11\/Bildschirmfoto-2022-11-22-um-08.52.57-620x238.jpg\" alt=\"\" width=\"620\" height=\"238\" srcset=\"https:\/\/www.wjst.de\/blog\/wp-content\/uploads\/2022\/11\/Bildschirmfoto-2022-11-22-um-08.52.57-620x238.jpg 620w, https:\/\/www.wjst.de\/blog\/wp-content\/uploads\/2022\/11\/Bildschirmfoto-2022-11-22-um-08.52.57-768x295.jpg 768w, https:\/\/www.wjst.de\/blog\/wp-content\/uploads\/2022\/11\/Bildschirmfoto-2022-11-22-um-08.52.57.jpg 1097w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><figcaption id=\"caption-attachment-21092\" class=\"wp-caption-text\">96 well plate assignment<\/figcaption><\/figure>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ndataentry &lt;- function(plate_id,city) {\r\n# get orig IDs from database first\r\n\r\n# position identifier A01, A02...\r\npos = NULL\r\nfor (i in 1:8) {\r\n  pos = c(pos, paste0( rep( LETTERS&#x5B;i], 12), str_pad( 1:12 ,2,pad=&quot;0&quot; ) ) )\r\n}\r\n\t\r\nHTML &lt;- paste('&lt;html&gt;\r\n&lt;script&gt;\r\nfunction download(filename, text) {\r\n  var element = document.createElement(&quot;a&quot;);\r\n  element.setAttribute(&quot;href&quot;, &quot;data:text\/plain;charset=utf-8,&quot; + encodeURIComponent(text));\r\n  element.setAttribute(&quot;download&quot;, filename);\r\n  element.style.display = &quot;none&quot;;\r\n  document.body.appendChild(element);\r\n  element.click();\r\n  document.body.removeChild(element);\r\n}\r\nfunction saveValues() {\r\n  var frm = document.getElementById(&quot;f&quot;);\r\n  var params = &quot;&quot;;\r\n  var sample_id = ',sample_id,';\r\n  for( var i=0; i&lt;frm.length; i++ ) {\r\n    sample_id++;\r\n    var fieldId = frm.elements&#x5B;i].id;\r\n    var fieldValue = frm.elements&#x5B;i].value;\r\n    if (fieldValue) params += &quot;INSERT INTO samples (plate_id,plate_position,patient_id) VALUES (',plate_id,',&quot; + fieldId + &quot;,&quot; + fieldValue + &quot;)\\\\n&quot;;\r\n  }\r\n  download(&quot;',plate_id,'.SQL&quot;,params);\r\n}\r\n&lt;\/script&gt;\r\n&lt;body&gt;\r\n&lt;form id=&quot;f&quot;&gt;')\r\n\t\r\n# dropdowns\r\nsel ='&lt;option value=&quot;&quot;&gt;&lt;\/option&gt;'\r\nfor (i in 1:dim(patients)&#x5B;1]) {\r\n  sel &lt;- paste0(sel,'&lt;option value=',patients&#x5B;i,&quot;patient_id&quot;],'&gt;',patients&#x5B;i,&quot;Orig_ID&quot;],'&lt;\/option&gt;\\n')\r\n}\r\nfor (i in pos) {\r\n  if (substr(i,2,3) == &quot;01&quot;) HTML &lt;- paste0(HTML,'&lt;BR&gt;')\r\n  HTML &lt;- paste(HTML,'&lt;select id=',i,'&gt;',sel,'&lt;\/select&gt;')\r\n}\r\n\t\r\nHTML &lt;- paste(HTML,'&lt;\/form&gt;\r\n&lt;br&gt;&lt;input name=&quot;save&quot; type=&quot;button&quot; value=&quot;SQL&quot; onclick=&quot;saveValues(); return false&quot;\/&gt;\r\n&lt;\/body&gt;&lt;\/html&gt;')\r\n\t\r\nsink( paste('plate.html') )\r\ncat(HTML)\r\nsink()\r\n}\r\ndataentry(725,'city')\r\n<\/pre>\n\n<p>&nbsp;<\/p>\n<div class=\"bottom-note\">\n  <span class=\"mod1\">CC-BY-NC Science Surf , accessed 03.04.2026<\/span>\n <\/div>","protected":false},"excerpt":{"rendered":"<p>Most recently I had to add forgotten data to a SQL server database that was in use only until 2010. Script language was Cold Fusion at that time creating a visual interface to 96 well plates where proband IDs are assigned to plate positions by drop down fields. R doesn&#8217;t have any good way for &hellip; <a href=\"https:\/\/www.wjst.de\/blog\/sciencesurf\/2022\/11\/a-r-function-creating-96-well-plate-sample-assignment\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">A R function creating 96 well plate sample assignment<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[3989,2893,3986,3988,3987,3983,2823,1863,2771],"class_list":["post-21089","post","type-post","status-publish","format-standard","hentry","category-computer-software","tag-96-well-plate","tag-r","tag-sql","tag-biology","tag-data-entry","tag-dropdown","tag-html","tag-interface","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/21089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/comments?post=21089"}],"version-history":[{"count":8,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/21089\/revisions"}],"predecessor-version":[{"id":21155,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/21089\/revisions\/21155"}],"wp:attachment":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/media?parent=21089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/categories?post=21089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/tags?post=21089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}