Include Iverson Bracket in R documentation - r

I am wanting to include an Iverson bracket in R documentation (unless there's a better way to represent this information; I'm no mathematician). This looks something like this.
Here I have valid LaTeX code for such an expression
w_{neg}=\left\{\begin{matrix}
1 & \sum{(X_i^{N})}>0 \\
0 & \sum{(X_i^{N}})=0
\end{matrix}\right.
I tried to include the code below:
#' w_{neg}=\left\{\begin{matrix}
#' 1 & \sum{(X_i^{N})}>0 \\
#' 0 & \sum{(X_i^{N}})=0
#' \end{matrix}\right.
in the details section of roxygen2 but it produces the error seen below when compiling the pdf help documentation:
Creating pdf output from LaTeX ...
Warning: running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --pdf "Rd2.tex" -I "C:/R/R-30~1.1/share/texmf/tex/latex" -I "C:/R/R-30~1.1/share/texmf/bibtex/bst"' had status 1
Error : running 'texi2dvi' on 'Rd2.tex' failed
LaTeX errors:
! Misplaced alignment tab character &.
<argument> \left \{\begin {matrix} 1 &
\sum {(X_i^{N})}>0 \\ 0 & \sum {(X_i^...
l.5566 \sum{(X_i^{N}})=0 \end{matrix}\right.}{}
! Emergency stop.
<argument> \left \{\begin {matrix} 1 &
\sum {(X_i^{N})}>0 \\ 0 & \sum {(X_i^...
l.5566 \sum{(X_i^{N}})=0 \end{matrix}\right.}{}
! ==> Fatal error occurred, no output PDF file produced!
Error in running tools::texi2pdf()
How can I include this Iverson Bracket in R documentation (unless there's a better way to represent this in which case that's the better approach)? Note that removal of these lines allows the package to compile.

I utilized the following in the roxygen2 documentation and it worked:
#' \deqn{w_{neg}=\left\{\begin{array}{cc}
#' 1 & \sum{(X_i^{N})}>0 \\
#' 0 & \sum{(X_i^{N}})=0
#' \end{array}\right.}

Related

How to set and use bash variables in R

I have a use case where I need to execute bash commands inside an R program. I can send and verify that bash commands are being executed, but for reasons I do not understand, I can't set and use variables. To begin with, a simple command works fine:
$ R
...
> system("ls")
1a.csv 1b.csv 2.csv 3.csv
[1] 0
>
Now moving on to the problem. I've tried as many approaches as I could find, but none seem to work:
> system("TEST_VAR=\"test_val\"")
[1] 127
Warning message:
In system("TEST_VAR=\"test_val\"") : 'TEST_VAR="test_val"' not found
> system("bash -c 'REPORT_S3=report_s3_test_val'")
[1] 0
> system("echo ${REPORT_S3}")
$REPORT_S3
[1] 0
> system('TEST_VAR=test_var')
[1] 127
Warning message:
In system("TEST_VAR=test_var") : 'TEST_VAR=test_var' not found
> Sys.setenv(TEST_VAR = "test_val")
> system("echo $TEST_VAR")
$TEST_VAR
[1] 0
> system("bash -c 'export TEST_VAR=\"test_val\"'")
[1] 0
> system(" echo ${TEST_VAR}")
$TEST_VAR
[1] 0
None of these attempts succeed.
What I need to do is set variables and subsequently use them to create successively more complex commands. This works fine in in bash. But I can't seem to get it to work in R, apparently for the reasons above.
REPORT_S3="s3://xxxxxxxx-reports/r/html/"$RMD_FILE"_"$EMAIL_ADDRESS"_"$CLOUDWATCH_UUID".html"
PRESIGNED_URL=$(aws s3 presign --expires-in 604800 $REPORT_S3)
JSON_STRING='xxxxx"$CLOUDWATCH_UUID"xxxxxxx"$PRESIGNED_URL".....'
echo $JSON_STRING > message.json
echo '{"ToAddresses":["'$EMAIL_ADDRESS'"],"CcAddresses":[],"BccAddresses":[]}' > destination.json
aws ses send-email --from xx#yyyyyyyy.co.nz --destination file://destination.json --message file://message.json --region ap-southeast-2
Perhaps there any other options for issuing bash commands other than system that would permit easier reuse of the original source bash code?
Environment variables are per process, and each system(..) call starts a new process. If you define and reference the variable in the same system call, it works fine:
> system('
var="foo"
echo "The variable is: $var"
')
The variable is: foo
If you put your entire script into a single system(..) call, instead of trying to run line by line, it should therefore work.
An alternative method is using Sys.setenv to set the variables in your current R process, so that future system() calls inherit it:
> Sys.setenv(var = "bar")
> system('echo "The variable is: $var"')
The variable is: bar
Obviously, since Sys.setenv is an R function, you must use R code to define your variables, and not rely on shell syntax like $(..)
PS: system() invokes sh and not bash, so all the code you pass it should be sh compatible.

Paragraph ended before \text# was complete. <to be read again> \par

I'm knitting my R markdown file to pdf and keeps getting this error
\begin {align}
\text {using the relation} \\
point estimate \pm z^{2} * SE \\
\text {where z is the z statistic corresponding to 95% of Norm distribution} \\
\end {align*}
i think it's due to the use to % in line 4 of code. File knits when i remove that line or separate from code block.
pls how can i correct it in the code block. I have multiple scenarios like that in my Rmd
Try:
\text {where z is the z statistic corresponding to 95\(\%\) of Norm distribution} \\
That works for me.

how to solve this error when run latex in R?

I'm trying to generate some plots in latex using R, here is the code:
msaPrettyPrint(seq_mapped, output="pdf", showNames="none",
showLogo="none", askForOverwrite=FALSE, verbose=FALSE)
and the error:
Error in texi2dvi(texfile, quiet = !verbose, pdf = identical(output, "pdf"), :
unable to run pdflatex on 'seq_mapped.tex'
LaTeX errors:
! Paragraph ended before \inf##get was complete.
<to be read again>
\par
l.25 ...e}{/tmp/RtmppWEuqI/seq108d544be5486.fasta}
! Misplaced alignment tab character &.
\msfline ->\par &
& & & #
l.25 ...e}{/tmp/RtmppWEuqI/seq108d544be5486.fasta}
! Misplaced alignment tab character &.
\msfline ->\par & &
& & #
l.25 ...e}{/tmp/RtmppWEuqI/seq108d544be5486.fasta}
! Misplaced alignment tab character &.
\msfline ->\par & & &
& #
l.25 ...e}{/tmp/RtmppWEuqI/seq108d544be5486.fasta}
! Misplaced alignment tab character &.
\msfline ->\par & & & &
#
l.25 ...e}{/tmp/RtmppWEuqI/seq108d544be5486.fasta}
I've also tried:
msaPrettyPrint(seq_mapped, output="tex", showNames="none",
showLogo="none", askForOverwrite=FALSE, verbose=FALSE)
tools::texi2pdf("seq_mapped.tex", clean=TRUE)
but still have an error
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'seq_mappedt.tex' failed.
LaTeX errors:
! LaTeX Error: File `texshade.sty' not found.
Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)
! Emergency stop.
<read *>
l.4 ^^M
! ==> Fatal error occurred, no output PDF file produced!
However, the texshade.sty is installed:
system.file("tex", "texshade.sty", package="msa")
[1] ".../R/x86_64-pc-linux-gnu-library/4.1/msa/tex/texshade.sty"
Any ideas?
FYI: Ubuntu 20.04.2 LTS focal
Thanks
I just came across this thread accicentally and I am aware that it 11 months old. However, just in case that it might be of help to somebody, I dare to reply...
Well, let's first start with the second question: You use msaPrettyPrint() to write a .tex file and then you call texi2dvi(). However, you do not supply texi2dvi() with the information where texshade.sty is. This must be done, since texshade.sty is stored within the msa package's folder, and not in the standard folders where LaTeX looks for input files. Long story short - you should do this:
tools::texi2pdf("seq_mapped.tex", texinputs=system.file("tex", package="msa"),clean=TRUE)
If you do that with the same input data as in the first question, I suppose that the same problem would also arise. In order to answer that in a more qualified way, I would need to try myself with the original files? Are you able to put together a minimal example exhibiting this problem and send it to me by e-mail? (msa#bioinf.jku.at)

R/exams: prevent page breaks between two paragraphs in exams2nops

I am currently working with the R/exams package, specifically creating exams using the exams2nops function and the order of the questions is randomized. Everything works fine except for one detail: The samepage = TRUE option only prevents page breaks within a paragraph. For my purpose it is however necessary not to split any of the exercises. Within one text it would still be possible to write only one paragraph (although this is not helpful for clarity). Unfortunately, whenever I need to include a table, I am forced to start a new paragraph which is not "protected" against page break. Due to the randomization there are always some copies where text and tables are splitted to multiple pages. I tried .Rmd as well as .Rnw files and also tried to integrate some LaTeX functions such as \nopagebreak and \needspace without success. So far, I am not too experienced with LaTeX and also with Google's help I did not find a solution.
Here is a minimal example of what kind of exercises I am talking about:
.Rmd
Question
========
Some kind of question:
| A | B | C |
|:-:|:-:|:-:|
| 1 | 2 | 3 |
| 1 | 2 | 3 |
| 1 | 2 | 3 |
| 1 | 2 | 3 |
| 1 | 2 | 3 |
Answerlist
----------
* First option
* Second option
* Third option
Some further informational text.
.Rnw
\begin{question}
Some kind of question:
\begin{center}
\begin{tabular}{ccccccr}
$\text{A}$ & $\text{B}$ & $\text{C}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
\end{tabular}
\end{center}
Some further informational text.
\begin{answerlist}
\item First option
\item Second option
\item Third option
\end{answerlist}
\end{question}
I am not sure what to try.
The samepage = TRUE option only enforces that the {answerlist} is in a {samepage} environment - but not the entire {question}.
The easiest option to accomplish what you want to do seems to be to re-define the {question} environment in the header = argument. You could either try to put everything into a {samepage} environment via
exams2nops(...,
header = "\\renewenvironment{question}{\\item \\begin{samepage}}{\\end{samepage}}")
This may have to be coupled, though, with some \nopagebreak commands in between paragraphs. (See: Make an unbreakable block in TeX)
A simpler solution might be to put every exercise on its own page by including a page break at the end of each exercise:
exams2nops(...,
header = "\\renewenvironment{question}{\\item}{\\newpage}")
In case you are not familiar with the LaTeX syntax above:
\renewenvironment{foo}{...}{...} re-defines the environment "foo".
The first ... is what is executed at the beginning of the environment.
The second ... is executed at the end.
By default, only \item is executed at the beginning to increase the enumerated counter for the exercises.
The double backslashes (e.g., \\item) are required in R to escape the backslashes as they are a special character.

Unexpected input error when doing subtraction in R

i try to run:
x = (1 / 1 – 2)
And here is the message:
Error: unexpected input in "x=(1 / 1 �"
I have the same issue with these sign : <- and * and minus, none of them are recognised.
You might be confronted to the so-called "zero-width space" which is not interpreted as a space. Check this wiki page:
If the blanks you have in your code example are this kind of character, it will make an error like this:
Error: unexpected input in "x=(1 / 1"
as some comments point out, one must be careful when pasting code from websites.

Resources