【问题标题】:Generate an HTML report based on user interactions in shiny基于闪亮中的用户交互生成 HTML 报告
【发布时间】:2016-02-29 08:47:57
【问题描述】:

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

现在,我想做的是:

  • 循环遍历 reactiveValues 列表中的所有项目,
    • 生成一些描述该项目的解释性文本(此文本最好采用 HTML/markdown 格式,而不是代码 cmets)
    • 显示项目
  • 将此循环的输出捕获为 HTML
  • 在 Shiny 中显示此 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))

【问题讨论】:

    标签: html r shiny knitr


    【解决方案1】:

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

    【讨论】:

    • 我必须调查一下。我宁愿保留原始格式而不是保存为图像,这样,例如,用户可以将数据表复制粘贴到 Excel 中,但如果我不能让它工作,这是一个可行的替代品。跨度>
    猜你喜欢
    • 1970-01-01
    • 2020-01-18
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2021-07-31
    相关资源
    最近更新 更多