forked from TrevorFrench/R-for-Data-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p5exercises.qmd
51 lines (41 loc) · 1.18 KB
/
p5exercises.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Exercises {.unnumbered}
## Questions
::: callout
## Exercise: 18-A
Write the first seven rows of the "faithful" dataset to a csv file named "faithful.csv". Make sure you do not include any row names in your output file.
:::
::: callout
## Exercise: 18-B
Write the entire "faithful" dataset to an xlsx file using the "saveWorkbook" function. Name the tab (worksheet) that the data is on "data" and make the text in the header row bold.
:::
## Answers
::: callout
## Answer: 18-A
You can accomplish this through the use of the "write.csv" function.
``` r
write.csv(head(faithful, 7), "faithful.csv", row.names = FALSE)
```
:::
::: callout
## Answer: 18-B
The following code will allow you to accomplish this task.
``` r
library(openxlsx)
wb <- createWorkbook()
heading <- createStyle(textDecoration = "bold")
addWorksheet(wb, "data")
writeData(wb
, "data"
, faithful
, startCol = 1
, startRow = 1
, rowNames = FALSE)
addStyle(wb
, "data"
, cols = 1:length(faithful)
, rows = 1
, style = heading
, gridExpand = TRUE)
saveWorkbook(wb, "faithful.xlsx", overwrite = TRUE)
```
:::