0

library(shiny) library(ggplot2)

report.Rmd

date: "2023-01-15" output: html_document params: x_column: "Sepal.Width" y_column: "Sepal.Length"

library(ggplot2)
ggplot(data = iris)+
      aes_string(params$x_column,params$y_column)+
      geom_point()

shiny app library(shiny) library(ggplot) library(rmarkdown)

ui <- fluidPage(

titlePanel("Iris Data"),

sidebarLayout(
    sidebarPanel(
        selectizeInput("xcol",
                    "Choose X Axis",
                    choices = names(iris)
        ),
        selectizeInput("ycol",
                       "Choose Y Axis",
                       choices = names(iris)
        ),
        downloadButton("project", "Download plot")
    ),

    mainPanel(
       plotOutput("plot")
    )
)

)

server <- function(input, output) {

filename = "report.html
output$plot <- renderPlot({
    ggplot(data=iris)+
            aes_string(x=input$xcol, y=input$ycol)+
            geom_point()
})

output$print <- downloadHandler({
      filename="report.html"
      content = function(file) {
            rmarkdown::render("report.Rmd",
                              output_file=file, params=list(x_column = input$xcol,
                                                            y_column = input$ycol))
      }
})

}

I can't get the app to render my rmarkdown file. I can't seem to see the mistake. report.Rmd is in my working directory. I can get it to work outside of shiny, but in shiny it says there is an error and the content is missing without a default value

Ken White
  • 123,280
  • 14
  • 225
  • 444
Kevin Roy
  • 3
  • 2

1 Answers1

0

There are two issues with your code. First, you used the id Project for the downloadButton but named the output print. Second, the API of downloadHandler is a bit different from reactive or renderXXX, i.e. you have to pass filename and content as arguments.

Note: Also it's good practice to copy the report file to a temporary directory before processing it and to evaluate the document in a child of the global environment. See Generating downloadable reports. As an example where missing the last step fails to render an Rmd correctly see this post. Finally, note that aes_string is deprecated since ggplot2 3.4.0. Instead it's recommended to use the .data pro-noun as I do in the code below.

library(shiny)
library(ggplot2)

ui <- fluidPage(
  titlePanel("Iris Data"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("xcol",
        "Choose X Axis",
        choices = names(iris)
      ),
      selectizeInput("ycol",
        "Choose Y Axis",
        choices = names(iris)
      ),
      downloadButton("print", "Download plot")
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output) {
  output$plot <- renderPlot({
    ggplot(data = iris) +
     aes(.data[[params$x_column]], .data[[params$y_column]]) +
      geom_point()
  })

  output$print <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      rmarkdown::render(tempReport,
        output_file = file, 
        params = list(
          x_column = input$xcol,
          y_column = input$ycol
        ),
        envir = new.env(parent = globalenv())
      )
    }
  )
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:5776
#> Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
#> ℹ Please use tidy evaluation ideoms with `aes()`

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51