Original link:tecdat.cn/?p=14899 

Original source:Tuo End number according to the tribe public number

 

You have some code, data, and a good result, and now you need to talk to your collaborators (or supervisors). What should you do?

In this guide, we want to show you how to write beautiful, repeatable reports using some of the fantastic, free tools and packages now available. These tools will help you communicate scientific knowledge, and hopefully you’ll never copy and paste R output again.

First, let’s review the key components of good analysis:

  1. data
  2. Code for analyzing data
  3. Code generated diagrams
  4. Text, explain graphics and results, and describe methods.

These elements are combined in the form of reports. As scientists, we write many small and large reports. Large reports like paper are rare, but we have been working on smaller reports. These include all preliminary results written during the life of the project, weekly updates, emails with numbers, and your own notes. Traditionally, most biologists perform stages 2 and 3 in R, then launch Word or Powerpoint and copy and paste everything from Stage 4. This works, but there are some downsides to this approach:

  • Waste a lot of time, plus copy and paste sucks
  • Your interpretation is separate from the code
  • Word doesn’t provide syntax highlighting, so it’s hard to read code displayed this way,
  • Word documents cannot be tracked well under version control
  • The report cannot be regenerated without all copy and paste operations.

Thankfully, there is now a better way to write reports with the package Knitr, which it calls the universal document translator PanDoc. Now you can also combine your interpretation (stage 4) with R code (stage 2) and results (stage 3) to produce beautiful, independent, and repeatable reports. Together, these provide a powerful tool set for scientists looking to save time and conduct repeatable research.

Why use Markdown?

The goal of this Markdown is to be “as easy to read and write as possible”. In essence, it’s a simple set of formatting commands applied to plain text documents that can be easily converted into beautifully formatted HTML, PDF, or Word docs. But unlike HTML, RTF, latex, or almost any other markup text, Markdown is very readable. And because plain text is used, files are small and can be easily edited on a variety of devices.

As scientists, we not only write papers, but also notes, codes, emails, reminders, to-do lists, blog posts, etc. Not only that, BUT I’m increasingly using Markdown for note-taking and report writing.

Because it’s plain text, you can write Markdown in any program. However, the best test editors also allow you to view the code in formatted HTML format.

Markdown documents usually end with extension.md or.markdown.

RMarkdown

Markdown proved so useful that many different coding groups adopted it, but also added their own “flavor.” Here’s what we know so far:

  • The original Markdown
  • Github style Markdown,
  • PandocMarkdown, as well as
  • Multimarkdown.

Each of these provides a slightly different set of capabilities.

RStudio implements something called “RMarkdown” (or RMarkdown). In addition, it includes “code blocks” that will be run by R. The code looks like this:

```{r}
mean(1:10) # or some other code
```
Copy the code

Produce:

` ` ` scheme (1:10) # or some other code ` ` ` ` ` ` # # [1] 5.5 ` ` `Copy the code

Markdown will render like this:

mean(1:10) # or some other code
Copy the code
# # 5.5 [1]Copy the code

 

R Markdown is used in Rstudio and allows R code to be marshaled along with text segments. Files written in R Markdown have the extension.rmd.

Think of R Markdown as something that can be compiled into one of the markdown variants above.

 

knitrThe report

The KnitR package is written to combine elements of RMarkdown and R code in a single document. Let’s start with an example.

Open Rstudio and install the Knitr package

install.packages(knitr)
Copy the code

Then open the demo file and click the Knit HTML button

 

This file is written in RMarkdown and includes some text and code.

 

Click to knit HTML to do a few things

  1. It runs all the code in the file
  2. It generates a Markdown file containing the bits of the original document and its output.
  3. It converts markdown documents to HTML.

You can also document from the console using the following command:

library(knitr)
library(markdown)
knit("example.Rmd")  # produces the md file
markdownToHTML("example.md", "example.html")  # converts an md file to html
Copy the code

 

Note that for this code to work, the sample file must be in your working directory, or you need to provide the path to the RMD file:

knit("myPath/example.Rmd")  # produces the md file
Copy the code

 

Ok, so you have a document (HTML file) in which you can document your analysis. Now, just replace the sample code with some actual material, and you can use it.

Some of the benefits of this approach include:

  • Do not copy and paste
  • Your report can be easily updated once you have more data, new ideas, and so on
  • Because they are just like any other code, you can trace the Knitr script under version control.
  • If important, the code bits used to generate the results can be displayed.
  • Your analysis is completely transparent and repeatable.

People now use Knitr for a variety of purposes, for example

  • Prepare reports on their data
  • Prepare the tutorial
  • Write blog posts.

 

To avoid trouble

We recommend that you use the script in setwd(). That’s more important here. Changing the working directory in the Rmd file will cause trouble. Therefore, it is recommended that you assume that all Rmd files are running in the root of your project for it to run.

Show and hide code and output

By setting options for each code block, you can choose what to include in the virtual report.

  • echo= TRUE: If you want to display code in a report,FALSEOr to hide code, select this option.
  • results= "hide": Select this option if you want to hide the results of running code.
  • eval =FALSE: causes the current block not to be evaluated.

For more details on these options, see

  • Complete documentation on the Knitr web site.

Convert to a different document format

Now, what if you want to generate other document types instead of HTML files? Input pandoc.

According to its creators, “PanDoc is your Swiss Army knife if you need to convert files from one markup format to another. It can read a variety of inputs including Markdown, reStructuredText, HTML, LaTeX, MediaWiki tags, and DocBook XML. It can write pure text, Markdown, reStructuredText, XHTML, HTML 5, LaTeX (including projector slides), ConTeXt, RTF, DocBook XML, OpenDocument XML, ODT, Word DOCx, GNU Texinfo, MediaWiki tag, EPUB, FictionBook2, Textile, Groff man page, Emacs org-mode, AsciiDoc and Slidy, Slideous, DZSlides or S5 HTML slide shows. It can also produce PDF output on systems with LaTeX installed.” “

First, you need to download and install PanDoc. Once installed, you can use the functionality that comes with the PNadoc Knitr package to convert the generated MD files into whatever format you want. For example, to convert the above example into a Word document, we write:

library(knitr)
library(markdown)
knit("example.Rmd")  # produces the md file
pandoc("example.md", format = "docx")  # converts md file into docx
Copy the code

 

Repeatable research

So, with it, you can use a set of tools to do repeatable research in R.


reference

Most welcome insight

1. How to solve the problem of SINGULAR FIT in linear mixed model

2. Install the R software package on the UBUNTU VM

3. Run R language commands on the CLI in WINDOWS

Error: OBJECT OF TYPE ‘SYMBOL’ IS NOT SUBSETTABLE