Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide bash chunk execution with terminal rmarkdown::render run #1782

Closed
splaisan opened this issue Nov 29, 2019 · 6 comments
Closed

hide bash chunk execution with terminal rmarkdown::render run #1782

splaisan opened this issue Nov 29, 2019 · 6 comments

Comments

@splaisan
Copy link

I have this small bash chunk which runs OK but outputs to the terminal when I run my Rmd from command line

R --slave -e "infile='extracted.fq.gz'; rmarkdown::render('Plot_FastqStats.Rmd', 'pdf_document', quiet=TRUE)"

How can I make it silent and just doing it job without echo'ing?

I have already set a bunch of things in the Rmd but still get the output below

opts_chunk$set(error=FALSE,
               message=FALSE, 
               warning=FALSE, 
               eval=TRUE,
               echo=FALSE,
               include=FALSE,
               results = "hide",
               fig.cap=".",
               fig.show="asis",
               fig.keep="high",
               fig.width=3,
               fig.hight=3,
               cache=FALSE,
               comment=NA,
               progress = FALSE, 
               verbose = FALSE)

my chunk of bash code

'''{bash, include=FALSE, results='hide'}

# declare a custom bash function
function getstats ()
{
if [ $# == 1 ]; then
  name=$(basename $1)
  zcat $1 | bioawk -c fastx -v varn=${name} 'BEGIN{FS="\t"; OFS="\t"; print "Name","Len","GC%","meanQ"}{print varn,length($seq),gc($seq),meanqual($qual)}' > data/counts.txt;
 else
     echo "# give a fastq.gz file as argument";
     return;
 fi
}

# apply the function to a fastq.gz input file
getstats $infile
'''
  • Example of a run with verbose output despite all settings to FALSE/hide

R --slave -e "infile='extracted.fq.gz'; rmarkdown::render('Plot_FastqStats.Rmd', 'pdf_document', quiet=TRUE)"

running: bash  -c "
# declare a custom bash function
function getstats ()
{
if [ \$# == 1 ]; then
  name=\$(basename \$1)
  zcat \$1 | bioawk -c fastx -v varn=\${name} 'BEGIN{FS=\"\\t\"; OFS=\"\\t\"; print \"Name\",\"Len\",\"GC%\",\"meanQ\"}{print varn,length(\$seq),gc(\$seq),meanqual(\$qual)}' > data/counts.txt;
 else
     echo \"# give a fastq.gz file as argument\";
     return;
 fi
}

# apply the function to a fastq.gz input file
getstats \$infile"
Parsed with column specification:
cols(
  Name = col_character(),
  Len = col_double(),
  `GC%` = col_double(),
  meanQ = col_double()
)
@cderv
Copy link
Collaborator

cderv commented Nov 29, 2019

Hi,

For such questions or issue, please next time, provide a reproducible example to work on. It is asked in the issue template you deleted.

This is more a question here than an issue I guess.

If you just want to suppress stdout and stderr in your linux terminal, you can use some linux tricks
about /dev/null

Small reprex:

---
title: "test"
output: md_document
---

```{bash message=FALSE, include=FALSE}
echo "chunk1"
```

put this in a test.Rmd then run

Rscript -e "rmarkdown::render('test.Rmd', quiet = TRUE)"

You'll have some outputs because non R engines print some messages to R output, currently by design I guess (from the source code in bloc_exec to explain what is going on.

Rscript -e "rmarkdown::render('test.Rmd', quiet = TRUE)">/dev/null 2>&1

This will print nothing in terminal. Does it work for you ?

@splaisan
Copy link
Author

Thanks a lot for the detailed answer and sorry for not providing a working example. You confirmed that the code cannot be made really quiet from inside which was my underlying question. I will suppress verbosity at bash level as you suggest. Thanks again for your time

@cderv
Copy link
Collaborator

cderv commented Nov 30, 2019

message, warning and error chunk option are there to decide if you want to keep it in rendered output with R code result or send back to console. I am not sure non R engines respect those options currently. I guess supporting quiet option for non R engine could be added but I am still in doubt if it should be done :

  • It is kind of more secure to have some infos on what is going on when non R related
  • Message can be suppress directly in console if necessary.

Will see what other think about this

@splaisan
Copy link
Author

I agree, i only had this verbosity issue with bash chunks (with all verbosity options to false). With r chunks i can control without problem. I can live with this though, I was seeking perfection. ;-)

@yihui yihui closed this as completed in 0e930ea Jan 15, 2020
@yihui
Copy link
Owner

yihui commented Jan 15, 2020

The chunk option message = FALSE works in this case now. Thanks for the report!

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants