knit2pdf formatting incorrectly, producing warnings - r

I have been using knitr for a while to generate some quite complex PDFs. On one of my machines knit2pdf started to cause me issues, and I cannot figure out why. knit2pdf continues to work on my other machines.
My typical setup is:
test.Rnw
\documentclass{article}
\begin{document}
This is a test
\end{document}
R Command
knit2pdf(input = "latex/test.Rnw", output = 'latex/knit2pdf', clean = TRUE)
however, I receive the following error message:
knit2pdf(input = "latex/test2.Rnw", output = 'latex/knit2pdf', clean = TRUE)
processing file: latex/test2.Rnw
|.................................................................|
100% ordinary text without R code
output file: latex/knit2pdf
[1] "latex/knit2pdf.pdf" Warning message: running command
'"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texify.exe" --quiet --pdf
"knit2pdf" --max-iterations=20 -I
"C:/PROGRA~1/R/R-33~1.2/share/texmf/tex/latex" -I
"C:/PROGRA~1/R/R-33~1.2/share/texmf/bibtex/bst"' had status 1
and the contents of knit2pdf.pdf is:
article[]graphicx[]color fgcolorrgb0.345, 0.345, 0.345
[1][rgb]0.686,0.059,0.5691[1][rgb]0.192,0.494,0.81[1][rgb]0.678,0.584,0.6861[1][rgb]0,0,01[1][rgb]0.345,0.345,0.3451[1][rgb]0.1framed
kframetotalleftmargin# setminipage #end#of#kframe shadecolorrgb.97,
.97, .97 messagecolorrgb0, 0, 0 warningcolorrgb1, 0, 1 errorcolorrgb1,
0, 0 knitrout alltt upquote.styupquote document This is a test
I have managed to get the output I require (using the example from How to create multiple PDFs with different content from a single data frame?), but with significantly more work than just calling knit2pdf.
filename <- "texi2pdf"
f_tex <- paste0("latex/", filename, ".tex")
f_pdf <- paste0("latex/", filename, ".pdf")
knit("latex/test.Rnw", output = f_tex)
tools::texi2pdf(file = f_tex, clean = TRUE, quiet = FALSE)
file.rename(from = paste0(filename, ".pdf"), to = f_pdf)
And in this case the PDF output (texi2pdf.pdf) is simply, and correctly,
This is a test
I am running R 3.3.2 and all packages are up to date.

The solution as outlined by Yihui Xie on Github is to append the output file with '.tex'
i.e.
knit2pdf(input = "latex/test.Rnw", output = 'latex/knit2pdf.tex', clean = TRUE)
I was running knitr v1.11 which did not require this, but later versions do.

Related

Hmisc describe output when export as latex file giving API error 2

I am new to latex and I am trying to output the R results from describe( ) function through latex( ).
A .tex file is being created but it is not giving me a pdf output
df = data.frame(A = rnorm(n = 100, 0,1)) %>% dplyr::mutate(B = 2*A+ runif(n = 100,-0.05,0.05))
# call latex( )
f = 'sample.tex'
df_latext = Hmisc::latex(describe(df,descript = 'Sample Description'), file = f)
When I run df_latext in the R console it is giving me the following error:
MiKTeX Problem Report
Message: Windows API error 2: The system cannot find the file specified.
Data: path="C:\Users\domjo\AppData\Local\Temp\RtmpiYZcTM\file56f02ed47c3a.dvi"
Source: Libraries\MiKTeX\Core\File\win\winFile.cpp
Line: 301
MiKTeX: 22.8
OS: Windows 10.0.22000
Invokers: .../explorer/rstudio/rsession
SystemAdmin: no
Root0: C:\Users\domjo\AppData\Roaming\MiKTeX
Root1: C:\Users\domjo\AppData\Local\MiKTeX
Root2: C:\Users\domjo\AppData\Local\Programs\MiKTeX
UserInstall: C:\Users\domjo\AppData\Local\Programs\MiKTeX
UserConfig: C:\Users\domjo\AppData\Roaming\MiKTeX
UserData: C:\Users\domjo\AppData\Local\MiKTeX
Also, I tried running the .tex file created but its giving the following error saying Environment spacing undefined.:
! LaTeX Error: Environment spacing undefined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.1 \begin{spacing}{0.7}
?

