3

I was wondering how you could turn an R script into an HTML report (From the sounds of it using knitr is the way to go). I would like to do this without using RStudio if possible.

Say I have to following R script

#Load librarys + initilisation calculations
#User shouldn't see the next 2 lines
library(foo)
set.seed(42)

#User will see the next 3 lines
print("This is some text to describe what the user is about to see")
head(mtcars)
boxplot(mtcars$mpg~mtcars$cyl)


#Not shown to user:
numbers <- runif(100)
moreNumbers <- rnorm(100)
group <- c(rep("a",100), rep("b",100))
df <- data.frame(c(numbers, moreNumbers), group)
names(df) <- c("ExampleData", "g")


#Show to user 
t.test(df$ExampleData~df$g)    

I've tried having a look around the internet and feel as if all the steps are just a little too far ahead of what I understand.

Do I need to make another R script which contains a stitch()/knit() function which calls the script above?

Thanks,

1
  • you do not need Rstudio to use knitr. See rmarkdown.rstudio.com Commented Feb 11, 2015 at 2:12

2 Answers 2

1

Call knitr::spin() on your R script. Or equivalently, click the notebook button on the RStudio toolbar.

Sign up to request clarification or add additional context in comments.

2 Comments

That's actually really close! Is there a way to suppress output from the lines of code if required?
@testuser Yes, you can use chunk options as usual, and the syntax is a little bit different, e.g. #+ results='hide', eval=TRUE Please see the link I included in the answer.
0

The answer to your question about making another script is no. What you need to do is knit("your_script.R") or whatever it is called. But first, you have to make it conform to knitr conventions. There are many sample files here which you can use to get things correct.

3 Comments

So it's basically just going to require re-formatting the "your_script.R"?
You just need to edit it. Look at the link, under demos then minimal examples. There is a header line for each code block which tells knitr what to do, display it or not, and many other options. Fixing your script above will take just a few minutes. And if you want html as your output you should name your script with a .Rmd extension so knitr automatically creates the html.
Correction: for html output use script.Rhtml. See ?knit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.