How to hide data parsing code in R Markdown - r-markdown

I am pretty new in R markdown and while creating a report "I like to hide data parsing code"
I have tried "echo=FALSE", but it does not doing the job in this particular case.
Any help would be much appreciated.
**Data Parsing**
```{r echo=FALSE}
library(ggplot2)
library(readr)
SU1 <- read_csv("......./SU.csv")
I like to hide the below chunk in the report
Thanks

Thanks.In fact i was able to solve it by adding Message= FALSE.

Related

RMarkdown, knitr::kable shows "\begin{table}" after knitting to pdf document

One of cells in my RMarkdown document
```{r echo=FALSE}
head(data,3) %>% knitr::kable(caption = "Pierwsze 3 wiersze ze zbioru danych Lista_1.csv", digits = 2, booktabs = T)
gives weird result after knitting to pdf:
Of course there shouldn't be "\begin{table}" ,"\caption{}" and "\end{table}" parts. I use knitr::kable often and it never worked this way. Does anyone know how to fix it?
Edit: I have also noticed that all section headers (like "##Section2") below the table are centered. They shouldn't.
I've just found this question: How do I prevent kable from leaving raw latex in the final document if I include a caption in a table?
and used tip from comment (format = pandoc). It worked for me.

How to create own table in Bookdown and reference it within the text?

I am currently writing my thesis in RMarkdown using the template Oxforddown (which is ultimately based on bookdown). I have been reading the documentation but I confess I am lost. I am trying to create a table that contains an overview of the experimental conditions and items I used in my empirical study, so it is not data that I can load into R and then use the kable function on. However, I do not understand how I could generate such a table. Generating RMarkdown tables outside code chunks seems to work, but then the captions and referencing are very different than the rest of the captions used so far, which I usually set up within code chunks. Example below:
{r pilot-short7, echo=FALSE, fig.scap="Pilot 2: ....", out.width="65%", message=FALSE, fig.pos='H', fig.align = 'center'}
When I am trying to include RMarkdown tables inside a code chunks, things go wrong. What would my options be?
Any help would be very much appreciated!
I prepared a markdown template for you.
Here I made a table with flextable library.
But you can use another, which you like, f.e.: kableExtra, gt etc.
As you can see, you should put \label{tab:caption} and after refer in the text by \ref{tab:caption}.
---
title: "Hello World"
header-includes:
- \usepackage{caption}
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, include = FALSE}
library(flextable)
library(dplyr)
table2 <- flextable(mtcars[1:5, 1:6])
```
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
\begin{table}[hbtp]
\captionof{table}{Text of the caption.}
\label{tab:caption}
`r table2`
\end{table}
Table \ref{tab:caption} is the baddest table in the World

Evaluate a Rmarkdown codechunk only for certain document type (html_document vs pdf_document)

I'd like to add an interactive map (a leaflet) to my HTML document, but this can't be done in a PDF document since they are static.
I know that Rmarkdown can choose to evaluate a chunk depending on a global variable, like this:
```{r setup}
ev_cars = TRUE
```
## First chunk
```{r cars, eval=ev_cars}
summary(cars)
```
I guess my question is "does knitr set a global variable indicating whether it is knitting an html_document or a pdf_document" ?
thanks
knitr::is_latex_output() and knitr::is_html_output do that job.
ref: https://bookdown.org/yihui/rmarkdown-cookbook/latex-html.html

R markdown summary output

I would like to keep all the columns of the summary output in same line, do you have a solution?
as shown in the picture, the last column goes to a new line, how to avoid it?
Thanks a lot in advance!
Without a code example, I can't help edit the code. In your Rmarkdown initial set up chunk, put the code below
```
{r setup}
knitr::opts_chunk$set(echo = TRUE)
options(width = 1000)
```
the width = 1000 will need to be adjusted to meet your needs.
here is a link that might help you get more information

XSS concerns when using OpenCPU and knitr to print user-supplied data

I'm using openCPU and knitr to generate custom feedback after surveys. To this end, I basically let survey developers specify rmd files. In this use case, the survey developers are trusted, but the survey takers may not be.
I'm now thinking about XSS. It's not a big worry as user feedback will of course usually only be displayed to the user who entered the data on display, but of course characters like '<' will be used for non-malicious reasons and I'd like to think ahead and explore some of the trials and tribulations of freely mixing R with web apps.
Knitr and R generally were not made with untrusted users and XSS in my mind. OpenCPU rectifies many security issues with running AppArmored-R as an API, but I wonder whether a maximum-flexibility approach like mine can also be proofed.
Possible points at which one might separate trusted and untrusted markup:
Before knitting, i.e. I pass escaped user data to the rmd-file. Drawback: An oblivious survey dev might unescape it accidentally or because it's annoying in some context.
During knitting. This would be ideal, I guess, but I don't know if it's possible, especially if a survey dev could potentially alter chunk settings.
After knitting. I think it's impossible to separate trusted and untrusted markup post-hoc.
Some code to paste into OpenCPU's knitr app:
```{r}
good_userdata = 'I like brackets [].'
bad_userdata = 'some text should not be
[linked](javascript:location.href=\'http://example.com?secrets\';), <s>struck</s> or __bold__'
escape_html = highr:::escape_html
escape_md <- function(x){
x <- gsub('\\[', '\\\\[', x);
x <- gsub('_', '\\\\_', x);
x
}
good_userdata_escaped = escape_md(escape_html(good_userdata))
bad_userdata_escaped = escape_md(escape_html(bad_userdata))
```
## let's say survey devs wants to print text like this
```{r}
cat(good_userdata_escaped)
cat(bad_userdata_escaped) # doesn't know about text like this
```
## gets annoyed, does
```{r}
good_userdata_escaped <- gsub('\\\\', '', good_userdata_escaped);
bad_userdata_escaped <- gsub('\\\\', '', bad_userdata_escaped);
```
##
so that this looks nice
```{r}
cat(good_userdata_escaped)
```
## later renders the same text inline, so that is evaluated as markdown
`r good_userdata_escaped # doesn't look dangerous`
`r bad_userdata_escaped`
Edit 2
Sorry, I had provided only some HTML tags, thinking possible XSS attacks were obvious. Michel Fortin had some examples on his page.
I'm not 100% sure I understand your concern. If you're worried about XSS, you're worried about users including a javascript tag or so in the markdown right?
```{r}
userdata = '<script>alert("I am evil")</script>'
```
```{r,results='asis'}
cat(userdata)
```
You can prevent this by escaping html characters. I think there's a section on this in the markdown definition. So you would need to escape all user input, either when inserting it in your DB or when extracting it:
escape <- function(x){
x <- gsub("<", "<", x);
x <- gsub(">", ">", x);
x <- gsub("&", "&", x);
x
}
Try running the following:
```{r output}
escape <- function(x){
x <- gsub("&", "&", x);
x <- gsub("<", "<", x);
x <- gsub(">", ">", x);
x
}
```
```{r}
userdata = escape('<script>alert("I am evil")</script>')
```
```{r,results='asis'}
cat(userdata)
```
That should take care of any code injection. I'm not quite sure how the __bold__ example is a concern, because afaics this can not be used for an XSS attack as there is no scripting. But if you want to prevent users from messing with layout too, than you should escape all markdown characters I guess.