如何在闪亮的应用程序中显示反应图,然后将其作为参数传递给降价文档?

How to display a reactive plot in a shiny app and then pass it as a parameter to a markdown document?

我的目的是使用闪亮的应用程序直观地探索一些数据,然后将生成的图表传递给 Markdown 文档。我的问题类似于我在Whosebug上找到的这两个问题。

不幸的是,我不知道如何使用提供的答案来解决我的问题。我假设我对反应值的功能了解不够。

在 MWE 中,我使用输入滑块来创建一些应显示在闪亮应用程序中的随机数。在闪亮的应用程序中创建绘图后,我需要将其嵌入到 Markdown 文档中。将绘图作为参数传递给 Markdown 不会产生错误,但是在 Markdown 文档中无法访问该参数(似乎不存在)。但是如果我在闪亮的应用程序中取消代码以直接在应用程序中显示绘图,那么绘图可以作为参数传递给 Markdown 文档并在那里显示。

我知道我可以在 Markdown 文档中重新创建情节,如 。我仍然想了解是否有解决方案可以在不重新创建情节的情况下通过该情节。

为了做到这一点,我必须理解为什么在闪亮的应用程序中显示绘图会阻止它被传递到 Markdown 文档。我不明白的是什么?请帮忙!谢谢!

MWE: server.R

library(shiny)

shinyServer(function(input, output) {

    # create a scatter plot of random numbers
    # based on value of input slicer
    chart <- reactive({
        plot(x = 1:input$dots, y =  rnorm(n = input$dots),
             xlab = "Numbers", ylab = "Random")
    })
    

    # Display the scatter plot in shiny app
    # in reaction to actionButton display
    observeEvent(eventExpr = input$display,
                 output$dotPlot <- renderPlot({
                 chart()
                  })
    )
    
    # Create R-Markdown Report with plot and
    # value of input slider as paramters
    output$report <- downloadHandler(
        filename = "Dot_Report.html",
        
        content = function(file) {
        tempReport <- file.path(tempdir(), "Dot_Report.Rmd")
        file.copy(from = "Dot_Report.Rmd", 
                  to = tempReport, 
                  overwrite = TRUE)
        
        pars <- list(n = input$dots,
                     random_plot = reactive(chart()))

        rmarkdown::render(input = tempReport, 
                          output_file = file,
                          params = pars,
                          envir = new.env(parent = globalenv()))
    })

})

MWE:ui.R

library(shiny)

# Define UI for application that displays a Scatter Plot
shinyUI(fluidPage(

    # Title
    titlePanel("Simple Plot Export"),

    # Sidebar with a slider input for number of random numbers
    sidebarLayout(
        sidebarPanel(
            
            sliderInput("dots",
                        "Number of Random dots:",
                        min = 1,
                        max = 50,
                        value = 30)
        ,
        actionButton("display", "Display Plot"),
        
        downloadButton("report", "Generate Report")
    ),
        mainPanel(
            plotOutput("dotPlot")
        )
    )
))

MWE:Dot_Report.Rmd

---
title: "Simple Scatter Plot"
output: html_document
params: 
  n: NA
  random_plot: NA
---


```{r, echo = F}

params$random_plot()

这里的问题是,底图直接在设备上绘制。

请参阅此 以了解解决方法。

一旦我们切换到例如ggplot(只需要修改server.R):

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {
  
  # create a scatter plot of random numbers
  # based on value of input slicer
  chart <- reactive({
    DF <- data.frame(x = 1:input$dots, y = rnorm(n = input$dots))
    ggplot(data = DF, aes(x = x, y = y)) + geom_point() + xlab("Numbers") + ylab("Random")
  })
  
  
  # Display the scatter plot in shiny app
  # in reaction to actionButton display
  observeEvent(eventExpr = input$display,
               output$dotPlot <- renderPlot({
                 chart()
               })
  )
  
  # Create R-Markdown Report with plot and
  # value of input slider as paramters
  output$report <- downloadHandler(
    filename = "Dot_Report.html",
    
    content = function(file) {
      tempReport <- file.path(tempdir(), "Dot_Report.Rmd")
      file.copy(from = "Dot_Report.Rmd", 
                to = tempReport, 
                overwrite = TRUE)
      
      pars <- list(n = input$dots,
                   random_plot = reactive(chart()))
      
      rmarkdown::render(input = tempReport, 
                        output_file = file,
                        params = pars,
                        envir = new.env(parent = globalenv()))
    })
  
})

编辑: 使用基数 plot()recordPlot()

server.R:

 # create a scatter plot of random numbers
  # based on value of input slicer
  chart <- reactive({
    plot(x = 1:input$dots, y =  rnorm(n = input$dots),
    xlab = "Numbers", ylab = "Random")
    recordPlot()
  })

一些 related information - 参见 ?dev.new?dev.copy

Only one device is the ‘active’ device: this is the device in which all graphics operations occur. There is a "null device" which is always open but is really a placeholder [...]