根据闪亮

时间:2015-11-26 09:58:37

标签: html r shiny knitr

我有一个闪亮的应用程序,允许我的用户浏览数据集。这个想法是用户探索数据集,以及用户发现他将通过电子邮件与他的客户分享的任何有趣的事情。我事先并不知道用户会发现多少有趣的东西。因此,在每个表或图表旁边,我有一个“将此项添加到报表中”按钮,该按钮隔离当前视图并将其添加到reactiveValues列表。

现在,我想做的是以下内容:

  • 遍历reactiveValues列表中的所有项目,
    • 生成描述该项目的一些解释性文本(此文本最好应格式化为HTML / markdown,而不是代码注释)
    • 显示项目
  • 以HTML格式捕获此循环的输出
  • 以Shiny作为预览显示此HTML
  • 将此HTML写入文件

knitr 似乎与我想要的完全相反 - knitr允许我在静态文档中添加交互式闪亮组件,我想生成有光泽的HTML(也许使用knitr,I不知道)基于用户创建的静态值。

我在下面构建了一个最小的不工作示例,试图表明我想要做什么。它不起作用,仅用于演示目的。

ui = shinyUI(fluidPage(
  title = "Report generator",
  sidebarLayout(
    sidebarPanel(textInput("numberinput","Add a number", value = 5),
                 actionButton("addthischart", "Add the current chart to the report")),
    mainPanel(plotOutput("numberplot"),
              htmlOutput("report"))
  )
))

server = shinyServer(function(input, output, session){
  #ensure I can plot
  library(ggplot2)

  #make a holder for my stored data
  values = reactiveValues()
  values$Report = list()

  #generate the plot
  myplot = reactive({
    df = data.frame(x = 1:input$numberinput, y = (1:input$numberinput)^2)
    p = ggplot(df, aes(x = x, y = y)) + geom_line()
    return(p)
  })

  #display the plot
  output$numberplot = renderPlot(myplot())

  # when the user clicks a button, add the current plot to the report
  observeEvent(input$addthischart,{
    chart = isolate(myplot)
    isolate(values$Report <- c(values$Report,list(chart)))
  })

  #make the report
  myreport = eventReactive(input$addthischart,{
    reporthtml = character()
    if(length(values$Report)>0){
      for(i in 1:length(values$Report)){
        explanatorytext = tags$h3(paste(" Now please direct your attention to plot number",i,"\n"))
        chart = values$Report[[i]]()
        theplot = HTML(chart) # this does not work - this is the crux of my question - what should i do here?
        reporthtml = c(reporthtml, explanatorytext, theplot)
        # ideally, at this point, the output would be an HTML file that includes some header text, as well as a plot
        # I made this example to show what I hoped would work.  Clearly, it does not work.  I'm asking for advice on an alternative approach.
      }
    }
    return(reporthtml)
  })

  # display the report
  output$report = renderUI({
    myreport()
  })
})

runApp(list(ui = ui, server = server))

1 个答案:

答案 0 :(得分:0)

您可以使用html2canvas捕获页面的HTML,然后使用this答案将捕获的DOM部分保存为图像,这样您的客户端就可以将其嵌入到任何HTML文档中而无需担心关于页面内容的来源