I am writing a shiny app that I want to deploy that produces a "rmarkdown document" for a given firm "on the fly". My Rmarkdown template takes Ticker as a parameter, filters the data, produces a table using kableExtra, a table using DT, a plot using ggplot, and a plot using ggplotly. Everything works fine when I run the template. When I embed the template into a shiny app following this approach:
The kableExtra and the ggplot work, bu the DT and ggplotly are simply "not there". Can someone help? Suppose I would very much like to use both DT and ggplotly in this context.
Here is my "global.R" file:
tickers = c("AAPL","ATVI","AMZN","AON","AXP","BAC","BK","BYDDF",
"C","CE","CHTR","CVX","DEO","DVA","FND","FWONK","GL","GM","HPQ",
"ITOCF","JEF","JNJ","KHC","KO","KR","LILA","LILAK","LPX","LSXMA","LSXMK",
"MA","MCK","MCO","MDLZ","MKL","MMC","NU","OXY","PARA","PG",
"RH","SNOW","STNE","TMUS","TSM","UPS","USB","V","VRSN")
df <- data.frame(tickers = rep(tickers,each=10),year = rep(11:20,length(tickers)), returns = rnorm(10*length(tickers)) )
library(dplyr)
library(shiny)
library(kableExtra)
library(ggplot2)
library(DT)
library(plotly)
Here is my template which is an .Rmd file. I marked the start and end of the code blocks with #``` so that the whole thing would look like code in this context. To get it to run you will need to delete the #.
---
title: "Test running an Rmarkdown Report via a Shiny App"
output: html_document
runtime: shiny
params:
ticker: MCO
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, eval=TRUE, message=FALSE, warning=FALSE)
ticker007 = params$ticker
```
You have selected `r params$ticker`
print the firm
```{r}
filtered = df[df$ticker==ticker007,]
print(filtered)
```
# Test kableExtra:
```{r,fig.fig.align='center'}
kableExtra::kable(filtered) %>% kable_styling()
```
# Test DataTable:
```{r}
DT::datatable(filtered,filter = "none", options = list(pageLength=20,dom = "tip", bFilter = 0, bSort = 0, bProcessing = 0, bPaginate = 0, bInfo = 0,rownames=F),class=list(class="row-border")) %>% DT::formatStyle(names(filtered),backgroundColor = "white")
```
# Test ggplot:
```{r}
plot1 <- ggplot(filtered,aes(x=year,y=returns)) + geom_path()
plot1
```
# Test ggplotly:
```{r}
ggplotly(plot1)
```
And this is my app:
source('global.R')
# Define UI for application that draws a histogram
ui <- fluidPage(
selectInput(
'ticker', 'Select ticker',
choices = tickers ),
htmlOutput('report')
)
server <- function(input, output) {
output$report <- renderUI({
includeHTML(
rmarkdown::render(
'testTemplate.Rmd',
params = list(ticker = input$ticker)
)
)
})
}
# Run the application
shinyApp(ui = ui, server = server)