I'm using the R package argparse to parse command line arguments in an R script.
For readability, I'd like to add line breaks in the "description" of the script and in the help of the arguments. However, I can't do it... Let's see an example. Given this script:
#!/usr/bin/env Rscript
require(argparse)
docstring<- "Description\nDone"
parser<- ArgumentParser(description= docstring)
args<- parser$parse_args()
When executed with -h it should print:
Description
Done
However, I'm getting the error:
Error in rjson::fromJSON(output) : unexpected character 'F'
Calls: <Anonymous> -> <Anonymous> -> <Anonymous>
Execution halted
Variation of docstring like paste("Description", "Done", sep= '\n') are equally unsuccessful.
EDIT: Passing RawTextHelpFormatter: No luck.
parser<- ArgumentParser(description= docstring, RawTextHelpFormatter= TRUE)
Any idea how to put line breaks in argparse?
Many thanks!
Dario
NB: Cross posted on r-help: https://stat.ethz.ch/pipermail/r-help/2014-November/423722.html
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] argparse_1.0.1 proto_0.3-10
loaded via a namespace (and not attached):
[1] findpython_1.0.1 getopt_1.20.0 rjson_0.2.13
After a while playing around and looking at the code, I found the solution to my own answer: Use formatter_class= 'argparse.RawTextHelpFormatter' and properly escape newline characters:
#!/usr/bin/env Rscript
require(argparse)
docstring<- "DESCRIPTION \\n\\
Do stuff \\n\\n\\
Do more stuff"
parser<- ArgumentParser(description= docstring, formatter_class= 'argparse.RawTextHelpFormatter')
args<- parser$parse_args()
Now it correctly gives (startup messages omitted):
./testParse.R -h
usage: ./testParse.R [-h]
DESCRIPTION
Do stuff
Do more stuff
optional arguments:
-h, --help show this help message and exit
#hpaulj your hint was correct it took me a while to figure it out!
Related
This seems to be a recurrent problem for who is willing to write dynamic documents with knitr in RStudio (see also here for instance).
Unfortunately I haven't find a solution on Stack Overflow or by googling more in general.
Here is a toy example I am trying to compile in RStudio. It is the minimal-example-002.Rnw (link):
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
Here is a code chunk.
<<foo, fig.height=4>>=
1+1
letters
chartr('xie', 'XIE', c('xie yihui', 'Yihui Xie'))
par(mar=c(4, 4, .2, .2)); plot(rnorm(100))
#
You can also write inline expressions, e.g. $\pi=\Sexpr{pi}$, and \Sexpr{1.598673e8} is a big number.
\end{document}
My problem is that I am not able to compile the pdf in RStudio by using knitr, while by changing the default weaving option to sweave I get the final pdf.
More specifically, I work in Windows 7, latest RStudio version (0.98.1103), I weave the file using the knitr option and I disabled the "always enable Rnw concordance" box.
Did this happen to you?
Any help would be highly appreciated, thank you very much.
EDIT
Apparently it is not an RStudio problem, as I tried to compile the document from R with:
library('knitr')
knit('minimal_ex.Rnw')
and I get the same error:
processing file: minimal_ex.Rnw
|
| | 0%
|
|...................... | 33%
ordinary text without R code
|
|........................................... | 67%
label: foo (with options)
List of 1
$ fig.height: num 4
Quitting from lines 8-10 (minimal_ex.Rnw)
Errore in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 3, 0
Inoltre: Warning messages:
1: In is.na(res[, 1]) :
is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(res) : is.na() applied to non-(list or vector) of type 'NULL'
EDIT 2:
This is my session info:
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Italian_Italy.1252 LC_CTYPE=Italian_Italy.1252 LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C
[5] LC_TIME=Italian_Italy.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.10.5
loaded via a namespace (and not attached):
[1] tools_3.1.1
After spending hours to try to figure out the problem, I updated R (v 3.2.0) and everything works fine now.
It is not clear if the problem was due to some packages conflict, for sure it wasn't an RStudio problem (as I had initially thought).
To add a little to this: It seems to be a bug with the echo parameter which defaults to TRUE. Setting it to false with knitr and pdfLaTeX as a renderer worked for me. In case you're in a situation where you can't update because of dependencies and/or rights issues, this input might be a helpful adhoc fix, since the error message is pretty useless.
This question is related to: Rhtml: Warning: conversion failure on '<var>' in 'mbcsToSbcs': dot substituted for <var> and R doesn't open with UTF-8
I use Ubuntu, I can not show a turkish character, ı, on the title of a plot:
myScript.r:
pdf(file='/home/sait/Desktop/abc.pdf')
plot(1:7,1:7,main='geziparkı')
I am having the following warning messages when I run the script using Rscript myScript.r,
Warning messages:
1: In title(...) :
conversion failure on 'geziparkı' in 'mbcsToSbcs': dot substituted for <c4>
2: In title(...) :
conversion failure on 'geziparkı' in 'mbcsToSbcs': dot substituted for <b1>
3: In title(...) :
conversion failure on 'geziparkı' in 'mbcsToSbcs': dot substituted for <c4>
4: In title(...) :
conversion failure on 'geziparkı' in 'mbcsToSbcs': dot substituted for <b1>
I added the line pdf.options(encoding='ISOLatin2.enc') on the top of my script as mentioned in the related previous questions, did not help.
Do I need to change something from my locale settings of Ubuntu. My sessioinInfo() is following,
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=tr_TR.UTF-8 LC_NUMERIC=C
[3] LC_TIME=tr_TR.UTF-8 LC_COLLATE=tr_TR.UTF-8
[5] LC_MONETARY=tr_TR.UTF-8 LC_MESSAGES=C
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=tr_TR.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
PS: I continue investigating this issue, and realized that if I use .png, it works perfectly, only problem is with .pdf.
I finally found the solution,
Substituting pdf(file='/home/sait/Desktop/abc.pdf') with
cairo_pdf('/home/sait/Desktop/abc.pdf', family="DejaVu Sans") did the trick.
I do not know what this actually done, however I have tried a lot of stuff and nothing has worked except this one.
I recently updated knitr to 1.4, and since then my .Rnw files don't compile.
The document is rich (7 chapters, included with child="").
Now, in the recent knitr version I get an error message:
Quitting from lines 131-792 (/DATEN/anna/tex/CoSta/chapter1.Rnw)
Quitting from lines 817-826 (/DATEN/anna/tex/CoSta/chapter1.Rnw)
Fehler in if (eval) { :
Argument kann nicht als logischer Wert interpretiert werden
(the last two lines mean that knitr is looking for a logical and it cannot find it.
At those lines 131 and 817 two figures end. Compiling these sniplets separately will work.
I have no idea how to resolve this problem.
Thank's in advance for any hints that allow to resolve my issue.
Here is the sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=de_DE.UTF-8 LC_NUMERIC=C
[3] LC_TIME=de_DE.UTF-8 LC_COLLATE=de_DE.UTF-8
[5] LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=de_DE.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] tools stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] knitr_1.4
loaded via a namespace (and not attached):
[1] compiler_2.15.1 digest_0.6.3 evaluate_0.4.7 formatR_0.9
[5] stringr_0.6.2 tcltk_2.15.1
Following the suggestions of Hui, I run each chapter separately with
knit("chapter1.Rnw")
and so on. No error message occurs, and separate tex files are created. To provide more information I display part of the code.
There is a main document in which several options are set
<<options-setting,echo=FALSE>>=
showthis <- FALSE
evalthis <- FALSE
evalchapter <- TRUE
opts_chunk$set(comment=NA, fig.width=6, fig.height=4)
#
The each chapter is used via child chunks, e.g. chapter1 is called from
<<child-chapter1, child='chapter1.Rnw', eval=evalchapter>>=
#
The error message which appears when knitting the main Rnw file was given above.
The related Figure environment is as follows
\begin{figure}[ht]
\centering
<<wuerfel-simulation,echo=showthis,fig.height=5>>=
data.sample6 <- sample(1:6,repl=TRUE,100)
table(data.sample6)
barplot(table(data.sample6)/100,col=5,main="Haeufigkeiten beim Wuerfeln")
#
\caption{Visualisierung beim W"urfeln. 100 Versuche.}
\label{fig:muent-vis}
\end{figure}
This is not very advanced, but the error is still as it was given before.
The quitting from lines concerns a long text, from 131 (end of first chunk) to line 792 (beginning of the followup chunk), which is
<< zeiten, echo=showthis,eval=evalthis>>=
zeiten <- c(17,16,20,24,22,15,21,15,17,22)
max(zeiten)
mean(zeiten)
zeiten[4] <- 18; zeiten
mean(zeiten)
sum(zeiten > 20)
#
Is there a problem with correctly closing a chunk?
I now located the error and I provide a short piece of code with reproducible error message.It concerns conditional evaluation of child processes involving Sexpr:
The main file is the following
\documentclass{article}
\begin{document}
<<options-setting,echo=FALSE>>=
evalchapter <- TRUE
#
<<test,child="test-child.Rnw", eval=evalchapter>>=
#
\end{document}
The related child file 'test-child.Rnw' is
<<no-sexpr>>=
t <- 2:4
#
text \Sexpr{(t <- 2:4)}
knitting this 'as is' gives the error message from above. Removing the Sexpr in the child everything works nicely.
But, everything also works nicely, if I remove the conditioning in the call of the child file, i.e., without 'eval=evalchapter'
Since I use Sexpr quite often I would like to have a solution to this problem. As I mentioned earlier, there were no problems up to knitR Version 1.2.
This is related to a change in knitr 1.3 and mentioned in the NEWS:
added an argument options to knit_child() to set global chunk options for child documents; if a parent chunk calls a child document (via the child option), the chunk options of the parent chunk will be used as global options for the child document, e.g. for <<foo, child='bar.Rnw', fig.path='figure/foo-'>>=, the figure path prefix will be figure/foo- in bar.Rnw; see How to avoid figure filenames in child calls for an application
And this caused a bug for inline R code. In your case, the chunk option eval=evalchapter was not evaluated when it is used for evaluating inline code. I have fixed the bug in the development version v1.4.5 on Github.
I am getting the following error when checking my R package
> Error: could not find function "locdata"
> Execution halted
> when running code in ‘DFSurvey.Rnw’
> ...
>
> > opts_chunk$set(cache = TRUE, fig.path = "DFSurveyImages/", dev = "pdf")
>
> When sourcing ‘DFSurvey.R’:
> Error: object 'opts_chunk' not found
> Execution halted
Yihui Xie (knitr developer) said that was because in RStudio, knitr was not set as the method for weaving .Rnw files, https://groups.google.com/forum/?fromgroups#!topic/knitr/9672CBbc8CM. I have knitr set in both the tools and build options, in the R package DESCRIPTION file I have:
VignetteBuilder: knitr
Suggests: knitr
and in the vignette I have:
%\VignetteEngine{knitr}
%\VignetteDepends{knitr,xtable,TSP}
When I use compile the pdf in RStudio or use knit("KNITR.Rnw"), it compiles correctly. When I check the package, I get the above errors for each vignette. I even put
require(knitr)
before my opts_chunk$set statement. That did not help. I have also run the check from the command line and gotten the same error. Thank you for any help.
Knitr is a useful package. I run long simulations in vignettes, and the cache makes it possible to correct errors without running the simulations over each time. It does not have the problem of trying to find the Sweave.sty file either.
Here is my sessionInfo()
> R version 3.0.0 (2013-04-03)
> Platform: x86_64-apple-darwin10.8.0 (64-bit)
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] tcltk grid stats graphics grDevices utils datasets methods
> [9] base
>
> other attached packages:
> [1] DualFrame_0.5 xtable_1.7-1 TSP_1.0-7
> [4] maptools_0.8-23 lattice_0.20-15 foreign_0.8-53
> [7] spsurvey_2.5 sp_1.0-9 stringr_0.6.2
> [10] sqldf_0.4-6.4 RSQLite.extfuns_0.0.1 chron_2.3-43
> [13] gsubfn_0.6-5 proto_0.3-10 RSQLite_0.11.3
> [16] DBI_0.2-7 knitr_1.2 gpclib_1.5-5
>
> loaded via a namespace (and not attached):
> [1] deldir_0.0-22 digest_0.6.3 evaluate_0.4.3 formatR_0.7 MASS_7.3-26
> [6] rgeos_0.2-17 tools_3.0.0
put library(knitr) before this opts_chunk$set(cache = TRUE, fig.path = "DFSurveyImages/", dev = "pdf")
You have to load the knitr library first, try this:
```{r setoptions, echo=FALSE}
library(knitr)
opts_chunk$set(cache = TRUE, fig.path = "DFSurveyImages/", dev = "pdf")```
For a knitr vignette that you can compile using knit() or with the "Compile PDF" button in RStudio, but that gets an
Error: object 'opts_chunk' not found
Execution halted
error when checking or building the package, the package check code is not recognizing that your .Rnw file should be knited and not Sweaveed. Check that you have the following:
The vignettes are in the vignette directory, if you have R 3.0.0 or
higher (this was the solution to this post),
cran.r-project.org/doc/manuals/r-devel/R-exts.html#Non_002dSweave-vignettes
Include %\VignetteEngine{knitr::knitr} in the vignette metadata,
yihui.name/knitr/demo/vignette/
Specify VignetteBuilder: knitr in the package DESCRIPTION file, and
Add Suggests: knitr in DESCRIPTION if knitr is needed only for
vignettes
If that does not work add a require(knitr) statement before you set your global options in opts_chunk(), as Ben Bolker, Yuhui and Tyler Rinker suggested.
If in RStudio:
In BOTH the Build configuration and Tool options, set the Sweave option to knitr, www.rstudio.com/ide/docs/authoring/rnw_weave
Changing the Sweave option to knitr in the Tools options worked for me.
I run into this same problem today. Before it was always good. The error message was:
Quitting from lines 14-49 (report.Rmd)
Error in eval(expr, envir, enclos) : object 'opts_chunk' not found
I first specified library(knitr) right before the global options. Did not help.
I specified the name space before the opts_chunk and it worked, like knitr::opts_chunk.
I have tried the following code, and then R exits unexpectedly:
temp <- rep(as.Date("2009-01-01")+1:365, 365)
print(temp)
Anyone tried this before, is it a bug, or if there is anything I can do?
I have increase the memory available for R from 1024M to 2047M, but the same thing happens.
Thanks.
UPDATE #1
Here's my sessionInfo()
> sessionInfo()
R version 2.11.0 (2010-04-22)
i386-pc-mingw32
locale:
[1] LC_COLLATE=Chinese_Hong Kong S.A.R..950 LC_CTYPE=Chinese_Hong Kong S.A.R..950
[3] LC_MONETARY=Chinese_Hong Kong S.A.R..950 LC_NUMERIC=C
[5] LC_TIME=Chinese_Hong Kong S.A.R..950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
And actually I am trying to do some format(foo, "%y%M"), but it also exit unexpectedly - for "unexpectedly", I mean R closes itself without any signs. Thanks again.
Looks like a known bug, fixed in 2.11.1 to me. Look at 2.11.1 changelog, section BUG FIXES, 8th item.
Works fine for me (R 2.12.0, running on Fedora Core 13)
You may check the output of getOption("max.print") and lower it a little bit (using, for instance, option(max.print=5000).
In general, however, there is no need to print out such a vector, as you will not be able to read the complete output. Functions like str(t) or head(t) are your friends!