compilation error in Knitr in Lyx - r

I have been using knitr with Lyx (2.1.4) in Windows for past few months. Recently I shifted to Ubuntu with same version for Lyx. I am getting an error during compilation while using the Knitr Module.
1) The copy of the error message (from message pane) :
01:00:48.616: Previewing ...
01:00:48.667: Rscript --verbose --no-save --no-restore "/usr/share/lyx/scripts/lyxknitr.R" "/tmp/lyx_tmpdir.MvERGeak1905/lyx_tmpbuf0/""newfile1.Rnw" "/tmp/lyx_tmpdir.MvERGeak1905/lyx_tmpbuf0/""newfile1.tex" ISO-8859-15 "/home/user/"
01:00:48.674: running
01:00:48.681: '/usr/lib/R/bin/R --slave --no-restore --no-save --no-restore --file=/usr/share/lyx/scripts/lyxknitr.R --args /tmp/lyx_tmpdir.MvERGeak1905/lyx_tmpbuf0/newfile1.Rnw /tmp/lyx_tmpdir.MvERGeak1905/lyx_
01:00:48.691: tmpbuf0/newfile1.tex ISO-8859-15'
01:00:48.695:
01:00:50.579:
01:00:50.580:
01:00:50.582: processing file: /tmp/lyx_tmpdir.MvERGeak1905/lyx_tmpbuf0/newfile1.Rnw
01:00:50.631:
|
| | 0%
|
01:00:50.636:
|...................... | 33%
01:00:50.641: ordinary text without R code
01:00:50.644:
01:00:50.648:
|
|........................................... | 67%
01:00:50.652: label: unnamed-chunk-1
01:00:51.025: Quitting from lines 11-12 (/tmp/lyx_tmpdir.MvERGeak1905/lyx_tmpbuf0/newfile1.Rnw)
01:00:51.031: Error in data.frame(..., check.names = FALSE) :
01:00:51.036: arguments imply differing number of rows: 3, 0
01:00:51.040: Calls: knit ... <Anonymous> -> <Anonymous> -> cbind -> cbind -> data.frame
01:00:51.043: In addition: Warning messages:
01:00:51.047: 1: In is.na(res[, 1]) :
01:00:51.052: is.na() applied to non-(list or vector) of type 'NULL'
01:00:51.056: 2: In is.na(res) : is.na() applied to non-(list or vector) of type 'NULL'
01:00:51.060:
01:00:51.065: Execution halted
01:00:53.236: Error while exporting format: PDF (pdflatex)
01:00:58.250: (dialog-toggle progress)
2) The converter definition:
Rscript --verbose --no-save --no-restore $$s/scripts/lyxknitr.R $$p$$i $$p$$o $$e $$r
3) My code chunk is
<<>>=
2+3
#
Please note that the .Rnw file is created in tmp folder (lyx_tmpbuf0 ) and can be complied successfully by R studio. Also I can successfully compile pdf using sweave module in lyx.
Update 1: According to scottkosty's suggestion I checked the system environment in both R and RStudio and noticed few differences (though I have no idea about them) .
This is the output for R :
"COLORTERM" "gnome-terminal"
"COLUMNS" "80"
"DBUS_SESSION_BUS_ADDRESS" "unix:abstract=/tmp/dbus-3muQCK69fQ,guid=e7edee405375ab42e386699a0000002f"
"DEFAULTS_PATH" "/usr/share/gconf/ubuntu-2d.default.path"
"DESKTOP_SESSION" "ubuntu-2d"
"DISPLAY" ":0"
"EDITOR" "vi"
"GDMSESSION" "ubuntu-2d"
"GNOME_DESKTOP_SESSION_ID" "this-is-deprecated"
"GNOME_KEYRING_CONTROL" "/tmp/keyring-m9K6wh"
"GNOME_KEYRING_PID" "1515"
"GPG_AGENT_INFO" "/tmp/keyring-m9K6wh/gpg:0:1"
"GTK_MODULES" "canberra-gtk-module:canberra-gtk-module"
"HOME" "/home/user"
"LANG" "en_IN"
"LANGUAGE" "en_US:en"
"LC_COLLATE" "en_US.UTF-8"
"LC_CTYPE" "en_US.UTF-8"
"LC_MESSAGES" "en_US.UTF-8"
"LD_LIBRARY_PATH" "/usr/lib/R/lib:/usr/lib/jvm/java-6-openjdk-i386/jre/lib/i386/server"
"LINES" "24"
"LN_S" "ln -s"
"LOGNAME" "user"
"MAKE" "make"
"MANDATORY_PATH" "/usr/share/gconf/ubuntu-2d.mandatory.path"
"PAGER" "/usr/bin/pager"
"PATH" "/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
"PWD" "/home/user"
"R_ARCH" ""
"R_BROWSER" "xdg-open"
"R_BZIPCMD" "/bin/bzip2"
"R_DOC_DIR" "/usr/share/R/doc"
"R_GZIPCMD" "/bin/gzip"
"R_HOME" "/usr/lib/R"
"R_INCLUDE_DIR" "/usr/share/R/include"
"R_LIBS_SITE" "/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library"
"R_LIBS_USER" "~/R/i686-pc-linux-gnu-library/3.1"
"R_PAPERSIZE" "letter"
"R_PAPERSIZE_USER" "a4"
"R_PDFVIEWER" "/usr/bin/xdg-open"
"R_PLATFORM" "i686-pc-linux-gnu"
"R_PRINTCMD" "/usr/bin/lpr"
"R_RD4PDF" "times,inconsolata,hyper"
"R_SESSION_TMPDIR" "/tmp/Rtmpp31WUY"
"R_SHARE_DIR" "/usr/share/R/share"
"R_SYSTEM_ABI" "linux,gcc,gxx,gfortran,?"
"R_TEXI2DVICMD" "/usr/bin/texi2dvi"
"R_UNZIPCMD" "/usr/bin/unzip"
"R_ZIPCMD" "/usr/bin/zip"
"SED" "/bin/sed"
"SESSION_MANAGER" "local/akd-HP-Mini-110-4100:#/tmp/.ICE-unix/1526,unix/akd-HP-Mini-110-4100:/tmp/.ICE-unix/1526"
"SHELL" "/bin/bash"
"SHLVL" "0"
"SSH_AGENT_PID" "1563"
"SSH_AUTH_SOCK" "/tmp/keyring-m9K6wh/ssh"
"TAR" "/bin/tar"
"TERM" "xterm"
"UBUNTU_MENUPROXY" "libappmenu.so"
"USER" "user"
"WINDOWID" "67108870"
"XAUTHORITY" "/home/user/.Xauthority"
"XDG_CONFIG_DIRS" "/etc/xdg/xdg-ubuntu-2d:/etc/xdg"
"XDG_CURRENT_DESKTOP" "Unity"
"XDG_DATA_DIRS" "/usr/share/ubuntu-2d:/usr/share/gnome:/usr/local/share/:/usr/share/"
"XDG_SEAT_PATH" "/org/freedesktop/DisplayManager/Seat0"
"XDG_SESSION_COOKIE" "c143c6cb80cfe9614354b2ab0000000d-1453572539.964572-1541574946"
"XDG_SESSION_PATH" "/org/freedesktop/DisplayManager/Session0"
This is the output for RStudio:
"DBUS_SESSION_BUS_ADDRESS" "unix:abstract=/tmp/dbus-3muQCK69fQ,guid=e7edee405375ab42e386699a0000002f"
"DEFAULTS_PATH" "/usr/share/gconf/ubuntu-2d.default.path"
"DESKTOP_SESSION" "ubuntu-2d"
"DISPLAY" ":0"
"EDITOR" "vi"
"GDMSESSION" "ubuntu-2d"
"GIO_LAUNCHED_DESKTOP_FILE" "/usr/share/applications/rstudio.desktop"
"GIO_LAUNCHED_DESKTOP_FILE_PID" "2397"
"GIT_ASKPASS" "rpostback-askpass"
"GNOME_DESKTOP_SESSION_ID" "this-is-deprecated"
"GNOME_KEYRING_CONTROL" "/tmp/keyring-m9K6wh"
"GNOME_KEYRING_PID" "1515"
"GPG_AGENT_INFO" "/tmp/keyring-m9K6wh/gpg:0:1"
"GTK_MODULES" "canberra-gtk-module:canberra-gtk-module"
"HOME" "/home/user"
"LANG" "en_IN"
"LANGUAGE" "en_US:en"
"LC_COLLATE" "en_US.UTF-8"
"LC_CTYPE" "en_US.UTF-8"
"LC_MESSAGES" "en_US.UTF-8"
"LD_LIBRARY_PATH" "/usr/lib/R/lib:/lib:/usr/lib/jvm/java-6-openjdk-i386/jre/lib/i386/server"
"LN_S" "ln -s"
"LOGNAME" "user"
"MAKE" "make"
"MANDATORY_PATH" "/usr/share/gconf/ubuntu-2d.mandatory.path"
"PAGER" "/usr/bin/pager"
"PATH" "/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
"PWD" "/home/user"
"R_BROWSER" "xdg-open"
"R_BZIPCMD" "/bin/bzip2"
"R_DOC_DIR" "/usr/share/R/doc"
"R_GZIPCMD" "/bin/gzip"
"R_HOME" "/usr/lib/R"
"R_INCLUDE_DIR" "/usr/share/R/include"
"R_LIBS" "/usr/lib/rstudio/R/library"
"R_LIBS_SITE" "/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library"
"R_LIBS_USER" "~/R/i686-pc-linux-gnu-library/3.1"
"R_PAPERSIZE" "letter"
"R_PAPERSIZE_USER" "a4"
"R_PDFVIEWER" "/usr/bin/xdg-open"
"R_PLATFORM" "i686-pc-linux-gnu"
"R_PRINTCMD" "/usr/bin/lpr"
"R_RD4PDF" "times,inconsolata,hyper"
"R_SESSION_TMPDIR" "/tmp/Rtmp7wgloJ"
"R_SHARE_DIR" "/usr/share/R/share"
"RS_PPM_FD_READ" "18"
"RS_PPM_FD_WRITE" "19"
"RS_RPOSTBACK_PATH" "/usr/lib/rstudio/bin/rpostback"
"RS_SHARED_SECRET" "115600967413966338811821464018"
"RSTUDIO" "1"
"RSTUDIO_SESSION_PORT" "20366"
"RSTUDIO_USER_IDENTITY" "user"
"R_SYSTEM_ABI" "linux,gcc,gxx,gfortran,?"
"R_TEXI2DVICMD" "/usr/bin/texi2dvi"
"R_UNZIPCMD" "/usr/bin/unzip"
"R_ZIPCMD" "/usr/bin/zip"
"SED" "/bin/sed"
"SESSION_MANAGER" "local/akd-HP-Mini-110-4100:#/tmp/.ICE-unix/1526,unix/akd-HP-Mini-110-4100:/tmp/.ICE-unix/1526"
"SHELL" "/bin/bash"
"SSH_AGENT_PID" "1563"
"SSH_AUTH_SOCK" "/tmp/keyring-m9K6wh/ssh"
"TAR" "/bin/tar"
"UBUNTU_MENUPROXY" "libappmenu.so"
"USER" "user"
"XAUTHORITY" "/home/user/.Xauthority"
"XDG_CONFIG_DIRS" "/etc/xdg/xdg-ubuntu-2d:/etc/xdg"
"XDG_CURRENT_DESKTOP" "Unity"
"XDG_DATA_DIRS" "/usr/share/ubuntu-2d:/usr/share/gnome:/usr/local/share/:/usr/share/"
"XDG_SEAT_PATH" "/org/freedesktop/DisplayManager/Seat0"
"XDG_SESSION_COOKIE" "c143c6cb80cfe9614354b2ab0000000d-1453572539.964572-1541574946"
"XDG_SESSION_PATH" "/org/freedesktop/DisplayManager/Session0"

