comments splitting into multiple lines in RMarkdown - r-markdown

I'm writing a document in RMarkdown, and sometimes I want to have comments in my code chunks that show in the PDF, like this:
vector <- 1:4
# finds the mean of these values
mean(vector)
## [1] 2.5
However, when I knit the PDF (or HTML document), it breaks the comment up into one-word lines, like this:
vector <- 1:4
# finds
# the
# mean
# of
# these
# values
mean(vector)
## [1] 2.5
This is really annoying, and it makes my documents way longer than they need to be. How can I fix it?
EDIT: updated all packages; here's the session info:
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] abind_1.4-5
loaded via a namespace (and not attached):
[1] backports_1.0.5 magrittr_1.5 rprojroot_1.2 htmltools_0.3.5 tools_3.3.1 yaml_2.1.14 Rcpp_0.12.9 stringi_1.1.2
[9] rmarkdown_1.3 knitr_1.15.1 stringr_1.1.0 digest_0.6.12 evaluate_0.10

It might be caused by knitr/formattR's code tidying, with a width.cutoff set too small. You can check your chunk options with:
opts_chunk$get()
And maybe set a larger width.cutoff; formatR will try to make sure no line in your code has a higher number of character than this:
opts_chunk$set(tidy=T‌​RUE,
tidy.opts=list(width.cutoff=60))

Related

Changing decimal and group mark in rmarkdown render

I am working in RStudio (on a Mac) to write reports which are rendered as PDF.
I am working in an English locale.
The report, however, is in German and I would like to use German decimal and group (thousands) marks in the report without changing the locale of the OS (or R).
I would like to do this by setting one global option at the beginning of the Rmd file.
Is there a way to accomplish this?
Up until now I tried
command line in Terminal
defaults write org.R-project.R force.LANG de_AT.UTF-8
This only changed the UI language used by R itself, no by RStudio.
Sys.setlocale("LC_NUMERIC","de_DE")
in an Rmd document produces
Error: package or namespace load failed for ’sf': .onLoad failed in loadNamespace() for ’units’, details: call:
udunits_initCfile.path(.get_ud_xml_dir(), "udunits2.xml")) error: Error in function udunits.init: Can't open
installed, default, unit database Execution halted
Putting
NUMERIC="de_DE"
LC_NUMERIC="de_DE"
in .Renviron does not help either
Quoting ?locales
Setting "LC_NUMERIC" to any value other than "C" may cause R to function anomalously, so gives a warning. Input conversions in R itself are unaffected, but the reading and writing of ASCII save files will be, as may packages which do their own input/output.
Setting it temporarily on a Unix-alike to produce graphical or text output may work well enough, but options(OutDec) is often preferable.
So let's try that:
---
output: pdf_document
lang: de-DE
---
```{r echo=FALSE}
options(OutDec = ",")
library(sf)
knitr::kable(head(mtcars))
```
Dies ist ein nichtssagender Blindtext inklusive R output `r pi` der genau so
lang ist, dass es zu Zeilenumbrüchen mit Trennung kommt.
Result:
Notes:
There is no group-separator, since R does not use that in its output.
I have added lang: de-DE to enable German hyphenation patterns.
Loading the sf package works without problems.

Flexdashboard opens new tab with every user input

