【发布时间】: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))
【问题讨论】: