0

I am new to R and R markdown. In my R code I used a textOutput so the user can enter a title in a blank field. The title is given to a variable called 'title'. How can I display that in an R markdown script that generates pdf, html, and doc files.

Thanks

SOLUTION:

In my Rmd file I wrote this: r dfdrctitle$title and in my server.R file I used this code to get the value for the textOutput:

drctitle <- as.character(input$drc.title)
dfdrctitle <- data.frame( title = drctitle)
Maurely
  • 35
  • 2
  • 10

2 Answers2

2

You could accomplish this by parameterizing your rmarkdown report. You pass parameters into the report as a list using an option of rmarkdown::render().

First off, in the yaml header of your rmarkdown document you'd include the title parameter. You can access passed parameters into the report via r params$item which instructs knitr to evaluate that as literal r code. You need to quote it because knitr expects a string as a title in the yaml.

---
title: "`r params$rep_title`"
author: "generic_user"
---

Include other output options that you need as well (document output type, etc.). Now to render your report and pass in the parameter in a list that matches the parameter name.

library(rmarkdown)
render(path_to_my_report.rmd,
          output_dir = "path_to_mydir",
          output_file = "myreport",
          params = list(rep_title = title))
Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36
-2

Try this in the title field of your header. I believe that you can do this to output any r variable from a code chunk as text, even in a header.

---
title: `r title`
author: "your_name"
date: "11/18/2016"
output: pdf_document
---

R markdown: Accessing variable from code chunk (variable scope)

Community
  • 1
  • 1
K. Li
  • 68
  • 1
  • 12