R Markdown - xtable with longtable and scalebox outputs scalebox value - r

I am creating a PDF document with with rmarkdown and knitr. Below is an example code chunk. When knitting to PDF it prints the scalebox value to the PDF, which I don't want. My actual table is much wider so using the scalebox argument is necessary.
```{r, results = 'asis', echo = FALSE, message = FALSE, warning=FALSE}
x <- matrix(rnorm(1000), ncol = 10)
x.big <- xtable(x)
print.xtable(x.big, hline.after=c(-1), tabular.environment = "longtable", scalebox = 0.7)
```
This only happens when using the longtable tabular environment. Running the same code chunk with the standard tabular environment doesn't output the scalebox info. I've tried setting every comment argument in the print.xtable function and the r code chunk to FALSE but with no luck.
How can I output my PDF file without that scalebox text being printed?

I haven't found a way to get around the scalebox issue. What I ended up doing was using was the size argument in print.xtable instead. Below is a sample function where size is an integer representing the desired size of the font.
outputXtableTest <- function( df, size){
sizeNew = paste0("\\fontsize{", size,"pt}{", size+1, "pt}\\selectfont")
print.xtable(
df, hline.after=c(-1,0, 1:nrow(table)),
tabular.environment = 'longtable',
floating = FALSE, size = sizeNew
)
}
See this post for more information.

Related

R markdown table

