Hi I'm asked to do an assignment, which requests to submit RNotebook (or Markdown) as html files for grading. This is a machine learning assignment.
What I need to know it, I'm going to use RStudio for the assignment. Can you please let me knwo is RNotebook / Markdown is another platform to use? Once the coding is done can I convert it to Markdown?
Within R studio, you can choose R markdown .
Navigate to File Option -> New File -> R Markdown
There are no changes in coding. All codes that you write in R would be as-is. Just with a flexibility that you can add comments and explain your code.
Make sure in .Rmd file, there are {} clauses. don't make any changes to it while writing code.
You will observe below chunk in .Rmd file. Make sure you should not delete {} but you may write any heading within these brackets.Followed by these brackets, you can write your code.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Related
I'm trying to embed a static image of a targets workflow in an rmarkdown document. I tried to do this by using tar_mermaid, defining a target that writes the workflow in mermaid format mm <- tar_mermaid(); writeLines(mm, "target_mermaid.js") but the help for tar_mermaid says
You can visualize the graph by copying
the text into a public online mermaid.js editor or a mermaid GitHub code chunk
I am looking for a programmatic way to either (1) embed the Javascript output in an (R)markdown file, or (2) render it (as SVG, PNG, whatever).
I thought as a shortcut that I could cut-and-paste into a markdown code chunk delimited by ```mermaid, or use cat(readLines("target_mermaid.js"), sep = "\n") in a chunk with results = "asis" but I guess that only works in Github markdown (I'm using Pandoc to render to HTML) ... ?
The visNetwork package has a visSave() function which can save to HTML (not quite what I wanted but better than what I've managed so far), and a visExport() function (which saves to PNG etc. but only by clicking in a web browser). Furthermore, targets wraps the visNetwork functions in a way that is (so far) hard for me to unravel (i.e., it doesn't return a visNetwork object, but automatically returns a widget ...)
For the time being I can go to https://mermaid.live, paste in the mermaid code, and export the PNG manually but I really want to do it programmatically (i.e. as part of my workflow, without manual steps involved).
I am not quite sure about the answer. But I have an idea. And I will delete if it is not adequate:
If you want execute mermaid code to get for example an html output then you could do this with quarto. I am not sure if this is possible with rmarkdown:
See https://quarto.org/docs/authoring/diagrams.htmlS
---
title: "Untitled"
format: html
editor: visual
---
## Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.
## Running Code
```{mermaid}
flowchart LR
A[Hard edge] --> B(Round edge)
B --> C{Decision}
C --> D[Result one]
C --> E[Result two]
```
output:
#landau's suggestion to look here almost works, if I'm willing to use Quarto instead of Rmarkdown (GH Markdown is not an option). The cat() trick was the main thing I was missing. The .qmd file below gets most of the way there but has the following (cosmetic) issues:
I don't know how to suppress the tidyverse startup messages, because targets is running the visualization code in a separate R instance that the user has (AFAIK) little control of;
the default size of the graph is ugly.
Any further advice would be welcome ...
---
title: "targets/quarto/mermaid example"
---
```{r}
suppressPackageStartupMessages(library("tidyverse"))
library("targets")
```
```{r, results = "asis", echo = FALSE}
cat(c("```{mermaid}", tar_mermaid(), "```"), sep = "\n")
```
Beginning of document:
Zooming out:
All my codes developed in base R and I don't want to use RStudio, however I want to use rmarkdown feature in base R which is available in Rstudio.
I have downloaded rmarkdown package in base r, but not able to derive a code to publish my work
All the output of my codes written in R should be view able through web browser.
First make sure you're using .Rmd as your file extension. If not, rename it to a .Rmd extension. Make sure you have Pandoc installed on your OS.
Next, add the following to the top of the file:
---
title: "Your notebook title"
output: html_document
---
output: could take any value. You can pass in the value of ioslides_presentation for example if you want but it looks like html_document fits the criteria of what you want pretty well.
Once you have that, write your code in any editor (or the R console if you prefer). Use the code chunks and markdown text formatting as you normally would:
```{r}
plot(1:10)
```
In my base R Console, this is how mynotebook.Rmd looks like:
Finally, use the render() function from rmarkdown. You can either attach it and run render():
library(rmarkdown)
render("mynotebook.Rmd")
Or, run rmarkdown::render("mynotebook.Rmd").
Notice that the use of RStudio is not required at all since Pandoc is the document converter performing this task. For the so inclined, this is what its documentation has to say:
When you run render, R Markdown feeds the .Rmd file to knitr,
which executes all of the code chunks and creates a new markdown (.md)
document which includes the code and it's output.
The markdown file generated by knitr is then processed by pandoc
which is responsible for creating the finished format.
This may sound complicated, but R Markdown makes it extremely simple
by encapsulating all of the above processing into a single render
function.
I'm creating a tutorial that involves telling the reader what to put into a file we'll call utils.R. The user would get the tutorial as an HTML file. Throughout the tutorial utils.R changes and the Rmd document uses the code in utils.R as it exists at that stage of the tutorial. During the rendering, I'd like for the code chunks to use source("utils.R") as it exists at that stage of the tutorial. I'm looking for a way to either...
1. Write the contents of a code chunk to a file. For example...
```{r utils_1}
summary(cars)
median(cars$speed)
```
Is there a way to write the code in utils_1 to a file?
2. Create a nicely formatted code chunk from a text string (I know how to write that to a file). For example...
z <- "summary(cars)\nmedian(cars$speed)"
write(z, "utils.R")
Will generate utils.R, but is there a way to turn z into a properly formatted code chunk.
I could create multiple versions of utils.R and use echo=F to hide that I'm loading that behind the scenes, but that seems like a pain.
Not sure if this is what are you looking for but you can use child option to generate them from another file. I use it for automated reports as it helps to keep the main Rmd a bit simpler
```{r child=utils.R}
```
I often place the child code in the YAML though, and call it (matter of tastes I guess...):
---
params:
utils: "utils.R"
---
```{r child=params$utils}
```
I have the following problem; There are 2 big documents, one written in R Markdown (check.Rmd ) and the other in R knitr (test.Rnw ). In the first doc we have a code like the following:
\section{Organisations Test}
\textbf{Running Organisations Checks}
<<CreateOrganisations, echo=FALSE, progress=TRUE, warning=FALSE, eval=TRUE>>=
source("OrganisationsTest.R")
OrganisationsTest(current_schema,server,user,pass)
#
and in the other as follows:
2. check the downwards shock
```{r chunk_Int_Sh_p2, echo=FALSE}
unique(param.int.shock.tab[SHOCKTYPE=="SHOCK_DOWN"&PERIODEND<21|PERIODEND==90, list( Maturity=PERIODEND, Shock_value=100*SHOCKVALUE)])
```
Now the question: how can I combine both so that I have just one script which runs and compile both one after each other. Just for clarification, I mean without any changes in both documents how can I have just one script which applyes to the first doc knit PDF to create pdf and to the other one CompilePDF ?
I suppose in Linux one can write a shell script but what a bout using RStudio in windowes?
I am really grateful for every hint I am a little bit helpless!
Addendum: In principle it is as follows: we have 2 files if you would compile a knitr file you would use bottom in RStudio, and for a Markdown file one may use bottom in RStudio, BUT we want to put both together and klick on one bottom. How is it possible?
The RStudio buttons "Compile PDF" (for RNW documents) and "Knit PDF" (for RMD documents) are convenient, but in cases like this one it is important to understand what they do in order to reproduce the same or similar behavior.
Summing the question up, it asks for a way to convert two files (a RMD and a RNW document) to PDF, preferably using a button like the two buttons mentioned above.
Unfortunately, (up to my knowledge) it is not possible to add any user-defined buttons to the RStudio GUI. But it is straightforward to write an R script that compiles both documents.
In the following I assume two files:
first.Rmd:
This is a RMD file.
```{r, echo=FALSE}
plot(1)
```
second.Rnw:
\documentclass{article}
\begin{document}
This is a RNW file.
<<>>=
plot(1)
#
\end{document}
To compile first.Rmd to PDF, we need the following (see How to convert R Markdown to PDF?):
library(knitr)
library(rmarkdown)
knit(input = "first.Rmd")
render(input = "first.md", output_format = "pdf_document")
The knit call generates first.md from first.Rmd, execting the R code in the chunks. render converts the resulting markdown file to PDF. [Note the addendum at the bottom!]
To compile first.Rnw to PDF, we can simply use knit2pdf:
knit2pdf("second.Rnw")
Copying both snippets into one R script and clicking "Source" is as close as possible to a "one-button-solution".
However, note that the snippets do something very similar to the "Compile / knit PDF" button, but it is not identical. The "Compile" buttons start a new R session while the solution above uses the current session.
Before executing the snippets make sure to use the correct working directory.
Both knit and knit2pdf by default use envir = parent.frame(). That means R code in chunks is executed in the calling enironment (see What is the difference between parent.frame() and parent.env() in R). This can be a useful feature, for example to "pass" variables to chunks, but it is important to know about it. Otherwise a document might compile just fine in one session (where certain variables exist in the calling environment) but cannot be compiled in another session (that is missing these variables). Therefore, this feature is a little bit dangerous in terms of reproducibility. As a solution, envir = new.env(parent = as.environment(2)) could be used; see knitr inherits variables from a user's environment, even with envir = new.env() for more details on that topic.
I just realized to following about render:
If the input requires knitting then knit is called prior to pandoc.
(Source: ?render)
Therefore, knit(input = "first.Rmd"); render(input = "first.md", output_format = "pdf_document") can be simplified to render(input = "first.Rmd", output_format = "pdf_document"). The envir issues of knit from above apply to render as well.
I am writing a report using r markdown. However, after this report is produced I would like to continue to analyse the data without having to open up a new editor window.
I was wondering if there is a simple command I could use to express the end of the document?
Thanks.
It appears that you are not necessarily interested in ending your markdown document but in hiding your results. The code below will enable you to continue the analysis in the same window and to exclude it from appearing in the core document when compiled.
```{r results='hide', message=FALSE, warning=FALSE}
# Stuff that you want to do
```
For a more detailed explanation, you may want to have a look at the Chunk Options in the knitr documentation.