Related

the problem of getSrcFilename() in R but not in littler

I'd expect getSrcFilename() also works in R not just lr. Is it a bug in the command R?
$ lr -e "source('main.R');"
[1] "main.R"
$ R --slave --no-save --no-restore --no-init-file -e "source('main.R')"
character(0)
$ cat main.R
options(keep.source=T)
f=function() {
getSrcFilename(f)
}
print(getSrcFilename(f))

Unable to Compile Bookdown Source for "Efficient R Programming" into PDF

I am trying to compile the PDF version of Efficient R Programming using RStudio on my Windows 7 machine. Using the instructions here I was able to compile the HTML version, but the code for the PDF version fails. After running the code below (which seems to work at first), I got the warning messages below:
## For pdf
bookdown::render_book("index.Rmd", output_format = bookdown::pdf_book())
output file: _main.knit.md
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS _main.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output _main.tex --table-of-contents --toc-depth 2 --template "C:\R\R-3.4.4\library\rmarkdown\rmd\latex\default-1.17.0.2.tex" --number-sections --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" --variable "compact-title:yes" --variable tables=yes --standalone --filter pandoc-citeproc
Error: Failed to compile _main.tex.
In addition: Warning messages:
1: In file.remove("voc_voyages.xlsx") :
cannot remove file 'voc_voyages.xlsx', reason 'No such file or directory'
2: In connection_release(conn#ptr) :
There are 1 result in use. The connection will be released when they are closed
3: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
no date field in DESCRIPTION file of package 'benchmarkme'
4: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
could not determine year for 'benchmarkme' from package DESCRIPTION file
5: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
no date field in DESCRIPTION file of package 'bookdown'
6: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
could not determine year for 'bookdown' from package DESCRIPTION file
7: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
no date field in DESCRIPTION file of package 'efficient'
8: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
could not determine year for 'efficient' from package DESCRIPTION file
9: running command '"pdflatex" -halt-on-error -interaction=batchmode "_main.tex"' had status 127
Please delete _main.Rmd after you finish debugging the error.
I found this discussion of MikTeX vs tinytex. I then installed tinytex and now get this (slightly different) error:
## For pdf
bookdown::render_book("index.Rmd", output_format = bookdown::pdf_book())
label: unnamed-chunk-235
ordinary text without R code
label: unnamed-chunk-236
ordinary text without R code
label: unnamed-chunk-237 (with options)
List of 1
$ message: logi FALSE
Attaching package: 'Matrix'
The following object is masked from 'package:tidyr':
expand
ordinary text without R code
label: unnamed-chunk-238
|............................................... | 73%
ordinary text without R code
label: unnamed-chunk-239 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
ordinary text without R code
label: unnamed-chunk-240 (with options)
List of 2
$ results: chr "hide"
$ indent : chr " "
ordinary text without R code
|................................................ | 73%
label: 7-4 (with options)
List of 3
$ echo : logi FALSE
$ out.width: chr "100%"
$ fig.cap : chr "Code profiling of the optimised code."
ordinary text without R code
label: unnamed-chunk-241 (with options)
List of 2
$ eval: logi FALSE
$ echo: logi FALSE
ordinary text without R code
|................................................ | 74%
label: unnamed-chunk-242 (with options)
List of 1
$ echo: language 1:2
ordinary text without R code
label: unnamed-chunk-243 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
ordinary text without R code
label: unnamed-chunk-244 (with options)
List of 2
$ eval: logi FALSE
$ tidy: logi FALSE
ordinary text without R code
label: unnamed-chunk-245 (with options)
List of 2
$ eval: logi FALSE
$ echo: language c(1, 3)
ordinary text without R code
|................................................ | 75%
label: unnamed-chunk-246 (with options)
List of 2
$ eval: logi FALSE
$ echo: language 2:3
|................................................. | 75%
ordinary text without R code
label: unnamed-chunk-247 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-248 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-249
ordinary text without R code
|................................................. | 76%
label: unnamed-chunk-250 (with options)
List of 2
$ type : chr "rmdtip"
$ engine: chr "block"
ordinary text without R code
label: unnamed-chunk-251 (with options)
List of 1
$ echo: logi FALSE
ordinary text without R code
label: unnamed-chunk-252 (with options)
List of 1
$ results: chr "hide"
|.................................................. | 76%
ordinary text without R code
label: 7-5 (with options)
List of 7
$ echo : logi FALSE
$ fig.cap : chr "Downloads per day from the RStudio CRAN mirror of packages that provide R interfaces to other languages."
$ warning : logi FALSE
$ fig.width : num 6
$ fig.height: num 4
$ out.width : chr "70%"
$ fig.align : chr "center"
inline R code fragments
|.................................................. | 77%
label: test_cpp (with options)
List of 2
$ eval : logi FALSE
$ results: chr "hide"
ordinary text without R code
label: unnamed-chunk-253
ordinary text without R code
label: unnamed-chunk-254 (with options)
List of 3
$ eval : logi FALSE
$ tidy : logi FALSE
$ engine: chr "Rcpp"
ordinary text without R code
label: unnamed-chunk-255 (with options)
List of 1
$ tidy: logi FALSE
ordinary text without R code
|.................................................. | 78%
label: unnamed-chunk-256
ordinary text without R code
|................................................... | 78%
label: unnamed-chunk-257
ordinary text without R code
label: unnamed-chunk-258 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
ordinary text without R code
label: cpptypes (with options)
List of 1
$ echo: logi FALSE
ordinary text without R code
|................................................... | 79%
label: unnamed-chunk-259 (with options)
List of 2
$ eval : logi FALSE
$ engine: chr "Rcpp"
ordinary text without R code
label: unnamed-chunk-260 (with options)
List of 2
$ eval : logi FALSE
$ engine: chr "Rcpp"
ordinary text without R code
label: unnamed-chunk-261 (with options)
List of 2
$ eval : logi FALSE
$ engine: chr "Rcpp"
ordinary text without R code
|.................................................... | 79%
label: unnamed-chunk-262 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
ordinary text without R code
|.................................................... | 80%
label: unnamed-chunk-263 (with options)
List of 1
$ engine: chr "Rcpp"
Building shared library for Rcpp code chunk...
ordinary text without R code
label: unnamed-chunk-264
ordinary text without R code
label: unnamed-chunk-265 (with options)
List of 2
$ eval : logi FALSE
$ engine: chr "Rcpp"
ordinary text without R code
label: unnamed-chunk-266
ordinary text without R code
label: unnamed-chunk-267 (with options)
List of 3
$ eval : logi FALSE
$ engine: chr "Rcpp"
$ indent: chr " "
|.................................................... | 81%
ordinary text without R code
label: unnamed-chunk-268 (with options)
List of 3
$ eval : logi FALSE
$ engine: chr "Rcpp"
$ indent: chr " "
|..................................................... | 81%
ordinary text without R code
label: unnamed-chunk-269
ordinary text without R code
label: unnamed-chunk-270 (with options)
List of 3
$ cache: logi TRUE
$ echo : language -1
$ eval : logi FALSE
ordinary text without R code
label: 7-6 (with options)
List of 7
$ fig.width : num 6
$ fig.height: num 4
$ echo : num 2
$ fig.cap : chr "Comparison of mean functions."
$ cache : logi TRUE
$ fig.pos : chr "t"
$ out.width : chr "70%"
|..................................................... | 82%
ordinary text without R code
label: unnamed-chunk-271 (with options)
List of 3
$ eval : logi FALSE
$ tidy : logi FALSE
$ engine: chr "Rcpp"
ordinary text without R code
label: unnamed-chunk-272 (with options)
List of 3
$ eval : logi FALSE
$ engine: chr "Rcpp"
$ indent: chr " "
ordinary text without R code
label: unnamed-chunk-273
|...................................................... | 82%
ordinary text without R code
label: unnamed-chunk-274 (with options)
List of 2
$ eval : logi FALSE
$ engine: chr "Rcpp"
|...................................................... | 83%
ordinary text without R code
label: unnamed-chunk-275 (with options)
List of 2
$ eval : logi FALSE
$ engine: chr "Rcpp"
ordinary text without R code
label: unnamed-chunk-276 (with options)
List of 2
$ include: logi FALSE
$ cache : logi FALSE
ordinary text without R code
label: unnamed-chunk-277 (with options)
List of 1
$ echo: logi FALSE
ordinary text without R code
label: unnamed-chunk-278
|...................................................... | 84%
ordinary text without R code
label: ascii (with options)
List of 1
$ echo: logi FALSE
ordinary text without R code
|....................................................... | 84%
label: unnamed-chunk-279 (with options)
List of 1
$ echo: num 2
ordinary text without R code
label: unnamed-chunk-280
ordinary text without R code
label: unnamed-chunk-281
|....................................................... | 85%
ordinary text without R code
label: unnamed-chunk-282 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
ordinary text without R code
label: unnamed-chunk-283 (with options)
List of 2
$ type : chr "rmdtip"
$ engine: chr "block"
ordinary text without R code
label: unnamed-chunk-284 (with options)
List of 1
$ results: chr "hide"
ordinary text without R code
|........................................................ | 85%
label: 8-1 (with options)
List of 3
$ fig.cap : chr "Three DIMM slots on a computer motherboard used for increasing the amount of available RAM. Credit: Wikimedia.org"
$ echo : logi FALSE
$ out.width: chr "100%"
|........................................................ | 86%
ordinary text without R code
label: unnamed-chunk-285
ordinary text without R code
label: unnamed-chunk-286
ordinary text without R code
label: unnamed-chunk-287 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
ordinary text without R code
label: 8-2 (with options)
List of 2
$ fig.cap: chr "A standard 2.5\" hard drive, found in most laptops. Credit: https://en.wikipedia.org/wiki/Hard\\_disk\\_drive"
$ echo : logi FALSE
|........................................................ | 87%
ordinary text without R code
label: unnamed-chunk-288 (with options)
List of 2
$ type : chr "rmdtip"
$ engine: chr "block"
ordinary text without R code
label: unnamed-chunk-289 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
|......................................................... | 87%
ordinary text without R code
label: unnamed-chunk-290 (with options)
List of 1
$ results: chr "hide"
ordinary text without R code
label: 8-3 (with options)
List of 5
$ echo : logi FALSE
$ fig.width : num 6
$ fig.height: num 4
$ fig.cap : chr "CPU clock speed. The data for this figure was collected from web-forum and wikipedia. It is intended to indicat"| __truncated__
$ out.width : chr "70%"
ordinary text without R code
|......................................................... | 88%
label: 8-4 (with options)
List of 6
$ echo : logi FALSE
$ fig.width : num 6
$ fig.height: num 4
$ fig.cap : chr "CPU benchmarks from the R package, **benchmarkme**. Each point represents an individual CPU result."
$ fig.keep : chr "last"
$ out.width : chr "70%"
ordinary text without R code
label: unnamed-chunk-291 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-292 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-293 (with options)
List of 2
$ include: logi FALSE
$ cache : logi FALSE
|.......................................................... | 88%
ordinary text without R code
|.......................................................... | 89%
label: unnamed-chunk-294 (with options)
List of 1
$ echo: logi FALSE
ordinary text without R code
label: unnamed-chunk-295 (with options)
List of 2
$ echo : logi FALSE
$ message: logi FALSE
ordinary text without R code
label: unnamed-chunk-296 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-297
ordinary text without R code
|.......................................................... | 90%
label: unnamed-chunk-298
ordinary text without R code
label: unnamed-chunk-299 (with options)
List of 2
$ engine: chr "bash"
$ eval : logi FALSE
ordinary text without R code
|........................................................... | 90%
label: unnamed-chunk-300 (with options)
List of 1
$ messages: logi FALSE
ordinary text without R code
label: unnamed-chunk-301
ordinary text without R code
|........................................................... | 91%
label: unnamed-chunk-302
ordinary text without R code
label: unnamed-chunk-303 (with options)
List of 2
$ type : chr "rmdwarning"
$ engine: chr "block"
ordinary text without R code
label: unnamed-chunk-304 (with options)
List of 2
$ results: chr "hide"
$ message: logi FALSE
Attaching package: 'lubridate'
The following objects are masked from 'package:data.table':
hour, isoweek, mday, minute, month, quarter, second, wday,
week, yday, year
The following object is masked from 'package:base':
date
ordinary text without R code
label: unnamed-chunk-305 (with options)
List of 2
$ eval : logi FALSE
$ results: chr "hide"
ordinary text without R code
|........................................................... | 92%
label: unnamed-chunk-306 (with options)
List of 2
$ echo : language 2:5
$ results: chr "hide"
|............................................................ | 92%
ordinary text without R code
label: unnamed-chunk-307 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-308 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-309 (with options)
List of 1
$ results: chr "hide"
ordinary text without R code
|............................................................ | 93%
label: 9-1 (with options)
List of 3
$ fig.cap : chr "The Git tab in RStudio"
$ echo : logi FALSE
$ out.width: chr "70%"
ordinary text without R code
label: 9-2 (with options)
List of 3
$ fig.cap : chr "The Git history navigation interface"
$ echo : logi FALSE
$ out.width: chr "100%"
ordinary text without R code
label: unnamed-chunk-310 (with options)
List of 1
$ eval: logi FALSE
|............................................................. | 93%
ordinary text without R code
label: note-text (with options)
List of 2
$ type : chr "rmdwarning"
$ engine: chr "block"
ordinary text without R code
label: unnamed-chunk-311 (with options)
List of 2
$ engine: chr "bash"
$ eval : logi FALSE
|............................................................. | 94%
ordinary text without R code
label: unnamed-chunk-312 (with options)
List of 1
$ echo: logi FALSE
ordinary text without R code
label: unnamed-chunk-313 (with options)
List of 2
$ include: logi FALSE
$ cache : logi FALSE
ordinary text without R code
label: unnamed-chunk-314 (with options)
List of 1
$ echo: logi FALSE
ordinary text without R code
label: unnamed-chunk-315
|............................................................. | 95%
ordinary text without R code
|.............................................................. | 95%
label: unnamed-chunk-316 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-317 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-318 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-319
|.............................................................. | 96%
ordinary text without R code
label: unnamed-chunk-320 (with options)
List of 3
$ echo : logi FALSE
$ fig.cap : chr "Package name autocompletion in action in RStudio for packages beginning with 'geo'."
$ out.width: chr "100%"
ordinary text without R code
label: unnamed-chunk-321 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
ordinary text without R code
|............................................................... | 96%
label: unnamed-chunk-322 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-323 (with options)
List of 1
$ eval: logi FALSE
|............................................................... | 97%
ordinary text without R code
label: unnamed-chunk-324 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-325
ordinary text without R code
label: unnamed-chunk-326 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-327 (with options)
List of 2
$ type : chr "rmdnote"
$ engine: chr "block"
|............................................................... | 98%
ordinary text without R code
label: unnamed-chunk-328 (with options)
List of 2
$ type : chr "rmdtip"
$ engine: chr "block"
|................................................................ | 98%
ordinary text without R code
label: unnamed-chunk-329 (with options)
List of 1
$ eval: logi FALSE
ordinary text without R code
label: unnamed-chunk-330 (with options)
List of 1
$ echo: language -3
ordinary text without R code
label: unnamed-chunk-331 (with options)
List of 1
$ results: chr "hide"
|................................................................ | 99%
ordinary text without R code
label: unnamed-chunk-332 (with options)
List of 2
$ include: logi FALSE
$ cache : logi FALSE
ordinary text without R code
label: unnamed-chunk-333 (with options)
List of 2
$ eval : logi FALSE
$ indent: chr " "
ordinary text without R code
label: unnamed-chunk-334 (with options)
List of 1
$ eval: logi FALSE
|.................................................................| 99%
ordinary text without R code
label: is_travis (with options)
List of 1
$ echo: logi FALSE
|.................................................................| 100%
ordinary text without R code
label: unnamed-chunk-335 (with options)
List of 4
$ echo : logi FALSE
$ warning: logi FALSE
$ message: logi FALSE
$ eval : language, mode "(": (!is_travis)
tweaking fortunes
ordinary text without R code
label: unnamed-chunk-336 (with options)
List of 3
$ echo : logi FALSE
$ message: logi FALSE
$ warning: logi FALSE
ordinary text without R code
output file: _main.knit.md
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS _main.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output _main.tex --table-of-contents --toc-depth 2 --template "C:\R\R-3.4.4\library\rmarkdown\rmd\latex\default-1.17.0.2.tex" --number-sections --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" --variable "compact-title:yes" --variable tables=yes --standalone --filter pandoc-citeproc
Output created: _book/_main.pdf
Error in tools::file_path_as_absolute(output_file) :
file '_book/_main.pdf' does not exist
In addition: Warning messages:
1: In file.remove("voc_voyages.xlsx") :
cannot remove file 'voc_voyages.xlsx', reason 'No such file or directory'
2: In connection_release(conn#ptr) :
There are 1 result in use. The connection will be released when they are closed
3: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
no date field in DESCRIPTION file of package 'benchmarkme'
4: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
could not determine year for 'benchmarkme' from package DESCRIPTION file
5: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
no date field in DESCRIPTION file of package 'bookdown'
6: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
could not determine year for 'bookdown' from package DESCRIPTION file
7: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
no date field in DESCRIPTION file of package 'efficient'
8: In citation(pkg, auto = if (pkg == "base") NULL else TRUE) :
could not determine year for 'efficient' from package DESCRIPTION file
9: running command '"pdflatex" -halt-on-error -interaction=batchmode "_main.tex"' had status 1
10: In readLines(logfile) : incomplete final line found on '_main.log'
Please delete _main.Rmd after you finish debugging the error.
Based on the comment from #jaySf below, I ran this code, but still got the same error:
remove.packages("rmarkdown")
install.packages("devtools")
library(devtools)
install_version("rmarkdown", version = "1.8")
## For pdf
bookdown::render_book("index.Rmd", output_format = bookdown::pdf_book())
Any suggestions would be appreciated. Thanks.

Passing Parameters to an R Script via CMD or Batch

Currently I have an R Script that takes 8 parameters that are hard-coded as the first 8 lines of my script.
I've made a Batch file to try and manually change the parameters on the fly, but it doesn't seem to be working the way I want it to.
Batch that currently runs the script (But doesnt actually change the parameters)
echo off
ECHO PRESS ENTER AT ANY INPUT TO ACCEPT the DEFAULT VALUE.
:: Setting of Variables
#Set /P RScript=Set path to R:_
#Set /P RProgram=Set path to RScript:_
#Set /P RStartDir=Set Start Directory:_
#Set /P Begin=Begin on which Loan?:_
#Set /P End=End on which Loan?:_
#Set /P OutputDir=Set Output Directory:_
#Set /P Deal=Set Deal input file (.txt):_
#Set /P OutputFile=Name Deal Output File:_
#Set /P AsOfDate=As of Date?:_
#Set /P ThirtyYrSpread=Thirty Year Mortgage Spread?:_
:: If Blank (enter), set variables/paths to Defaults (Listed Below)
if "%RScript%"=="" Set RScript=c:\program files\r\r-
3.4.3\bin\x64\rscript.exe
if "%RProgram%"=="" Set RProgram=C:\MortgageMatt\Cirt2014-
1\0.Mortgage Model.R
if "%RStartDir%"=="" Set RStartDir=C:\MortgageMatt\Cirt2014-1
if "%Begin%"=="" Set Begin=1
if "%End%"=="" Set End=2
if "%OutputDir%"=="" Set OutputDir=C:\MortgageMatt\Cirt2014-1
if "%Deal%"=="" Set Deal=Cirt 2014-1 Loan Level.txt
if "%OutputFile%"=="" Set OutputFile=Cirt 2014-1d
if "%AsOfDate%"=="" Set AsOfDate=62017
if "%ThirtyYrSpread%" == "" Set ThirtyYrSpread=135
echo "%RScript% %RProgram% %RStartDir% %Begin% %End% %OutputDir% %Deal%
%OutputFile% %AsOfDate% %ThirtyYrSpread%"
ECHO PLEASE CHECK IF THESE VALUES ARE CORRECT
pause
:: Command Prompt, /c Carries out command specified by string and then
terminates
cmd /c ""%RScript%" "%RProgram%" "%RStartDir%" "%Begin%" "%End%"
"%OutputDir%" "%Deal%" "%OutputFile%" "%AsOfDate%" "%ThirtyYrSpread%""
So because the parameters were actually hard coded into the R Script, this is what I've added to try to accommodate. Does this look okay? I think this is where I'm running into errors.
Added to R Script
args <- commandArgs(trailingOnly = TRUE)
if (length(args) == 0) {
if (!exists("dataDir")) { stop("variables dataDir not found") }
# Set dataDir variable when Running inside a R Session
args <- c(getwd(), 1, 2, ".", "Cirt 2014-1 Loan Level.txt", "Cirt 2014-
1", "62017", 175)
}
print(args)
# Input Values
Input.Directory <- paste(args[1]) ## getwd() , "/", "inputs", sep = "")
Begin.Sim <- args[2]
End.Sim <- args[3]
Output.Directory <- paste(args[1],"\\",args[4],sep = "") ##, "/", "outputs",
sep = "")
Pool.ID.File <- args[5] #"Cirt 2014-1 Loan Level.txt"
Pool.ID <- args[6] #"Cirt 2014-1"
asofdate <- args[7] #"62017"
Thirty.Yr.Mort.Spread <- args[8] # 175
When I try to run it in cmd using the .bat.. I get an error that says cannot change working directory. Anyone have any suggestions?
I sort-of understand where the error is but I'm struggling to fix it.
The path to my file with everything in it is
C:\MortgageMatt\Cirt2014-1
Edit:
I've also heard of something called R CMD Batch... should I look into that? I'm finding that it's an older technique.
What my code looked like before the Args/IF
# Input Values
Input.Directory <- "C:/Mortgage/Cirt 2014 - 1"
Output.Directory <- "C:/Mortgage/Cirt 2014 - 1"
Pool.ID.File <- "Cirt 2014-1 Loan Level.txt"
Pool.ID <- "Cirt 2014-1 NEW"
start<- 1
sims <- 2 # Number of Simulations
asofdate <- "62017"
Thirty.Yr.Mort.Spread <- 175
You can do all of this in R using one of these packages to parse command-line options:
docopt (my favourite)
optparse
argparse
getopt
or doing it manually -- not recommended.
You also do not want the older R CMD BATCH -- use Rscript (or littler, but littler does not work on Windows).
Code Example
#!/usr/bin/Rscript
suppressMessages(library(docopt))
doc <- "Usage: foo.R [-h] [-x] [--src REPODIR] [--out OUTDIR] [FILES...]
-s --src REPODIR source root directory [default: ~/git]
-o --out OUTDIR output directory [default: /tmp]
-h --help show this help text"
opt <- docopt(doc) # docopt parsing
print(opt)
Use with -h
You get a nice message, automatically, with not formatting need:
edd#rob:/tmp$ Rscript so50256138.R -h
Usage: foo.R [-h] [-x] [--src REPODIR] [--out OUTDIR] [FILES...]
-s --src REPODIR source root directory [default: ~/git]
-o --out OUTDIR output directory [default: /tmp]
-h --help show this help text
edd#rob:/tmp$
Use with argument
Note how one default argument is used, and the other from the command-line:
edd#rob:/tmp$ Rscript so50256138.R -s A
List of 9
$ --src : chr "A"
$ --out : chr "/tmp"
$ --help: logi FALSE
$ -x : logi FALSE
$ FILES : list()
$ src : chr "A"
$ out : chr "/tmp"
$ help : logi FALSE
$ x : logi FALSE
NULL
You can access them in opt by name or by option flag.
The docopt site has more; this is actually a portable specification and the CRAN package implements it for R.

R optparse error with command line arguments

For some reason, the optparse usage in this script breaks:
test.R:
#!/usr/bin/env Rscript
library("optparse")
option_list <- list(
make_option(c("-n", "--name"), type="character", default=FALSE,
dest="report_name", help="A different name to use for the file"),
make_option(c("-h", "--height"), type="numeric", default=12,
dest = "plot_height", help="Height for plot [default %default]",
metavar="plot_height"),
make_option(c("-w", "--width"), type="numeric", default=10,
dest = "plot_width", help="Width for plot [default %default]",
metavar="plot_width")
)
opt <- parse_args(OptionParser(option_list=option_list), positional_arguments = TRUE)
print(opt)
report_name <- opt$options$report_name
plot_height <- opt$options$plot_height
plot_width <- opt$options$plot_width
input_dir <- opt$args[1] # input directory
I get this error:
$ ./test.R --name "report1" --height 42 --width 12 foo
Error in getopt(spec = spec, opt = args) :
redundant short names for flags (column 2).
Calls: parse_args -> getopt
Execution halted
However, if I remove the "-h" from this line:
make_option(c("--height"), type="numeric", default=12,
dest = "plot_height", help="Height for plot [default %default]"
It seems to work fine;
$ ./test.R --name "report1" --height 42 --width 12 foo
$options
$options$report_name
[1] "report1"
$options$plot_height
[1] 42
$options$plot_width
[1] 12
$options$help
[1] FALSE
$args
[1] "foo"
Any ideas what might be going on here?
I am using R 3.3.0 and optparse_1.3.2 (getopt_1.20.0)
The -h flag is reserved by optparse (which is described as a feature of optparse which is not in getopt, from the getopt.R source file on Github):
Some features implemented in optparse package unavailable in getopt:
2. Automatic generation of an help option and printing of help text when encounters an "-h"
Therefore, when the user specifies -h, the sanity check for uniqueness of flags fails. The issue tracker does not seem to have any mention of the need to create a better error message for this case, however.
Finally, note that optparse seems to be invoking getopt, as they have the same author.

How properly use knit2html?

I'm trying to produce a HTML using that script:
title: "SQL"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output:
html_document:
keep_md: yes
---
```{r setup, echo = FALSE}
library(knitr)
path <- "drive"
extension <- "sql"
```
This document contains the code from all files with extension ``r extension`` in ``r paste0(getwd(), "/", path)``.
```{r, results = "asis", echo = FALSE}
fileNames <- list.files(path, pattern = sprintf(".*%s$", extension))
fileInfos <- file.info(paste0(path, fileNames))
for (fileName in fileNames) {
filePath <- paste0(path, fileName)
cat(sprintf("## File `%s` \n\n### Meta data \n\n", fileName))
cat(sprintf(
"| size (KB) | mode | modified |\n|---|---|---|\n %s | %s | %s\n\n",
round(fileInfos[filePath, "size"]/1024, 2),
fileInfos[filePath, "mode"],
fileInfos[filePath, "mtime"]))
cat(sprintf("### Content\n\n```\n%s\n```\n\n", paste(readLines(filePath), collapse = "\n")))
Using the same setup & R-scripts and same directories on a Windows machine works well. I suppose there is a problem with encoding.
Sometimes the execution begins with:
During startup - Warning messages:
1: Setting LC_CTYPE failed, using "C"
2: Setting LC_COLLATE failed, using "C"
3: Setting LC_TIME failed, using "C"
4: Setting LC_MESSAGES failed, using "C"
5: Setting LC_MONETARY failed, using "C"
6: Setting LC_PAPER failed, using "C"
7: Setting LC_MEASUREMENT failed, using "C"
and the execution just hangs
Then I have to:
Sys.setenv(LANG="de_DE.UTF-8")
and re-execute. Following output appears:
processing file: all_SQLs.Rmd
|................ | 25%
inline R code fragments
|................................ | 50%
label: setup (with options)
List of 2
$ echo : logi FALSE
$ indent: chr " "
|................................................. | 75%
inline R code fragments
|.................................................................| 100%
label: unnamed-chunk-1 (with options)
List of 2
$ results: chr "asis"
$ echo : logi FALSE
output file:SQL.knit.md
/usr/lib/rstudio-server/bin/pandoc/pandoc +RTS -K512m -RTS SQL.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash- implicit_figures --output all_SQLs.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /usr/local/lib/R/site-library /rmarkdown/rmd/h/default.html --variable 'theme:bootstrap' --include-in-header /tmp/RtmpBkTBSH/rmarkdown-str50021b4e910.html --mathjax --variable 'mathjax- url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX- AMS-MML_HTMLorMML' --no-highlight --variable highlightjs=/usr/local/lib/R/site- library/rmarkdown/rmd/h/highlight
pandoc: Cannot decode byte '\xfc': Data.Text.Internal.Encoding.Fusion.streamUtf8: Invalid UTF-8 stream
Error: pandoc document conversion failed with error 1
I suppose there is a problem with encodings. However I tried all I could by setting the locale in R as well as in Ubuntu to one standard:
locale R
[1] LC_CTYPE=de_DE.UTF-8 LC_NUMERIC=C LC_TIME=de_DE.UTF-8
[4] LC_COLLATE=de_DE.UTF-8 LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=de_DE.UTF-8
[7] LC_PAPER=de_DE.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
Locale Ubuntu
LANG=de_DE.UTF-8
LANGUAGE=
LC_CTYPE="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_PAPER="de_DE.UTF-8"
LC_NAME="de_DE.UTF-8"
LC_ADDRESS="de_DE.UTF-8"
LC_TELEPHONE="de_DE.UTF-8"
LC_MEASUREMENT="de_DE.UTF-8"
LC_IDENTIFICATION="de_DE.UTF-8"
LC_ALL=
When using knit2html() instead of the Knit HTMLbutton in Rstudio `I'll get the following:
warning:
1: In readlines (con):
incomplete last line in '/SQL.Rmd'
2: In RegExpr (".. '? [Hh] [1-6] *> (*) </ [Hh] [1-6] *.?>", Html, perl = TRUE):
Input string 1 is incorrect UTF-8
3: In Grepl (i, html, perl = TRUE):
Input string 1 is incorrect UTF-8
4: In Grepl (i, html, perl = TRUE):
Input string 1 is incorrect UTF-8

Resources