SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

Must-read for university students! RStudio's R Markdown is recommended for writing reports

Hello, university students!

When you are asked to submit a report, have you ever worried about “How can I create an efficient, clean, and persuasive report?”?

That is why I would like to introduce you to a feature of the standard data analysis tool “RStudio” called “R Markdown”!

“R Markdown” is a powerful tool that allows you to combine text, code, and graphs into a single document to easily create professional reports.

If you are already using R, please take this opportunity to utilize R Markdown. If you have never used R before, don't worry! R is a surprisingly simple and easy-to-learn language, so let's give it a try!

If you are using RStudio for the first time, let's start by installing R and RStudio from the link below.

  • To install R and RStudio, click here

Let's make the most of R Markdown and aim to create high-quality reports!

Please also take a look at this article.





What is good about R Markdown?


The biggest advantages of R Markdown are that you can
combine text, code, and graphs into a single file and that
mathematical formulas and graphs are displayed beautifully. Let's look at how this is useful in actual report writing.

For example, imagine you are submitting the “results and discussion of a t-test” for a statistics report. With the conventional method, you needed to perform
tedious tasks like analyzing data in Excel, creating graphs, copying and pasting them into Word, and then attaching the analysis code separately. Furthermore, you might later find yourself wondering,
“Wait, where did the data for this graph come from?”
.

With R Markdown,
everything from loading data to running t-tests, creating graphs, and writing discussions is completed in one file!
Even if a professor asks you to “explain how this data was processed,” you can respond immediately because the code and explanation are integrated. The same applies to data analysis for psychology experiments or regression analysis in economics.

Also, the benefit of beautiful mathematical expression proves its true value in reports on differential equations or linear algebra. Complex formulas that take time to write by hand are displayed beautifully if you input them in LaTeX format.

For example, when writing the formula for a Fourier transform:

$$F(\omega) = \int_{-\infty}^{\infty} f(t) e^{-i\omega t} dt$$

It can be expressed beautifully like this!

Furthermore, even in a panic state where you realize a calculation error 10 minutes before submitting your report, R Markdown allows you to simply correct the code, and the graphs and numerical results will be updated automatically.
With the Word + Excel combination, you have to go through the trouble of correcting, recalculating, copying, and pasting.

Additionally, as the time for job hunting or your graduation thesis approaches, there may be times when you want to use an analysis method you used in a past report again.
With an R Markdown file, since the code and explanations are integrated, you can immediately recall why you performed this analysis and how you executed it, even months later.



How to launch R Markdown


Let's launch R Markdown with the following steps:

1 Click the + icon in the top-left source pane and select R Markdown from the menu.

2 Enter the document title and Author.
(You don't have to enter anything; leaving it as Untitled is fine.)
You can also choose the default document type (HTML, PDF, Word).
HTML is recommended for beginners.

3. Press OK.

With just this, a new R Markdown file is created!

Since sample code is written from the start, let's check what it's like.

The title and Author entered above are reflected in the red box ❶.
Also, the default script contains a brief explanation of R Markdown ❷ and an example of how to write code ❸.

Default R Markdown script



How to write text in R Markdown

You can just enter text normally! Since you can use Markdown syntax, you can set formatting as follows:

# 大見出し
## 中見出し
### 小見出し

*イタリック体* や **太字** も簡単!

- 箇条書き1
- 箇条書き2
  - ネストした箇条書き




How to write code in R Markdown


The biggest feature of R Markdown is that you can embed and execute R code within the document.
R chunkYou write code using a block called an.
For example, like this.

​```{r}
# ここにRコードを書く
x <- c(1, 2, 3, 4, 5)
mean(x)  # 平均値を計算
​```

The point is to enclose the front and back with ```
and insert {r}.

Although the code will run as described above,
as an option,
you can assign labels to R chunks or
configure settings such as whether to display the code itself in the document and so on.

Let's look at this in detail.

The following is the code included in the default script.
It is code that visualizes the 'pressure' dataset using the plot function.

```{r pressure, echo=FALSE}
plot(pressure)
```

The part after 'r' contains the options.

{r chunk label chunk options}

The chunk label is the name of this R chunk.
In the example above, 'pressure' is the chunk label.

Chunk options are settings for the R chunk.
For example, if you set echo=TRUE, the code will be displayed in the document.
Conversely, if you set echo=FALSE, the code will not be displayed in the document.
In the example above, it is set to echo=FALSE, so the code is not displayed.


Settings that apply to the entire document

While you can configure R chunk settings for each chunk individually, you can also set common settings for the entire document.

The following is the 'setup chunk' placed at the beginning of an R Markdown file.
In this, knitr:: is used to set common options for all chunks.

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE)
```

Here, because echo = TRUE is specified, the R code for subsequent chunks will be displayed in the document.
Note that with the include = FALSE option, neither the code nor the results of this chunk itself will be displayed in the final document (though it will still be executed).



4. Output

Displaying the preview

Clicking the 'Knit' button (the yarn ball icon) at the top will convert the document and display a preview.

Pressing the Knit button

If you have not saved the file, you will be prompted to enter a filename and save location.

Enter the name and save location

With the default document, it will be output as follows.

Preview screen


Select a file format and output

You can select and output HTML, PDF, or Word file formats from the ▼ button next to Knit.



What did you think?

R Markdown is a hidden, useful feature of RStudio.
It may take a little getting used to at first, but once you master it, your report writing efficiency will improve dramatically.
When you take on your next report assignment, please give R Markdown a try! I hope this makes your university life a little easier!

いいなと思ったら応援しよう!

Non よろしければサポートお願いします! いただいたサポートはクリエイターとしての活動費に使わせていただきます!