Output formats (HTML, PDF, Word) in R Programming - Time & Space Complexity
When working with output formats like HTML, PDF, or Word in R, it's important to understand how the time to create these outputs grows as the input data gets bigger.
We want to know: How does the time to generate these files change when we have more data or more content?
Analyze the time complexity of the following R code that generates an HTML report from a data frame.
library(rmarkdown)
data <- data.frame(x = 1:1000, y = rnorm(1000))
rmarkdown::render(
input = 'report.Rmd',
output_format = 'html_document',
params = list(data = data)
)
This code renders an HTML report using a data frame with 1000 rows as input.
Look at what repeats when generating the output.
- Primary operation: Processing each row of the data to include in the report.
- How many times: Once for each row in the data (1000 times here).
As the number of rows grows, the time to generate the report grows roughly in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 units of work |
| 100 | 100 units of work |
| 1000 | 1000 units of work |
Pattern observation: The work grows directly with the number of rows. Double the rows, double the work.
Time Complexity: O(n)
This means the time to create the output grows in a straight line with the size of the input data.
[X] Wrong: "Generating an HTML or PDF report always takes the same time no matter how much data there is."
[OK] Correct: More data means more content to process and include, so the time grows with data size, not stays fixed.
Understanding how output generation time grows helps you write efficient reports and scripts, a useful skill in many real projects.
"What if the report included complex plots for each data row? How would the time complexity change?"