{"id":224,"date":"2011-07-07T19:43:26","date_gmt":"2011-07-07T23:43:26","guid":{"rendered":"http:\/\/bitc.bme.emory.edu\/~lzhou\/blogs\/?p=224"},"modified":"2011-07-11T11:34:04","modified_gmt":"2011-07-11T15:34:04","slug":"using-interactive-r-script-in-batch-mode","status":"publish","type":"post","link":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/?p=224","title":{"rendered":"Using interactive R script in batch mode"},"content":{"rendered":"<p>According to R manual, <a href=\"https:\/\/owa.emory.edu\/owa\/redir.aspx?C=eb78fb1534e1480fb2c5867d23a26909&amp;URL=http%3a%2f%2fcran.r-project.org%2fdoc%2fmanuals%2ffullrefman.pdf%2c\" target=\"_blank\"> http:\/\/cran.r-project.org\/doc\/manuals\/fullrefman.pdf,<\/a> page 411,<br \/>\n&#8221;<br \/>\nValue<br \/>\nA character vector of length one. Both leading and trailing spaces and tabs are stripped from the<br \/>\nresult.<br \/>\nIn non-interactive use the result is as if the response was RETURN and the value is &#8220;&#8221;.<br \/>\n&#8221;<\/p>\n<p>So, if you have an R script that used readline as the way of grabbing keyboard input, it won&#8217;t be able to grab the proper input from your batch file that calls this script.<\/p>\n<p>Alternatively, R is using command &#8220;scan&#8221; to do this job. see R manual page 443.<\/p>\n<p>This change can be adapted into original R sources that used readline by a function:<\/p>\n<p>readlineB &lt;- function(myString0) {<br \/>\nprint(myString0)<br \/>\na &lt;- scan(what=character(0),nmax=1)<br \/>\nreturn(a)<br \/>\n}<\/p>\n<p>which means to grab the next one line in undetermined length of characters as the readline result.<br \/>\nThen by globally replace function readline by the customer defined function readlineB, your old<br \/>\nways of sourcing R script by using following up lines as interactive input is working again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>According to R manual, http:\/\/cran.r-project.org\/doc\/manuals\/fullrefman.pdf, page 411, &#8221; Value A character vector of length one. Both leading and trailing spaces and tabs are stripped from the result. In non-interactive use the result is as if the response was RETURN and the value is &#8220;&#8221;. &#8221; So, if you have an R script that used readline [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,3],"tags":[],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-computer-tips","category-mri-technical-support","post-blog"],"_links":{"self":[{"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=224"}],"version-history":[{"count":0,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions"}],"wp:attachment":[{"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}