R package development - Calling a function that renders a Rmarkdown file that uses `dplyr`? Mask errors

I am currently developing an R package and all the functions are working nicely except one. This problematic function was meant to render a Rmarkdown file for generating a final overview.
This Rmarkdown calls other functions from my package, but then for some reason, they do not work as expected when rendering the Rmarkdown.
Here's the function that renders my Rmarkdown:
#' #importFrom rmarkdown render
#' #export
quality_report <- function(data_folder = "test/test_dataset/sanger_sequences", outputfile = "QC_report.html", output_dir = "test/", processors = 1) {
input <- system.file("rmd", "HC_report.Rmd", package = "RepertoiR")
render(input,
output_dir = output_dir,
params = list(
data_folder = data_folder,
output_dir = output_dir,
processors = processors
),
output_file = outputfile
)
}
Here's the beginning of the Rmarkdown chunk where I get the error:
library(mypackage)
# Function to summarise result that works fine outside the Rmarkdown file
sf <- summarise_quality(folder_sequences = data_folder,
secondary.peak.ratio = 0.33,
trim.cutoff = 0.01,
processors = processors)
# If-conditional to try to stop knitting if the first function does not generate a data.frame as expected
if (is.null(dim(sf[["summaries"]]))) {
print("No files were processed, knitting was stopped early.")
knitr::knit_exit()
}
sf_summaries <- sf[["summaries"]]
# Use dplyr to just filter a random variable that should be present in the df
sf_filtered <- sf_summaries %>%
filter(raw.length >= 400)
ERROR:
Quitting from lines 61-104 (HC_report.Rmd)
Error in `filter()`:
! Problem while computing `..1 = raw.length >= 400`.
Caused by error in `mask$eval_all_filter()`:
! object 'raw.length' not found
Backtrace:
1. sf_summaries %>% filter(raw.length >= 400)
3. dplyr:::filter.data.frame(., raw.length >= 400)
4. dplyr:::filter_rows(.data, ..., caller_env = caller_env())
5. dplyr:::filter_eval(dots, mask = mask, error_call = error_call)
7. mask$eval_all_filter(dots, env_filter)
I thought it was some error regarding the masking so I have tried .data$raw.length to
see if it would work, but without success:
Error in `filter()`:
! Problem while computing `..1 = .data$raw.length >= 400`.
Caused by error in `.data$raw.length`:
! Column `raw.length` not found in `.data`.
Backtrace:
1. sf_summaries %>% filter(.data$raw.length >= 400)
11. rlang:::abort_data_pronoun(x, call = y)
I am almost thinking that dplyr does not work for development inside Rmarkdown files, I have functions that use dplyr and they work fine, I could fix errors by using .data for masking. Do you have any recommendations of what might be the issue or advice regarding functions to render Rmarkdown files?

Error: C stack usage is too close to the limit at R startup

