tag:support.markedapp.com,2011-09-11:/discussions/problems/60274-rmarkdownknitr-as-processorMarked: Discussion 2018-05-07T13:48:20Ztag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:07:04Z2014-09-17T16:18:18Zrmarkdown/knitr as processor<div><p>Hi!</p>
<p>I'm trying to use <code>R</code> in combination with
<code>rmarkdown</code> as my preprocessor so I can display markdown
documents that contain R cod, which is evaluated during the render
process. The script works fine when I call it from the command
line:</p>
<pre>
<code>cat report.md | rmarkdown-wrapper > report.html</code>
</pre>
<p>But if I run the script from within Marked 2 and redirect STDERR
to STDOUT, I get the following message:</p>
<pre>
<code>/usr/local/bin/R: /usr/local/Library/ENV/4.3/sed: /bin/bash: bad interpreter: Operation not permitted
ERROR: option '-e' requires a non-empty argument</code>
</pre>
<p>In case I don't redirect the output nothing is returned and
Marked performs the default render process.</p>
<p><code>/usr/local/bin/R</code> is a shell script that performs
the option parsing and sets up all necessary environment variables
and then initiates the <code>R</code> runtime. At least that is
what I deduced from staring at it for a while. My assumption at
this point is that there isn't anything wrong with <code>R</code>
and it might just come down to a permission problem. Is there any
way, I can force Marked to execute a script in my own shell
environment, just to see if this would fix the problem?</p>
<p>I also tried <code>Rscript</code> which is usually used for bash
execution of <code>R</code> programs. This one seems to be an
actual binary. The problem looks is essentially the same:</p>
<pre>
<code>/usr/local/Cellar/r/3.1.1/R.framework/Versions/3.1/Resources/bin/R: /usr/local/Library/ENV/4.3/sed: /bin/bash: bad interpreter: Operation not permitted
ERROR: option '-e' requires a non-empty argument</code>
</pre>
<p>Using the following dummy script, also results in an error
– a popup with the message "Operation not permitted"
appears.</p>
<pre>
<code>echo 'foo' | /usr/local/bin/gsed 's/foo/bar'</code>
</pre>
<p>Is Marked not allowed to access anything in
<code>/usr/local/bin</code>?</p>
<p>Thanks for any help in advance!</p></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:32:36Z2014-09-17T16:32:36Zrmarkdown/knitr as processor<div><p>Drag your /usr/local folder from Finder to Marked's icon to
allow it<br>
access.</p>
<p>-Brett</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:46:49Z2014-09-17T16:47:26Zrmarkdown/knitr as processor<div><p>This resulted in Marked just displaying a random file in the
folder. The Permission Problem still exists; that is the script
still produces the same error.</p>
<p>Can I reset Marked's permissions?</p></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:47:50Z2014-09-17T16:47:50Zrmarkdown/knitr as processor<div><p>When it displayed the file, though, did it request permission to
access<br>
it?</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:48:34Z2014-09-17T16:48:34Zrmarkdown/knitr as processor<div><p>Also, try also dragging /usr/Library if that directory
exists.</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:49:03Z2014-09-17T16:49:03Zrmarkdown/knitr as processor<div><p>No, it didn't. The only time I was asked was when I clicked the
"Update Permissions" button in the settings.</p></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:51:41Z2014-09-17T16:51:41Zrmarkdown/knitr as processor<div><p>If you provided permission when requested by "Update
Permissions" then<br>
this probably isn't a permissions error. I note references to ENV
and an<br>
error related to sed -e, which indicates that the script is loading
an<br>
incorrect version of utilities from within the Marked shell. Note
that<br>
your usual shell environment is unavailable when the custom
processor is<br>
run. The script needs to be able to function without any custom<br>
environment variables or environment managers (rbenv, pyenv,
etc.).</p>
<p>-Brett</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:52:55Z2014-09-17T16:52:55Zrmarkdown/knitr as processor<div><p>Can I replicate such an environment outside Marked to see if the
script functions correctly?</p></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T16:57:57Z2014-09-17T16:57:57Zrmarkdown/knitr as processor<div><p>Try running <code>sudo -s</code> and entering your password.
Your superuser<br>
environment should be clean enough to test.</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:07:41Z2014-09-17T17:07:41Zrmarkdown/knitr as processor<div><p>Script runs just fine within a sudo shell. It even runs if I
invoke it with <code>env -i</code> which resets the
environment.</p></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:10:27Z2014-09-17T17:10:27Zrmarkdown/knitr as processor<div><p>Are you receiving any new/different error messages at this
point?</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:17:47Z2014-09-17T17:18:14Zrmarkdown/knitr as processor<div><p>The following "converter" works:</p>
<pre>
<code>/usr/local/bin/gecho Hello</code>
</pre>
<p><code>gecho</code> is simply the GNU version of
<code>echo</code> and part of the <code>core-utils</code> package I
installed via <code>homebrew</code>.</p>
<p>The following "converter" does not work:</p>
<pre>
<code>echo 'Hello' | /usr/local/bin/gsed 's/Hello/World/' 2>&1</code>
</pre>
<p>It results in the following error:</p>
<pre>
<code>/Users/t6d/bin/Rmarkdown: line 3: /usr/local/bin/gsed: Operation not permitted</code>
</pre></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:22:08Z2014-09-17T17:22:08Zrmarkdown/knitr as processor<div><p>What is this?</p>
<pre>
<code>/Users/t6d/bin/Rmarkdown</code>
</pre>
<p>And why is a simple gsed call loading it? Are you sure your<br>
/usr/local/bin/gsed is a hashed binary and not a symlink?</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:27:27Z2014-09-17T17:27:27Zrmarkdown/knitr as processor<div><p>That is my wrapper script. I'm one step closer. Apparently some
of the permissions in <code>/usr/local/</code> were off. Sticky bit
was set and the owning group was <code>wheel</code>. I fixed it by
using <code>chmod -R</code>. Now I get a new error.
<code>mktemp</code> fails. Let me check if I can work it out
myself.</p>
<p>The <code>gsed</code> dummy script does work now.</p></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:37:52Z2014-09-17T17:37:52Zrmarkdown/knitr as processor<div><p>I'm back to the old issue. While we have been discussing the
problem, I'll reinstalled Marked 2 twice, each time with deleting
all preferences files, etc. The last time I did so, I didn't
immediately click the "Update Permissions" button. I fixed
<code>/usr/local</code> and opened it in Marked. I wasn't asked to
grant permission, but the <code>echo ... | gsed</code> dummy script
worked. My real script, however, now complained that it couldn't
create a tempfile using <code>mktemp</code>, so I thought I give
this "Update permissions" button a try. Helped to solve the issue
that I couldn't access <code>/tmp</code>, but R is still
complaining. It is by the way R that uses <code>sed</code> for it's
option parsing.</p>
<p>This is how my script looks by the way. I'm setting
<code>HOME</code> just is case, because <code>pandoc</code> which
is used by <code>rmarkdown</code> if <code>HOME</code> is not
set.</p>
<pre>
<code>#!/bin/bash
export HOME=/Users/t6d
export PATH=/Users/t6d/bin/:/Users/t6d/.cabal/bin:/usr/local/bin:$PATH
INPUT=`mktemp -t input`
OUTPUT=`mktemp -t output`
cat - > $INPUT
Rscript -e "rmarkdown::render('$INPUT', output_file='$OUTPUT')" 2>&1
cat $OUTPUT
unlink $OUTPUT
unlink $INPUT</code>
</pre></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:42:06Z2014-09-17T17:42:06Zrmarkdown/knitr as processor<div><p>IT WORKS!</p>
<p>Problem is reproducible now. If a file has the following group
permissions: <code>rws</code> it will result in an "Operation not
permitted" bug.</p></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:44:07Z2014-09-17T17:44:37Zrmarkdown/knitr as processor<div><p>Simply executing <code>chmod g-s</code> solves the problem. I
just missed some files. I don't know how my executables ended up
with these permissions in the first place, but I'm happy it works!
Sorry, for all the trouble and thank you very much for your help
and patience! I'm a big fan of Marked btw. and now that I can
directly view my statistics reports, I love it even more!</p></div>Konstantin Tennhardtag:support.markedapp.com,2011-09-11:Comment/346136572014-09-17T17:52:57Z2014-09-17T17:52:57Zrmarkdown/knitr as processor<div><p>That's great! I'll leave this up as a reference, hopefully I'll
remember<br>
to link it if I hear about this issue again :).</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572017-05-15T16:43:54Z2017-05-15T16:43:56Zrmarkdown/knitr as processor<div><p>Would it be possible to make the RMarkdown-wrapper available? I, too, would like to run RMarkdown as a Custom Processor in Marked. Thanks!</p></div>Mark Bowertag:support.markedapp.com,2011-09-11:Comment/346136572017-05-15T17:32:44Z2017-05-15T17:32:44Zrmarkdown/knitr as processor<div><p>Mark, I haven't tested it, but I would think the script in <a href="http://support.markedapp.com/discussions/problems/60274-rmarkdownknitr-as-processor#comment_34615357">#14 above</a> using Rscript would work… is there an additional wrapper needed?</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/346136572017-05-15T17:54:50Z2017-05-15T17:54:52Zrmarkdown/knitr as processor<div><p>The script (from #14) is not working for me. I made a txt file from the script code listed and made it executable. Should the link to that executable be in the "Processor" or "Pre-processor" tab? The output from Marked 2 do not include results from R, but has a thin red line above R code sections. Where is the error output/log to read what went wrong?</p></div>Mark Bowertag:support.markedapp.com,2011-09-11:Comment/346136572017-06-06T15:02:01Z2017-06-06T15:02:01Zrmarkdown/knitr as processor<div><p>Sorry for the late reply on this, Mark,</p>
<p>The first thing to check is that the script is working for you from the command line. Run <code>cat your_markdown_file.md | scriptfile.sh > outfile.html</code> (replacing the filenames appropriately) and ensure that the output you're getting is what you're expecting. The resulting <code>outfile.html</code> should be "partial" HTML (not a full document with head/body tags) and should view properly when you load it in a browser.</p>
<p>Because you're using the script to do the complete render resulting in HTML, you'll want to use it as the Processor. Preprocessors are subsequently run through the selected default processor (Markdown or GFM), so they expect Markdown input.</p>
<p>You can see the STDOUT/STDERR output of the script within Marked by going to Help->Show Custom Processor Log.</p></div>Brett