I am working on a paper in R Markdown. I'd like to create a table which contain math symbols. Below, it my simple code with a table:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r echo=FALSE}
library(knitr); library(kableExtra)
df <- data.frame(A=c("Hello!", 100, "$x^2+100$"), L=c(10, "World!", "100+250*1"))
knitr::kable(df, escape = FALSE, booktabs = TRUE,
caption = "An example[note]",
col.names = c("Left", "Right")) %>%
add_indent(c(2, 3), level_of_indent = 1.5, all_cols = TRUE) %>%
add_footnote(c("Source: Book."), notation = "symbol")
```
As a result, every time I knit the file the same error appears:
! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate
I will appreciate if very much if you help me to fix this error.
when I run your example it knits perfectly fine. this is what it looked like in HTML. I haven't seen that error before so I am not sure what the problem is. Perhaps it is a problem with your YAML header, or a line of code previous to the table that is causing problems. Hopefully someone else knows!

How to stop rmarkdown from generating the "attaching packages" when knitting to pdf

I use rmarkdown for my intro to data science class. I recently lost a lot of points for formatting on a few homework assignments.
I would like to know if there is a way of getting rid of the "attaching" packages output when knitting to .pdf?
I have attached an image of the output and some formatting issues to this post.
Either in code:
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
or in Rmarkdown format:
#+ echo = FALSE, warning = FALSE, message = FALSE
Look it up in Yihui's book, it's a good exercise.
You can also try to silence R itself:
debug.code <- FALSE # try TRUE to see the difference
options(warn = ifelse(debug.code, 1, -1), verbose = debug.code)

R, generate pretty plot by dfSummary

I have a problem using summarytools packet. There is tutorial:https://cran.r-project.org/web/packages/summarytools/vignettes/Introduction.html with pretty plots of data:
My problem is that my code generate only TEXT GRAPH. This is chunk of code in my markdown to generate plot:
```{r summary, results='markup'}
library(summarytools)
my_data <- ...
dfSummary(my_data)
```
Unfortunately it generates something like this:
How can I generate this pretty report using summarytools?
Or have you better tools for this? (generate graph, mean, std, etc.)
I found the correct syntax to generate plot:
print(dfSummary(baseline_train), method = 'render')
And the results look like this:
A little update on this:
Always use knitr chunk option results='asis', as someone pointed in an earlier comment.
It is possible to generate summaries including png graphs using print():
print(dfSummary(iris), method = "render")
Starting with version 0.9.0 (available only on GitHub as of Feb. 2019), markdown summaries will also include png graphs provided you specify the following arguments:
plain.ascii = FALSE
style = "grid"
a physical location for temporary png's (tmp.img.dir)
dfSummary(iris, plain.ascii = FALSE, style = "grid", tmp.img.dir = "/tmp")
Additionnal tips
In both cases, you will (in all likelihood) need to adjust the size of the graphs with dfSummary()'s graph.magnif parameter (try values between .75 and .85).
Exclude a column or two to avoid overly wide summaries:
dfSummary(iris, [...], varnumbers = FALSE, valid.col = FALSE)
You need to use results = 'asis' for the code chunk. Here is minimal reproducible example:
---
title: "Untitled"
output: html_document
---
```{r, results='asis'}
library(summarytools)
dfSummary(iris, plain.ascii = FALSE, style = "grid")
```
produces

Why I get different results when running code in R Markdown with knitr than in R script

I have an R code that I've written which is in a normal .r file, now I want to make a markdown html report so I'm basically running the same code, but in chunks with text in between.
I have the weirdest problem where some code works as it worked in the regular r file, but some code produces different results entirely. For example:
mydata_complete_obs %>% select(-(prom_id:end_a)) %>% select(qualified, everything()) %>%
cor(use = "complete.obs", method = "spearman") %>%
corrplot(type = "lower", method = "circle", diag = F, insig = "pch", addCoef.col = "grey",
p.mat = res1$p, title = "Spearman Correlations")
The above code which produces a corrplot does work and produces the same graph as I get in the .r file, but a simple summary() function gives me different things - the correct output being produced in the .r file and in the markdown report I get all zeroes (min, 1st quartile, median, mean, etc. - all 0!). This is the chunk for the summary():
```{r hists, echo = FALSE, warning = FALSE, message = FALSE, error =
FALSE, results="markup"}
summary(mydata_complete_obs)
```
What can be wrong? I'm loading all the libraries and read in the data from an .rds file in the first chunk, and then later use mydata_complete_obs to produce graphs and summaries. If I understand correctly I don't need to load the data for each chunk separately, because I thought that was the problem.
Ok, I have solved the problem. I had to change the query in the database so that the format of the columns will be INT instead of BIGINT (that's in Impala), then when I read it in R, everything worked and the strange behaviour disappeared.

Embeded Plots Missing when Knitting to PDF

I know this question has been asked in different formats, but when searching I couldn't find a solution or problem similar to mine.
Using Mac with RStudio (Version 1.0.153 ) R (version 3.4.3 (2017-11-30) -- "Kite-Eating Tree")
I am just trying to knit to a PDF (weave using knitr) in R markdown, but when I do, the plots are missing. When I knit to HTML the plots are there. I am using the earlywarnings package which helps create the plots. See example below using R markdown:
```{r, earlywarnings, fig.keep='all'}
library(earlywarnings)
data("foldbif")
x = foldbif
plot.ts(x)
```
That plot shows in the pdf output, but not when I create other plots like these:
```{r, echo=FALSE,results='hide',fig.keep='all', fig=TRUE}
out<-generic_ews(x,winsize=50,detrending="gaussian",
bandwidth=5,logtransform=FALSE,interpolate=FALSE)
```
or this one:
```{r, echo=FALSE, results='hide', fig.keep='all', fig=TRUE, warning=FALSE}
qda = qda_ews(x, param = NULL, winsize = 50, detrending = "gaussian",
bandwidth = NULL, boots = 100,s_level = 0.05, cutoff = 0.05,
detection.threshold = 0.002, grid.size = 50, logtransform = FALSE, interpolate = FALSE)
```
The last function also only displays 2 out of 3 plots, but I'll figure that out another time.
Any help would be appreciated. Please let me know if this was not clear.
I can confirm that the issue is related with the use of dev.new(). I copy-pasted the code of the function, removed it (the one line 136 of the function) and it worked, i.e. I get the figure on my pdf. There may be a way to call a function to avoid the effect of dev.new() that I am not aware of.

Resources