Everytime I open a new session in RStudio, I'm greeted with the error message:
Error: C stack usage 7953936 is too close to the limit
Based on suggestions for similar issues posted here and here, I tried using the ulimit command in terminal, but get the following error.
Isabels-MacBook-Pro ~ % ulimit -s
8176
Isabels-MacBook-Pro ~ % R --slave -e 'Cstack_info()["size"]'
Error: C stack usage 7954496 is too close to the limit
Execution halted
Yet, when I run ulimit on it's own, I get:
Isabels-MacBook-Pro ~ % ulimit
unlimited
Just to double-check, I try setting it to unlimited again:
Isabels-MacBook-Pro ~ % ulimit -s unlimited
but then get a new error:
Isabels-MacBook-Pro ~ % R --slave -e 'Cstack_info()["size"]'
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Execution halted
I have no clue what this means in this context. Is the Cstack_info() the bit getting stuck on infinite recursion?? I'd love to get this figured out, as it's getting in the way of installing some necessary packages!
In case it's helpful, here's my session info
R version 4.1.3 (2022-03-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.2.1
And contents of .Rprofile
# REMEMBER to restart R after you modify and save this file!
# First, execute the global .Rprofile if it exists. You may configure blogdown
# options there, too, so they apply to any blogdown projects. Feel free to
# ignore this part if it sounds too complicated to you.
if (file.exists("~/.Rprofile")) {
base::sys.source("~/.Rprofile", envir = environment())
}
# Now set options to customize the behavior of blogdown for this project. Below
# are a few sample options; for more options, see
# https://bookdown.org/yihui/blogdown/global-options.html
options(
# to automatically serve the site on RStudio startup, set this option to TRUE
blogdown.serve_site.startup = FALSE,
# to disable knitting Rmd files on save, set this option to FALSE
blogdown.knit.on_save = TRUE,
# build .Rmd to .html (via Pandoc); to build to Markdown, set this option to 'm$
blogdown.method = 'html'
)
# fix Hugo version
options(blogdown.hugo.version = "0.82.0")
Here are the contents from /Library/Frameworks/R.framework/Resources/library/base/R/Rprofile
### This is the system Rprofile file. It is always run on startup.
### Additional commands can be placed in site or user Rprofile files
### (see ?Rprofile).
### Copyright (C) 1995-2020 The R Core Team
### Notice that it is a bad idea to use this file as a template for
### personal startup files, since things will be executed twice and in
### the wrong environment (user profiles are run in .GlobalEnv).
.GlobalEnv <- globalenv()
attach(NULL, name = "Autoloads")
.AutoloadEnv <- as.environment(2)
assign(".Autoloaded", NULL, envir = .AutoloadEnv)
T <- TRUE
F <- FALSE
R.version <- structure(R.Version(), class = "simple.list")
version <- R.version # for S compatibility
## for backwards compatibility only
R.version.string <- R.version$version.string
## NOTA BENE: options() for non-base package functionality are in places like
## --------- ../utils/R/zzz.R
options(keep.source = interactive())
options(warn = 0)
# options(repos = c(CRAN="#CRAN#"))
# options(BIOC = "http://www.bioconductor.org")
## setting from an env variable added in 4.0.2
local({to <- as.integer(Sys.getenv("R_DEFAULT_INTERNET_TIMEOUT", 60))
if (is.na(to) || to <= 0) to <- 60L
options(timeout = to)
})
options(encoding = "native.enc")
options(show.error.messages = TRUE)
## keep in sync with PrintDefaults() in ../../main/print.c :
options(show.error.messages = TRUE)
## keep in sync with PrintDefaults() in ../../main/print.c :
options(scipen = 0)
options(max.print = 99999)# max. #{entries} in internal printMatrix()
options(add.smooth = TRUE)# currently only used in 'plot.lm'
if(isFALSE(as.logical(Sys.getenv("_R_OPTIONS_STRINGS_AS_FACTORS_",
"FALSE")))) {
options(stringsAsFactors = FALSE)
} else {
options(stringsAsFactors = TRUE)
}
if(!interactive() && is.null(getOption("showErrorCalls")))
options(showErrorCalls = TRUE)
local({dp <- Sys.getenv("R_DEFAULT_PACKAGES")
if(identical(dp, "")) ## it fact methods is done first
dp <- c("datasets", "utils", "grDevices", "graphics",
"stats", "methods")
else if(identical(dp, "NULL")) dp <- character(0)
else dp <- strsplit(dp, ",")[[1]]
dp <- sub("[[:blank:]]*([[:alnum:]]+)", "\\1", dp) # strip whitespace
options(defaultPackages = dp)
})
## Expand R_LIBS_* environment variables.
Sys.setenv(R_LIBS_SITE =
.expand_R_libs_env_var(Sys.getenv("R_LIBS_SITE")))
Sys.setenv(R_LIBS_USER =
.expand_R_libs_env_var(Sys.getenv("R_LIBS_USER")))
local({
if(nzchar(tl <- Sys.getenv("R_SESSION_TIME_LIMIT_CPU")))
setSessionTimeLimit(cpu = tl)
if(nzchar(tl <- Sys.getenv("R_SESSION_TIME_LIMIT_ELAPSED")))
setSessionTimeLimit(elapsed = tl)
})
setSessionTimeLimit(elapsed = tl)
})
.First.sys <- function()
{
for(pkg in getOption("defaultPackages")) {
res <- require(pkg, quietly = TRUE, warn.conflicts = FALSE,
character.only = TRUE)
if(!res)
warning(gettextf('package %s in options("defaultPackages") was not found', sQuote(pkg)$
call. = FALSE, domain = NA)
}
}
## called at C level in the startup process prior to .First.sys
.OptRequireMethods <- function()
{
pkg <- "methods" # done this way to avoid R CMD check warning
if(pkg %in% getOption("defaultPackages"))
if(!require(pkg, quietly = TRUE, warn.conflicts = FALSE,
character.only = TRUE))
warning('package "methods" in options("defaultPackages") was not found',
call. = FALSE)
}
if(nzchar(Sys.getenv("R_BATCH"))) {
.Last.sys <- function()
{
cat("> proc.time()\n")
print(proc.time())
}
## avoid passing on to spawned R processes
## A system has been reported without Sys.unsetenv, so try this
try(Sys.setenv(R_BATCH=""))
}
local({
if(nzchar(rv <- Sys.getenv("_R_RNG_VERSION_")))
local({
if(nzchar(rv <- Sys.getenv("_R_RNG_VERSION_")))
suppressWarnings(RNGversion(rv))
})
.sys.timezone <- NA_character_
.First <- NULL
.Last <- NULL
###-*- R -*- Unix Specific ----
.Library <- file.path(R.home(), "library")
.Library.site <- Sys.getenv("R_LIBS_SITE")
.Library.site <- if(!nzchar(.Library.site)) file.path(R.home(), "site-library") else unlist(strspl$
.Library.site <- .Library.site[file.exists(.Library.site)]
invisible(.libPaths(c(unlist(strsplit(Sys.getenv("R_LIBS"), ":")),
unlist(strsplit(Sys.getenv("R_LIBS_USER"), ":")
))))
local({
popath <- Sys.getenv("R_TRANSLATIONS", "")
if(!nzchar(popath)) {
paths <- file.path(.libPaths(), "translations", "DESCRIPTION")
popath <- dirname(paths[file.exists(paths)][1])
}
bindtextdomain("R", popath)
bindtextdomain("R-base", popath)
assign(".popath", popath, .BaseNamespaceEnv)
})
local({
## we distinguish between R_PAPERSIZE as set by the user and by configure
papersize <- Sys.getenv("R_PAPERSIZE_USER")
if(!nchar(papersize)) {
lcpaper <- Sys.getlocale("LC_PAPER") # might be null: OK as nchar is 0
papersize <- if(nchar(lcpaper))
if(length(grep("(_US|_CA)", lcpaper))) "letter" else "a4"
else Sys.getenv("R_PAPERSIZE")
}
options(papersize = papersize,
}
options(papersize = papersize,
printcmd = Sys.getenv("R_PRINTCMD"),
dvipscmd = Sys.getenv("DVIPS", "dvips"),
texi2dvi = Sys.getenv("R_TEXI2DVICMD"),
browser = Sys.getenv("R_BROWSER"),
pager = file.path(R.home(), "bin", "pager"),
pdfviewer = Sys.getenv("R_PDFVIEWER"),
useFancyQuotes = TRUE)
})
## non standard settings for the R.app GUI of the macOS port
if(.Platform$GUI == "AQUA") {
## this is set to let RAqua use both X11 device and X11/TclTk
if (Sys.getenv("DISPLAY") == "")
Sys.setenv("DISPLAY" = ":0")
## this is to allow gfortran compiler to work
Sys.setenv("PATH" = paste(Sys.getenv("PATH"),":/usr/local/bin",sep = ""))
}## end "Aqua"
## de-dupe the environment on macOS (bug in Yosemite which affects things like PATH)
if (grepl("^darwin", R.version$os)) local({
## we have to de-dupe one at a time and re-check since the bug affects how
## environment modifications propagate
while(length(dupes <- names(Sys.getenv())[table(names(Sys.getenv())) > 1])) {
env <- dupes[1]
value <- Sys.getenv(env)
Sys.unsetenv(env) ## removes the dupes, good
.Internal(Sys.setenv(env, value)) ## wrapper requries named vector, a pain, hence internal
}
})
local({
tests_startup <- Sys.getenv("R_TESTS")
if(nzchar(tests_startup)) source(tests_startup)
})
Is there anything glaring here that could be causing the issue?
Your user .Rprofile file is loading itself recursively for some reason:
if (file.exists("~/.Rprofile")) {
base::sys.source("~/.Rprofile", envir = environment())
}
From your comments it seems that these lines are inside ~/.Rprofile (~ expands to the user home directory, i.e. /Users/mycomputer in your case, assuming mycomputer is your user name).
Delete these lines (or comment them out), they don’t belong here. In fact, the file looks like it’s a template for a project-specific .Rprofile configuration. It would make sense inside a project directory, but not as the profile-wide user .Rprofile.
The logic for these files is as follows:
If there is an .Rprofile file in the current directory, R attempts to load that.
Otherwise, if the environment variable R_PROFILE_USER is set to the path of a file, R attempts to load this file.
Otherwise, if the file ~/.Rprofile exists, R attempts to load that.
Now, this implies that ~/.Rprofile is not loaded automatically if a projects-specific (= in the current working directory) .Rprofile exists. This is unfortunate, therefore many projects add lines similar to the above to their project-specific .Rprofile files to cause the user-wide ~/.Rprofile to be loaded as well. However, the above implementation ignores the R_PROFILE_USER environment variable. A better implementation would therefore look as follows:
rprofile = Sys.getenv('R_PROFILE_USER', '~/.Rprofile')
if (file.exists(rprofile)) {
base::sys.source(rprofile, envir = environment())
}
rm(rprofile)
Success! Thank you to everyone in the comment. The issue was resolved by deleting /Library/Frameworks/R.framework/Resources/library/base/R/Rprofile and re-installing R and Rstudio.

Problem using R by shell - But the same script works at RStudio

I have this script in shell:
R -q -e "library(knitr);data<- read.table('dados.txt', header = T);siregid.table<- as.data.frame(table(unlist(data$siregid))); a=data.frame(unclass(summary(siregid.table$Freq)), check.names = FALSE, stringsAsFactors = FALSE);a$names<-rownames(a);a$names<-as.character(a$names);b<- a[ , order(names(a))];c <- setNames(data.frame(t(b[,-1])), b[,1]); kable(head(c), format = 'rst', row.names = FALSE);"
but this error occurs:
Error in order(names(a)) : argument 1 is not a vector
Execution halted
but the same script works perfectly when I run at RStudio. What's the matter?
update:
My table a in shell (terminal)
and My table ain RStudio:
The problem was the dollar sign. Just this.

Error trying to read a PDF using readPDF from the tm package

(Windows 7 / R version 3.0.1)
Below the commands and the resulting error:
> library(tm)
> pdf <- readPDF(PdftotextOptions = "-layout")
> dat <- pdf(elem = list(uri = "17214.pdf"), language="de", id="id1")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp
\RtmpS8Uql1\pdfinfo167c2bc159f8': No such file or directory
How do I solve this issue?
EDIT I
(As suggested by Ben and described here)
I downloaded Xpdf copied the 32bit version to
C:\Program Files (x86)\xpdf32
and the 64bit version to
C:\Program Files\xpdf64
The environment variables pdfinfo and pdftotext are referring to the respective executables either 32bit (tested with R 32bit) or to 64bit (tested with R 64bit)
EDIT II
One very confusing observation is that starting from a fresh session (tm not loaded) the last command alone will produce the error:
> dat <- pdf(elem = list(uri = "17214.pdf"), language="de", id="id1")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp\RtmpKi5GnL
\pdfinfode8283c422f': No such file or directory
I don't understand this at all because the function variable is not defined by tm.readPDF yet. Below you'll find the function pdf refers to "naturally" and to what is returned by tm.readPDF:
> pdf
function (elem, language, id)
{
meta <- tm:::pdfinfo(elem$uri)
content <- system2("pdftotext", c(PdftotextOptions, shQuote(elem$uri),
"-"), stdout = TRUE)
PlainTextDocument(content, meta$Author, meta$CreationDate,
meta$Subject, meta$Title, id, meta$Creator, language)
}
<environment: 0x0674bd8c>
> library(tm)
> pdf <- readPDF(PdftotextOptions = "-layout")
> pdf
function (elem, language, id)
{
meta <- tm:::pdfinfo(elem$uri)
content <- system2("pdftotext", c(PdftotextOptions, shQuote(elem$uri),
"-"), stdout = TRUE)
PlainTextDocument(content, meta$Author, meta$CreationDate,
meta$Subject, meta$Title, id, meta$Creator, language)
}
<environment: 0x0c3d7364>
Apparently there is no difference - then why use readPDF at all?
EDIT III
The pdf file is located here: C:\Users\Raffael\Documents
> getwd()
[1] "C:/Users/Raffael/Documents"
EDIT IV
First instruction in pdf() is a call to tm:::pdfinfo() - and there the error is caused within the first few lines:
> outfile <- tempfile("pdfinfo")
> on.exit(unlink(outfile))
> status <- system2("pdfinfo", shQuote(normalizePath("C:/Users/Raffael/Documents/17214.pdf")),
+ stdout = outfile)
> tags <- c("Title", "Subject", "Keywords", "Author", "Creator",
+ "Producer", "CreationDate", "ModDate", "Tagged", "Form",
+ "Pages", "Encrypted", "Page size", "File size", "Optimized",
+ "PDF version")
> re <- sprintf("^(%s)", paste(sprintf("%-16s", sprintf("%s:",
+ tags)), collapse = "|"))
> lines <- readLines(outfile, warn = FALSE)
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp\RtmpquRYX6\pdfinfo8d419174450': No such file or direc
Apparently tempfile() simply doesn't create a file.
> outfile <- tempfile("pdfinfo")
> outfile
[1] "C:\\Users\\Raffael\\AppData\\Local\\Temp\\RtmpquRYX6\\pdfinfo8d437bd65d9"
The folder C:\Users\Raffael\AppData\Local\Temp\RtmpquRYX6 exists and holds some files but none is named pdfinfo8d437bd65d9.
Intersting, on my machine after a fresh start pdf is a function to convert an image to a PDF:
getAnywhere(pdf)
A single object matching ‘pdf’ was found
It was found in the following places
package:grDevices
namespace:grDevices [etc.]
But back to the problem of reading in PDF files as text, fiddling with the PATH is a bit hit-and-miss (and annoying if you work across several different computers), so I think the simplest and safest method is to call pdf2text using system as Tony Breyal describes here.
In your case it would be (note the two sets of quotes):
system(paste('"C:/Program Files/xpdf64/pdftotext.exe"',
'"C:/Users/Raffael/Documents/17214.pdf"'), wait=FALSE)
This could easily be extended with an *apply function or loop if you have many PDF files.

Resources