Show output but hide code when sending Rmd to other people? - output

I have a problem with Rmarkdown when teaching. How can I send file .Rmd to other people without code but output remains?
In python (jupyter notebook, gg collab), I write code and run. After that, I delete the code and send the notebook to other people. They can't see code, but they know the output. Their mission is to write code to have the results as mine.
Is there any platform in R for designing exercises (hide code but show output)?
Thank you for all your advice.

There are a few things you can do with Rstudio and Rmarkdown, I will be creating HTML documents using Rmarkdown and Rstudio.
First, you can specify each R chunk to hide the code, this can be done with some chunk options, or easily clicking the preferred option. This is where you can see there is an option to "show output only", no matter the code inside the R chunk.
Second, you can use the below YAML settings that allow you to render a single HTML document, and allow the end user to select if they want to see the R chunk code, or hide it, while still showing the output.
---
title: "Untitled"
author: "author"
date: "9/25/2021"
output:
html_document:
code_folding: hide
---

Related

Convert markdown table of contents to HTML

I am trying to convert a markdown document to HTML, using pandoc. I cannot get the HTML output to create the table of contents correctly.
Issue:
I have added a table of contents to the markdown doc, where clicking on each header takes the reader to the relevant section. I am using the format below, where clicking on 'Header Title' will send the reader to the section 'header' in the document:
[Header Title](#header)
I tried to convert this to HTML using the pandoc command
pandoc -i input.md -f markdown -t html -o input.html
This creates a valid HTML file I can open in Firefox, and the items in the table of contents show up as links - but when I click them, nothing happens (I am expecting it to jump to the relevant section)
This happens when I use either markdown or markdown_github as the input format (-i in pandoc)
Question:
How can I get the table of contents to show the expected behavior in HTML?
Or is the concept of 'table of contents' a wrong approach to HTML, and I should change my markdown code?
Apologies if I am going about this the wrong way, I have no experience with HTML / web documents.
I found a couple of similar questions but they seemed to be specific to other programming languages / tools, so any help how I can achieve this with markdown / pandoc is much appreciated.
I am using pandoc 1.19.2.4 on Ubuntu.
Example markdown:
- [Chapter 1](#chapter-1)
- [1. Reading a text file](#1-reading-a-text-file)
## Chapter 1
This post focuses on standard text processing tasks such as reading files and processing text.
### 1. Reading a text file
Reading a file.
Looking at your markdown file, you have used #1-reading-a-text-file as the id for the 1st subheading.
While converting it to HTML, the following line is generated for the subheading:
<h3 id="reading-a-text-file">1. Reading a text file</h3>
The problem is the mismatch of "#1" which is present in the table of contents, but not in the heading.
My guess is that pandoc does not allow HTML id to start with a number.
Changing the table of contents to the following should work:
- [Chapter 1](#chapter-1)
- [1. Reading a text file](#reading-a-text-file)

Markdown HTML output

I am doing the knitting of my code to produce a html file. I got it but next to the graphs it also display some boxes with information about i.e. line, color, text and so on. Is it possible not to see that information in my html file?
add echo=FALSE to the r chunk. It will remove the evaluated code inside the chunk. Also you can add message=FALSE, and warning=FALSE to not show messages and warnings.
Example (It will plot the chart and not show the code, warnings and messages).
```{r, echo=FALSE,warning=FALSE,message=FALSE}
plot(iris)
```

Combining HTML CSS with Rmarkdown

On my way to becoming a datajournalist, I have come across lots of fantastic tools, such as R and Rmarkdown. Now that I work for a proper newspaper, I realize that most of the time, I end up using MS Excel (which, I don't deny, is also a great tool) and a concatenate function that I paste in an HTML file. I am losing quite a lot of my R knowledge and practice, and that's sad.
I know that I will soon be faced with a task that I would like to use as a way to get back to R and Rmarkdown.
I have some html pages working just fine and I'd like to change the data that is presented in them using Rmd.
My question is : is there a package or a way to combine into one Rmarkdown file most of an HTML page (header, css and javascript files and analytics, footer), and some Rmarkdown chunks ?
Thanks a lot for your answer.
Maybe you could try something like this (http://rmarkdown.rstudio.com/html_document_format.html#includes):
---
title: "Habits"
output:
html_document:
includes:
in_header: header.html
before_body: doc_prefix.html
after_body: doc_suffix.html
---
This would allow the combination of html and Rmd in general. Furthermore there is the chunk option child, which would allow to use Rmd files as chunk content?
You can use .css files to style your Rmakdown. In the header you do something like:
---
title: "Habits"
output:
html_document:
css: styles.css
---
see this

How to incorporate html table in Rmarkdown

I used package 'sjPlot' to run some data analyses. For instant, function 'sjt.df' gives me a html table regarding simple description of variables. Then I created a Rmarkdown (see below). But when I clicked knit HTML/pdf, the result of the html table did not incorporate into the Rmarkdown. Rather, it popped up in my browser. How can I deal with that?
{r}
library(sjPlot)
data(iris)
sjt.df(iris)
See my tutorial basics of sjt-functions, section Knitr integration of HTML tables.
If you just want to have the table, use 'r sjt.df(iris, no.output=TRUE)$knitr' - (note that the ' have to be `).
The no.output=TRUE ensures that the table is not displayed in the viewer pane or browser, and the $knitr parameter contains the HTML-snippet that will be incorporated in RMarkdown.
If you also want to display the R-code, use
```{r eval=FALSE}
sjt.df(iris)
```
However, I guess that these tables are not converted well to PDF, only to HTML.

Making nicely formatted tables in Markdown: knitr not compiling stargazer>html table

I'm trying to embed an output table, nicely formatted by the stargazer function, in a markdown document.
I tried embedding the stargazer() in an R code block in the Rmd document:
```{r}
library(stargazer)
stargazer(cor(attitude), type="html")
```
The code runs correctly, but it outputs the html code that in turn is not parsed by knitr, so the actual table html 'source' code is shown in the rendered document.
I'm aware of this similar question (here). I'm asking a separate question because most answers there indicate that stargazer does not support html output, which is no longer true. Html support was probably incorporated into stargazer after that tread (and I donĀ“t have engouth rep to reopen or post comments there).
This seems related to a more simple problem of making knitr compile the html table source code.
EDIT: #hrbrmstr gave me the answer in the comment bellow, which is:
```{r results='asis'}
library(stargazer)
stargazer(cor(attitude), type="html")
```
Remove the ' from 'asis'
ex: {r results=asis}