using output of shiny in markdown - r

I’m working in a R notebook and I’d like to add some interactivity using shiny.
So, I need that the user provide some text in an input field. Then, this text will used in different R chunks in the notebook. However, I wasn’t able to find the best way to do this. Here is an example of what I’m trying to do:
---
title: "R Notebook"
output: html_document
runtime: shiny
---
```{r}
data <- reactive({
as.character(input$my_mol)
})
```
```{r}
textInput("my_mol", "Please provide an ID", "MyID_1")
```
```{r}
renderPrint(data())
# is there any way to get the text output in a variable and using it in a different R chunk?
#something like:
#my_output <- renderPrint(data())
```
```{r}
# Then, I'd like to using it on this R chunk
#pba3 <- fromJSON(paste0(baseur_sim, url_encode(my_output), ".json?limit=50"), flatten=TRUE)
```
Thanks!

Related

Using webshot and robervable

I would like to include screenshots from an observable notebook in an rmarkdown rendered to pdf. Directly including code blocks that call robservable does not work, so I thought I could use the webshot package instead. The example below creates a file, test.html, that does contain the interactive notebook. However, the rendered pdf still does not show any screenshot.
---
title: "robservable and webshot"
output: pdf_document
---
```{r}
knitr::opts_chunk$set(screenshot.opts = list(delay = 5))
library("robservable")
library("webshot")
library("htmlwidgets")
library("dplyr")
```
This is a test.
```{r}
f <- "test.html"
robservable("#d3/horizontal-bar-chart", include = "chart") %>%
saveWidget(f)
```
```{r}
webshot(f)
```

How can I print the function description of an R package to Rmarkdown instead of terminal?

Using R markdown and trying to write a small document for the group I'm working in. I wanted to be able to print a functions description directly into the Rmarkdown document I'm writing. For example if I wanted the description for the function "cor" I would use
?cor
And this prints the description in the terminal I'm working on. Now using
print(?cor)
Or
dummy <- ?cor
print(dummy)
doesn't print the description to the Rmarkdown file, but instead again prints in inside the terminal and hangs, until the user exits at which point the RMarkdown pdf is generated. Unfortunately there is no sign of it within the pdf.
How can I make R print this to Rmardkdown and not the terminal?
What you need is the printr package. Make sure you install it before using the example Rmarkdown code below (i.e. run install.packages('printr') first), but here's an example of how to include a help file in an Rmarkdown created PDF:
---
title: "SO"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Printing Help
```{r}
library(printr)
help(sum)
```

knitr and knit_print with HTML output - dispatch not working

I have a shiny app (radiant.data) that uses knitr to generate reports viewable inside the application (R > Report). Because the output is displayed inside a shiny app I need a render function for something like a DT table to be displayed (i.e., convert to shiny.render.function). This all works fine.
Now, however, I want to use a custom print method to handle rendering so I can just use DT::datatable(mtcars) with knitr and knit_print.datatables to generate a shiny.render.function.
Below some example R-markdown that includes the knit_print.datatables function i'm using. chunk1 and chunk2 show a shiny.render.function as intended but the chunk6 shows nothing. If screenshot.force = TRUE I get a screenshot from chunk6 but that is not what I want either.
Example R-markdown to be processed with knitr::knit2html
```{r chunk1}
knitr::opts_chunk$set(screenshot.force = FALSE)
DT::renderDataTable(DT::datatable(mtcars))
```
```{r chunk2}
knit_print.datatables <- function(object, ...) {
DT::renderDataTable(object)
}
```
```{r chunk3}
knit_print <- knitr::knit_print
knit_print.datatables(DT::datatable(mtcars))
```
```{r chunk4}
getS3method("knit_print", "datatables")
```
```{r chunk5}
class(DT::datatable(mtcars))
```
```{r chunk6}
DT::datatable(mtcars)
```
I realize the R-markdown above, although reproducible if you have knitr and DT installed, looks a bit weird but when I export the knit_print.datatables function properly I get the same result in my application (see example output below).

Excluding part of the R markdown (html_notebook) from the final html output

I am writing a relatively long report using R Notebook that makes use of R markdown language which combines text and code in the same document and generates html output.
I would like to be able to exclude some of the analysis (both text and R code) from showing in the final HTML. This is very useful if I want to create two versions of the report - a full/detailed version, as well as a shorter version with main graphs and conclusions.
Obviously, I can create separate Rmd file for each type of report (or comment out pieces of the report that need to be excluded for the shorter version), but I was wondering if there is a more elegant way to do it.
Something like this:
if (Version == "full_text"){
Full analysis goes here
```{r}
R code goes here (could be multiple chunks)
```
}
else {
The shorter version goes here
```{r}
R code goes here
```
}
Place the "detailed" part of the report in a knitr child document which you call optionally from the main document.
Detailed content can then be switched on by calling the child document and it can be switched off by setting the variable child_docs to NULL. For example here are a main and a child document below.
Child document
Save this document under knitr-child.Rmd
---
title: "knitr child"
output: html_document
---
# Details from the child document
Hi, there. I'm a child with a plot and as many details as necessary.
```{r test-child}
plot(cars)
```
Main document - full/detailed version
---
title: "Report"
output: html_document
---
# Summary
```{r setup}
child_docs <- c('knitr-child.Rmd')
# child_docs <- NULL
```
```{r test-main, child = child_docs}
```
# Conclusion
Main document shorter version
---
title: "Report"
output: html_document
---
# Summary
```{r setup}
# child_docs <- c('knitr-child.Rmd')
child_docs <- NULL
```
```{r test-main, child = child_docs}
```
# Conclusion

Loop in R markdown

I have an R markdown document like this:
The following graph shows a histogram of variable x:
```{r}
hist(x)
```
I want to introduce a loop, so I can do the same thing for multiple variables. Something hypothetically like this:
for i in length(somelist) {
output paste("The following graph shows a histogram of somelist[[" , i, "]]")
```{r}
hist(somelist[[i]])
```
Is that even possible?
PS: The greater plan is to create a program that would go over a data frame and automatically generates appropriate summaries for each column (e.g. histogram, tables, box plots, etc). The program then can be used to automatically generate a markdown document that contains the exploratory analysis you would do when seeing a data for the first data.
Could that be what you want?
---
title: "Untitled"
author: "Author"
output: html_document
---
```{r, results='asis'}
for (i in 1:2){
cat('\n')
cat("#This is a heading for ", i, "\n")
hist(cars[,i])
cat('\n')
}
```
This answer was more or less stolen from here.
As already mentioned, any loop needs to be in a code chunk. It might be easier to to give the histogram a title rather than add a line of text as a header for each one.
```{r}
for i in length(somelist) {
title <- paste("The following graph shows a histogram of", somelist[[ i ]])
hist(somelist[[i]], main=title)
}
```
However, if you would like to create multiple reports then check out this thread.
Which also has a link to this example.
It seems when the render call is made from within a script, the environmental variables can be passed to the Rmd file.
So an alternative might be to have your R script:
for i in length(somelist) {
rmarkdown::render('./hist_.Rmd', # file 2
output_file = paste("hist", i, ".html", sep=''),
output_dir = './outputs/')
}
And then your Rmd chunk would look like:
```{r}
hist(i)
```
Disclaimer: I haven't tested this.

Resources