R Markdown Shiny renderplot来自lapply的绘图列表 [英] R Markdown Shiny renderPlot list of plots from lapply

查看:211
本文介绍了R Markdown Shiny renderplot来自lapply的绘图列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将R Markdown Shiny文档开发为:

I am developing an R Markdown Shiny document to:

  1. 子集一个数据框,以包括日期"列和一些数字数据列.设置闪亮的用户输入的方式是,选择要包含的数据列的单选按钮,然后单击子集数据"按钮以创建d()-没问题:)
  2. 生成图的列表(plotList),每个数字数据列对应一个图(针对日期列进行绘制).我正在使用openairtimePlot函数生成绘图,并使用lapply生成绘图对象列表(plotList)-没问题:)
  3. 使用renderPlotplotList中的所有图输出到R Markdown文档-问题:(
  1. Subset a data frame to include the "date" column and some numeric data columns. The way the shiny user input is set up, you select radio buttons for the data columns to include, then hit the "Subset Data" button to create d() - NO PROBLEM:)
  2. Generate a list of plots (plotList), one for each numeric data column (plotted against the date column). I am using the openair package timePlot function to generate the plots, and lapply to generate the list of plot objects (plotList) - NO PROBLEM:)
  3. use renderPlot to output all the plots in plotList to the R Markdown document - PROBLEM:(

我知道也有类似的问题(例如 https://gist.github.com/wch /5436415/ R发光的反应性,带有玩具示例,然后使用闪亮的方式将图动态添加到网页中) ,请相信我,我已经尝试过(例如,使用for循环代替lapply-不是我的偏好,但是如果可以,那么谁在乎;添加local()和/或observe()等).不管我做什么,我都无法正常工作.我是R Markdown和Shiny的新手,我只是想不通-请帮忙!

I know there have been similar questions (e.g https://gist.github.com/wch/5436415/, Reactivity in R shiny with toy example, and dynamically add plots to web page using shiny), and please believe me I have tried and tried (e.g. using a for loop in stead of lapply-not my preference, but if it worked then who cares; adding local() and/or observe(); etc). No matter what I do I can't get it to to work. I am new to R Markdown and to Shiny, I just can't figure this out - please help!

这是一个可复制的示例(作为R markdown闪亮文档运行).

Here is a reproducible example (to be run as an R markdown shiny document).

首先创建创建反应性数据集的块d():

First the chunk that creates a reactive dataset d():

```{r reactive-dataset, echo=FALSE,message=FALSE}
library(openair)
library(dplyr)
data<-mydata[1:50,]
print(tbl_df(data))

inputPanel(
  checkboxGroupInput(inputId="p",
                label="select pollutants to plot",
                choices=names(data)[-1]
              ),
  actionButton(inputId="import",
               label="Subset Data")
)


d<-eventReactive(input$import,{
  d<-data %>% select(date,one_of(input$p))
  })

renderPrint({tbl_df(d())})
```

现在,第二个块创建plotList并输出(不起作用的部分):

Now the second chunk, to create plotList and output it (PART THAT DOESN'T WORK):

尝试1:仅显示最后一个情节

Attempt 1: only last plot is displayed

 ```{r plot,echo=FALSE,message=FALSE}
 renderPlot({
  i<-names(d())[-1]
  tp<-function(x){
    p<-timePlot(d(),
         pollutant=print(x),
         main="Minute Validation",
         ylab="Minute Conc. (ug/m3 or ppb)",
         key=T)
    p$plot
    }
  lapply(i,tp)

  }) 
  ``` 

尝试2(基于 R发光的反应性和玩具示例).没有显示图

Attempt 2 (based on Reactivity in R shiny with toy example). No plots are displayed

```{r plot,echo=FALSE,message=FALSE}
plotList<-reactive({
  i<-names(d())[-1]
  tp<-function(x){
    p<-timePlot(d(),
         pollutant=print(x),
         main="Minute Validation",
         ylab="Minute Conc. (ug/m3 or ppb)",
         key=T)
    p$plot
    }
  lapply(i,tp)
  })


observe({
  for (j in 1:length(plotList())){
    local({
      my_j<-j
      renderPlot({plotList()[[my_j]]})

      })#end local
    } #end for loop
}) #end observe
```

我无休止地摆弄着这个,指的是我上面链接到的类似问题.

I have fiddled with this endlessly, referring the to similar questions that I have linked to above.

推荐答案

[新答案]

我终于解决了这个问题.关键是要完全遵循帖子第三个链接中的示例,首先使用renderUI!

I finally got this worked out. The key is to exactly follow the example in the third link of your post, using renderUI first!

```{r plot,echo=FALSE,message=FALSE}
tp_list <- reactive({
  i<-names(d())[-1]
  tp<-function(x){
    p<-timePlot(d(),
         pollutant=print(x),
         main="Minute Validation",
         ylab="Minute Conc. (ug/m3 or ppb)",
         key=T)
    p$plot
  }
  lapply(i, tp)
}) 

renderUI({
    plot_output_list <- lapply(1:length(tp_list()), function(i) {
        plotname <- paste("plot", i, sep="")
        plotOutput(plotname)
    })
    do.call(tagList, plot_output_list)
})

observe({
for (i in 1:length(tp_list())) {
    local({
        my_i <- i
        plotname <- paste("plot", my_i, sep="")
        output[[plotname]] <- renderPlot({
            tp_list()[[my_i]]
        })
    })
}
})
```

[基于格面板的原始答案]

[Original answer based on lattice panels]

这并不是您想要的,但是我将所有图显示在一个图中.

This is not exactly what you want, but I got all the plots displayed in one plot.

```{r plot,echo=FALSE,message=FALSE}
renderPlot({
  i<-names(d())[-1]
  tp<-function(x){
    p<-timePlot(d(),
         pollutant=print(x),
         main="Minute Validation",
         ylab="Minute Conc. (ug/m3 or ppb)",
         key=T)
    p$plot
  }
  tp(i)
}) 
``` 

这篇关于R Markdown Shiny renderplot来自lapply的绘图列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