I would like to ask for help with a Flexdashboard using Plotly and Shiny. The only thing now is that the app is throwing away browser tabs like there's no tomorrow. For every reactive input from the user, the app opens a new tab, where it does produce the correct output.
I've noticed that Flexdashboard, Plotly, and Shiny all work well for me. The problem seems to arise somewhere in their combination.
I would greatly appreciate some advice. The reproducible .Rmd script may be found on Github. It was deployed in about two minutes by using the code below:
install.packages('arules')
install.packages('car')
install.packages('contrast')
install.packages('corpcor')
install.packages('doBy')
install.packages('dplyr')
install.packages('flexdashboard')
install.packages('gdata')
install.packages('ggplot2')
install.packages('ggrepel')
install.packages('GPArotation')
install.packages('gtools')
install.packages('Hmisc')
install.packages('irr')
install.packages('lattice')
install.packages('leaflet')
install.packages('ltm')
install.packages('MASS')
install.packages('pastecs')
install.packages('plotly')
install.packages('plyr')
install.packages('png')
install.packages('psych')
install.packages('qpcR')
install.packages('QuantPsyc')
install.packages('RColorBrewer')
install.packages('RCurl')
install.packages('reshape')
install.packages('Rmisc')
install.packages('rsconnect')
install.packages('scales')
install.packages('shiny')
install.packages('tibble')
library(arules)
library(car)
library(contrast)
library(corpcor)
library(doBy)
library(dplyr)
library(flexdashboard)
library(gdata)
library(ggplot2)
library(ggrepel)
library(GPArotation)
library(gtools)
library(Hmisc)
library(irr)
library(lattice)
library(leaflet)
library(ltm)
library(MASS)
library(pastecs)
library(plotly)
library(plyr)
library(png)
library(psych)
library(qpcR)
library(QuantPsyc)
library(RColorBrewer)
library(RCurl)
library(reshape)
library(Rmisc)
library(rsconnect)
library(scales)
library(shiny)
library(tibble)
rmarkdown::run('complete norms flex.rmd')
Below is the output I got (which didn't reveal a lot to me).
> rmarkdown::run('complete norms flex.rmd')
Loading required package: shiny
Listening on http://127.0.0.1:6295
processing file: complete_norms_flex.rmd
|
| | 0%
|
|..... | 8%
ordinary text without R code
|
|.......... | 15%
label: global (with options)
List of 1
$ include: logi FALSE
Loading required package: MASS
Loading required package: msm
Loading required package: polycor
Attaching package: 'psych'
The following object is masked from 'package:ltm':
factor.scores
The following object is masked from 'package:polycor':
polyserial
Loading required package: carData
Attaching package: 'car'
The following object is masked from 'package:psych':
logit
Loading required package: rms
Loading required package: Hmisc
Loading required package: survival
Loading required package: Formula
Loading required package: ggplot2
RStudio Community is a great place to get help:
https://community.rstudio.com/c/tidyverse.
Attaching package: 'ggplot2'
The following objects are masked from 'package:psych':
%+%, alpha
Attaching package: 'Hmisc'
The following object is masked from 'package:psych':
describe
The following objects are masked from 'package:base':
format.pval, units
Loading required package: SparseM
Attaching package: 'SparseM'
The following object is masked from 'package:base':
backsolve
Attaching package: 'rms'
The following object is masked from 'package:Hmisc':
plotp
The following objects are masked from 'package:car':
Predict, vif
The following object is masked from 'package:shiny':
validate
Attaching package: 'pastecs'
The following object is masked from 'package:rms':
specs
Attaching package: 'scales'
The following objects are masked from 'package:psych':
alpha, rescale
Loading required package: Matrix
Attaching package: 'Matrix'
The following object is masked from 'package:reshape':
expand
Attaching package: 'arules'
The following object is masked from 'package:car':
recode
The following objects are masked from 'package:base':
abbreviate, write
Attaching package: 'plyr'
The following objects are masked from 'package:reshape':
rename, round_any
The following objects are masked from 'package:Hmisc':
is.discrete, summarize
Attaching package: 'magrittr'
The following object is masked from 'package:pastecs':
extract
Attaching package: 'dplyr'
The following objects are masked from 'package:plyr':
arrange, count, desc, failwith, id, mutate, rename, summarise,
summarize
The following objects are masked from 'package:arules':
intersect, recode, setdiff, setequal, union
The following object is masked from 'package:reshape':
rename
The following objects are masked from 'package:pastecs':
first, last
The following objects are masked from 'package:Hmisc':
src, summarize
The following object is masked from 'package:car':
recode
The following object is masked from 'package:MASS':
select
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
Attaching package: 'rlang'
The following object is masked from 'package:magrittr':
set_names
gdata: Unable to locate valid perl interpreter
gdata:
gdata: read.xls() will be unable to read Excel XLS and XLSX files
gdata: unless the 'perl=' argument is used to specify the location
gdata: of a valid perl intrpreter.
gdata:
gdata: (To avoid display of this message in the future, please
gdata: ensure perl is installed and available on the executable
gdata: search path.)
gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLX' (Excel 97-2004) files.
gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLSX' (Excel 2007+) files.
gdata: Run the function 'installXLSXsupport()'
gdata: to automatically download and install the perl
gdata: libaries needed to support Excel XLS and XLSX formats.
Attaching package: 'gdata'
The following objects are masked from 'package:rlang':
env, ll
The following objects are masked from 'package:dplyr':
combine, first, last
The following objects are masked from 'package:pastecs':
first, last
The following object is masked from 'package:stats':
nobs
The following object is masked from 'package:utils':
object.size
The following object is masked from 'package:base':
startsWith
Loading required package: boot
Attaching package: 'boot'
The following object is masked from 'package:survival':
aml
The following object is masked from 'package:car':
logit
The following object is masked from 'package:psych':
logit
The following object is masked from 'package:lattice':
melanoma
The following object is masked from 'package:msm':
cav
Attaching package: 'QuantPsyc'
The following object is masked from 'package:Matrix':
norm
The following object is masked from 'package:SparseM':
norm
The following object is masked from 'package:base':
norm
Loading required package: minpack.lm
Loading required package: rgl
Loading required package: robustbase
Attaching package: 'robustbase'
The following object is masked from 'package:boot':
salinity
The following object is masked from 'package:survival':
heart
The following object is masked from 'package:psych':
cushny
Attaching package: 'gtools'
The following objects are masked from 'package:boot':
inv.logit, logit
The following object is masked from 'package:rlang':
chr
The following object is masked from 'package:car':
logit
The following object is masked from 'package:psych':
logit
Loading required package: bitops
Attaching package: 'rsconnect'
The following object is masked from 'package:shiny':
serverInfo
Attaching package: 'plotly'
The following objects are masked from 'package:plyr':
arrange, mutate, rename, summarise
The following object is masked from 'package:reshape':
rename
The following object is masked from 'package:Hmisc':
subplot
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:MASS':
select
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
R was not square, finding R from data
R was not square, finding R from data
|
|............... | 23%
ordinary text without R code
|
|.................... | 31%
label: unnamed-chunk-1
|
|......................... | 38%
ordinary text without R code
|
|.............................. | 46%
label: unnamed-chunk-2
|
|................................... | 54%
ordinary text without R code
|
|........................................ | 62%
label: unnamed-chunk-3
|
|............................................. | 69%
ordinary text without R code
|
|.................................................. | 77%
label: unnamed-chunk-4
|
|....................................................... | 85%
ordinary text without R code
|
|............................................................ | 92%
label: unnamed-chunk-5 (with options)
List of 3
$ fig.width : num 5
$ fig.height: num 5
$ echo : logi FALSE
|
|.................................................................| 100%
ordinary text without R code
output file: C:/Users/Pablo/AppData/Local/Temp/RtmpuWjXVZ/complete_norms_flex.knit.md
"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS "C:/Users/Pablo/AppData/Local/Temp/RtmpuWjXVZ/complete_norms_flex.utf8.md" --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc1de01a9e76fc.html --smart --email-obfuscation none --standalone --section-divs --template "C:\Users\Pablo\Documents\R\win-library\3.5\flexdashboard\rmarkdown\templates\flex_dashboard\resources\default.html" --include-in-header "C:\Users\Pablo\AppData\Local\Temp\RtmpuWjXVZ\rmarkdown-str1de074483ff7.html" --id-prefix section- --variable "theme:cosmo" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --include-in-header "C:\Users\Pablo\AppData\Local\Temp\RtmpuWjXVZ\file1de02c6f317chtml" --include-before-body "C:\Users\Pablo\AppData\Local\Temp\RtmpuWjXVZ\file1de0258b6988.html" --include-after-body "C:\Users\Pablo\AppData\Local\Temp\RtmpuWjXVZ\file1de04c9e1cbf.html" --highlight-style pygments --include-before-body "C:\Users\Pablo\AppData\Local\Temp\RtmpuWjXVZ\file1de0220034f3.html" --include-after-body "C:\Users\Pablo\AppData\Local\Temp\RtmpuWjXVZ\file1de013ca2efd.html"
Output created: C:/Users/Pablo/AppData/Local/Temp/RtmpuWjXVZ/file1de059b61073.html
I expected the dashboard to stay on one browser tab, but it multiplies itself.
Thank you very much.
Just use renderPlotly() instead of renderPlot(). Found through these two other questions:
Shiny renderPlot within Interactive Document opens a new Browser Window with dygraph
https://stackoverflow.com/a/51378684/7050882

R error: join_cols() / join_vert(): number of columns must be the same

I have a problem fitting instrumental variable models with covariates with the bayesm package in R. Adding covariates results in the error message :
"error: join_cols() / join_vert(): number of columns must be the same"
The error stems from the external function 'rivDP_rcpp_loop.cpp' called via Rcpp. I am however not skilled enough to handle the problem on R level.
I constructed an example based on the function example which results in the error on my machine.
##
## simulate scaled log-normal errors and run
##
set.seed(66)
k=10
delta=1.5
Sigma=matrix(c(1,.6,.6,1),ncol=2)
N=1000
tbeta=4
set.seed(66)
scalefactor=.6
root=chol(scalefactor*Sigma)
mu=c(1,1)
##
## compute interquartile ranges
##
ninterq=qnorm(.75)-qnorm(.25)
error=matrix(rnorm(100000*2),ncol=2)
error=t(t(error)+mu)
Err=t(t(exp(error))-exp(mu+.5*scalefactor*diag(Sigma)))
lnNinterq=quantile(Err[,1],prob=.75)-quantile(Err[,1],prob=.25)
##
## simulate data
##
error=matrix(rnorm(N*2),ncol=2)%*%root
error=t(t(error)+mu)
Err=t(t(exp(error))-exp(mu+.5*scalefactor*diag(Sigma)))
#
# scale appropriately
Err[,1]=Err[,1]*ninterq/lnNinterq
Err[,2]=Err[,2]*ninterq/lnNinterq
z=matrix(runif(k*N),ncol=k)
x=z%*%(delta*c(rep(1,k)))+Err[,1]
y=x*tbeta+Err[,2]
w<-matrix(rnorm(10000),ncol=10)
# set intial values for MCMC
Data = list(); Mcmc=list()
Data$z<-cbind(z,w); Data$x=x; Data$y=y; Data$w<-w
# start MCMC and keep results
Mcmc$maxuniq=100
Mcmc$R=R
end=Mcmc$R
begin=100
out=rivDP(Data=Data,Mcmc=Mcmc)
I had the same problem and have contacted the package maintainer for clarification. In the mean time I was able to get the rivDP() function to work by downloading the version 2.2-5 from 2012. Make sure to install Rtools first (from here: https://cran.r-project.org/bin/windows/Rtools/) and then run the following code:
packageurl <- "https://cran.r-project.org/src/contrib/Archive/bayesm/bayesm_2.2-5.tar.gz"
install.packages(packageurl, repos=NULL, type="source")
library(bayesm)

Running compiled C++ code with Rcpp

I have been working my way through Dirk Eddelbuettel's Rcpp tutorial here:
http://www.rinfinance.com/agenda/
I have learned how to save a C++ file in a directory and call it and run it from within R. The C++ file I am running is called 'logabs2.ccp' and its contents are directly from one of Dirk's slides:
#include <Rcpp.h>
using namespace Rcpp;
inline double f(double x) { return ::log(::fabs(x)); }
// [[Rcpp::export]]
std::vector<double> logabs2(std::vector<double> x) {
std::transform(x.begin(), x.end(), x.begin(), f);
return x;
}
I run it with this R code:
library(Rcpp)
sourceCpp("c:/users/mmiller21/simple r programs/logabs2.cpp")
logabs2(seq(-5, 5, by=2))
# [1] 1.609438 1.098612 0.000000 0.000000 1.098612 1.609438
I am running the code on a Windows 7 machine from within the R GUI that seems to install by default. I also installed the most recent version of Rtools. The above R code seems to take a relatively long time to run. I suspect most of that time is devoted to compiling the C++ code and that once the C++ code is compiled it runs very quickly. Microbenchmark certainly suggests that Rcpp reduces computation time.
I have never used C++ until now, but I know that when I compile C code I get an *.exe file. I have searched my hard-drive from a file called logabs2.exe but cannot find one. I am wondering whether the above C++ code might run even faster if a logabs2.exe file was created. Is it possible to create a logabs2.exe file and store it in a folder somewhere and then have Rcpp call that file whenever I wanted to use it? I do not know whether that makes sense. If I could store a C++ function in an *.exe file then perhaps I would not have to compile the function every time I wanted to use it with Rcpp and then perhaps the Rcpp code would be even faster.
Sorry if this question does not make sense or is a duplicate. If it is possible to store the C++ function as an *.exe file I am hoping someone will show me how to modify my R code above to run it. Thank you for any help with this or for setting me straight on why what I suggest is not possible or recommended.
I look forward to seeing Dirk's new book.
Thank you to user1981275, Dirk Eddelbuettel and Romain Francois for their responses. Below is how I compiled a C++ file and created a *.dll, then called and used that *.dll file inside R.
Step 1. I created a new folder called 'c:\users\mmiller21\myrpackages' and pasted the file 'logabs2.cpp' into that new folder. The file 'logabs2.cpp' was created as described in my original post.
Step 2. Inside the new folder I created a new R package called 'logabs2' using an R file I wrote called 'new package creation.r'. The contents of 'new package creation.r' are:
setwd('c:/users/mmiller21/myrpackages/')
library(Rcpp)
Rcpp.package.skeleton("logabs2", example_code = FALSE, cpp_files = c("logabs2.cpp"))
I found the above syntax for Rcpp.package.skeleton on one of Hadley Wickham's websites: https://github.com/hadley/devtools/wiki/Rcpp
Step 3. I installed the new R package "logabs2" in R using the following line in the DOS command window:
C:\Program Files\R\R-3.0.1\bin\x64>R CMD INSTALL -l c:\users\mmiller21\documents\r\win-library\3.0\ c:\users\mmiller21\myrpackages\logabs2
where:
the location of the rcmd.exe file is:
C:\Program Files\R\R-3.0.1\bin\x64>
the location of installed R packages on my computer is:
c:\users\mmiller21\documents\r\win-library\3.0\
and the location of my new R package prior to being installed is:
c:\users\mmiller21\myrpackages\
Syntax used in the DOS command window was found by trial and error and may not be ideal. At some point I pasted a copy of 'logabs2.cpp' in 'C:\Program Files\R\R-3.0.1\bin\x64>' but I do not think that mattered.
Step 4. After installing the new R package I ran it using an R file I named 'new package usage.r' in the 'c:/users/mmiller21/myrpackages/' folder (although I do not think the folder was important). The contents of 'new package usage.r' are:
library(logabs2)
logabs2(seq(-5, 5, by=2))
The output was:
# [1] 1.609438 1.098612 0.000000 0.000000 1.098612 1.609438
This file loaded the package Rcpp without me asking.
In this case base R was faster assuming I did this correctly.
#> microbenchmark(logabs2(seq(-5, 5, by=2)), times = 100)
#Unit: microseconds
# expr min lq median uq max neval
# logabs2(seq(-5, 5, by = 2)) 43.086 44.453 50.6075 69.756 190.803 100
#> microbenchmark(log(abs(seq(-5, 5, by=2))), times=100)
#Unit: microseconds
# expr min lq median uq max neval
# log(abs(seq(-5, 5, by = 2))) 38.298 38.982 39.666 40.35 173.023 100
However, using the dll file was faster than calling the external cpp file:
system.time(
cppFunction("
NumericVector logabs(NumericVector x) {
return log(abs(x));
}
")
)
# user system elapsed
# 0.06 0.08 5.85
Although base R seems faster or as fast as the *.dll file in this case, I have no doubt that using the *.dll file with Rcpp will be faster than base R in most cases.
This was my first attempt creating an R package or using Rcpp and no doubt I did not use the most efficient methods. Also, I apologize for any typographic errors in this post.
EDIT
In a comment below I think Romain Francois suggested I modify the *.cpp file to the following:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector logabs(NumericVector x) {
return log(abs(x));
}
and recreate my R package, which I have now done. I then compared base R against my new package using the following code:
library(logabs)
logabs(seq(-5, 5, by=2))
log(abs(seq(-5, 5, by=2)))
library(microbenchmark)
microbenchmark(logabs(seq(-5, 5, by=2)), log(abs(seq(-5, 5, by=2))), times = 100000)
Base R is still a tiny bit faster or no different:
Unit: microseconds
expr min lq median uq max neval
logabs(seq(-5, 5, by = 2)) 42.401 45.137 46.505 69.073 39754.598 1e+05
log(abs(seq(-5, 5, by = 2))) 37.614 40.350 41.718 62.234 3422.133 1e+05
Perhaps this is because base R is already vectorized. I suspect with more complex functions base R will be much slower. Or perhaps I am still not using the most efficient approach, or perhaps I simply made an error somewhere.
You say
I have never used C++ until now, but I know that when I compile C code
I get an *.exe file
and that is true if and only you build an executable. Here, we build dynamically loadable libraries and those thend to have different extensionos depending on the operating system: .dll for Windoze, .so for Linux, .dynlib for OS X.
So nothing wrong here, you simply had the wrong assumption.
If you want to get some entity you can keep, what you are looking for is an R package. There are many resources online to learn how to make them (e.g. Hadley's slides).
We have Rcpp.package.skeleton you might find useful.
So, the function is compiled once when the package is installed, and then you just use it.

Using CMake with CTest and CDash

I am going to use CDash with CMake/CTest on my C++ project.
In order to enable CDash and customize settings, like
"MEMORYCHECK_SUPPRESSIONS_FILE", "DART_TESTING_TIMEOUT", I added the following lines in the root CMakeLists.txt
set(MEMORYCHECK_SUPPRESSIONS_FILE "${CMAKE_SOURCE_DIR}/valgrind.supp")
set(DART_TESTING_TIMEOUT "120")
include(CTest)
However, the generated "DartConfiguration.tcl" does not contain my settings at all
( MemoryCheckSuppressionFile is empty and TimeOut is still the default value )
I found that, for example, if I pass -DDART_TESTING_TIMEOUT=STRING:120 , it works , but it fails if specifying them in the CMakeLists.txt.
Thank you in advance :)
DartConfiguration.tcl
# Dynamic analisys and coverage
PurifyCommand:
ValgrindCommand:
ValgrindCommandOptions:
MemoryCheckCommand: /usr/bin/valgrind
MemoryCheckCommandOptions:
MemoryCheckSuppressionFile:
CoverageCommand: /usr/bin/gcov
# Testing options
# TimeOut is the amount of time in seconds to wait for processes
# to complete during testing. After TimeOut seconds, the
# process will be summaily terminated.
# Currently set to 25 -9.0.0.71596-0inutes
TimeOut: 1500
There are three possible solutions:
You create cache variables. This also creates a GUI entry for the variable, which is not always what you want for automatic testing: SET(DART_TESTING_TIMEOUT "120" CACHE STRING "")
You specify your options with a simple "set" command, but in a file called DartConfig.cmake instead of the main CMakeLists.txt . This file gets parsed to create the DartConfiguration.tcl
You use CTest scripting to set up your dartclient: http://www.cmake.org/Wiki/CMake_Scripting_Of_CTest