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)
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))
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.