2015-11-26 48 views
0

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

,我想要做的是以下几点:

  • 环路穿过reactiveValues列表中的所有项目,
    • 生成描述该项目的一些说明文字(本文最好格式化HTML /降价,而不是代码的注释)
    • 显示项目
  • 捕捉该环路作为H的输出TML
  • 显示这个HTML中闪亮的预览
  • 写这个的HTML文件

knitr似乎做的正是我想要的东西相反 - 在knitr让我添加交互闪亮成分在另一个静态文档中,我想根据用户创建的静态值生成光泽的HTML(可能使用knitr,我不知道)。

我已经在下面构建了一个最低限度不工作的示例,以表明我想要做什么。它不起作用,仅用于演示目的。

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)) 

回答

0

你可以使用html2canvas捕获你的网页的HTML,然后保存使用this回答DOM作为图像的拍摄部分,这样你的客户可以在任何HTML文档中嵌入这个不用担心的由来页面内容

+0

我得看看这个。我宁愿保留原始格式而不是保存为图像,以便例如用户可以将数据表复制粘贴到Excel中,但如果我无法使其工作,这是一个可行的替代方案。 – CPhil