I have a shiny app for plotting graph. I've added an action button that, when clicked, opens the graph in a new browser tab. The problem is, the graph only appears in the new tab if I exit the app.
I've created an html file for the graph and saved it to the temporary directory (tempdir()). I can't and don't have permission to save the html in any directory. Can anyone help me solve this problem?
Code Below - 1) r script 2) rmarkdown
R script
ui <- fluidPage(
#graph
plotOutput("plot"),
#button to show in the new tab
actionButton(inputId = "action", label = "new tab")
)
server <- function(input, output) {
output$plot <- renderPlot({
ggplot(PlantGrowth, aes(x = group, y = weight)) + geom_boxplot()
})
observeEvent(req(input$action),{
plt <- ggplot(PlantGrowth, aes(x = group, y = weight)) + geom_boxplot()
#path
tempPreview <- file.path(tempdir(), "show.Rmd")
#copy
file.copy("www/show.Rmd", tempPreview, overwrite = TRUE)
#height and width
hgt <- 7
wd <- 7
#parameters for markdown
param <- list(view = plt, height = hgt, width = wd)
rmarkdown::render(tempPreview, output_file = "show.html",
params = param, envir = new.env(parent = globalenv()))
#display the html
browseURL(paste0(tempdir(),"/show.html"))
})
}
Rmarkdown
---
title: ""
author: ""
date: ""
output: html_document
params:
view: NA
height: NA
width: NA
-
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r echo=FALSE, fig.width=params$width, fig.height=params$height}
params$view
```