使用闪亮的应用程序创建反应式数据框 [英] Creating a reactive dataframe with shiny apps

查看:86
本文介绍了使用闪亮的应用程序创建反应式数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使这种反应性返回我可以使用plotly处理的数据帧.

I am trying to get this reactive to return a data frame that I can manipulate with plotly.

avghour <- reactive({
    result <- data.frame()
    start_date <- as.numeric(unlist(input$i6[1]))
    end_date <- as.numeric(unlist(input$i6[2]))
    mkw <- maxkwpeakdates[(maxkwpeakdates >= start_date & maxkwpeakdates <= 
                             end_date) & !is.na(maxkwpeakdates), ]
    mkw <- na.omit(mkw)
    mopkw <- maxonpeakkwdates[(maxonpeakkwdates >= x & 
                                 maxonpeakkwdates <= y) & !is.na(maxonpeakkwdates), 
                              ]
    mopkw <- na.omit(mopkw)
    mkwhour <- data.frame(as.data.frame(apply(mkw, 2, hour)))
    mopkwhour <- as.data.frame(apply(mopkw, 2, hour))
    mkwhour <- as.data.frame(sapply(mkwhour, tabulate, 24))
    mopkwhour <- as.data.frame(sapply(mopkwhour, tabulate, 24))
    mkwhour <- as.data.frame(apply(mkwhour, 1, mean))
    mopkwhour <- as.data.frame(apply(mopkwhour, 1, mean))
    result <- data.frame(mkwhour, mopkwhour)
    colnames(result) <- c("1", "2")
    return(result)
  })

我希望能够绘制出结果数据框.当我调试我的应用程序时,结果表明avghour被保存为函数而不是数据框.反应式要求的值迫使反应式正确更新,但是我无法将结果保存为数据框.我是否正确使用反应式?我需要以其他方式执行此操作吗?

I want to be able to graph the resulting data frame. When I debug my app it turns out that avghour is getting saved as a function and not as a data frame. The values that the reactive require are forcing the reactive to update properly but I just cant get the result saved as a data frame. Am I using reactive correctly? Do I need to be doing this another way?

更新

I have changed my server code to be like this:

server <- function(input, output) {

  averages <- reactiveValues(hourly = avghour(input$'i6'[1], input$'i6'[2]))

  observeEvent(input$submit,{
    averages$hourly <- avghour(input$'i6'[1], input$'i6'[2])
  })

  output$"1" <- renderPlotly({
    plot_ly() %>%  
      add_trace(type = 'bar',
                data=averages$hourly,
                x=~'1',
                y=~'2')
  })

}

getHourlyAverage与我以前使用的功能相同.现在我得到error: operation not allowed without an active reactive context.如何在用户输入中使用observeEvent和reactValues?

getHourlyAverage is the same function as I had before. Now I am getting error: operation not allowed without an active reactive context. How can I use observeEvent and reactiveValues with user input?

推荐答案

我建议将数据帧存储在所谓的reactiveValues列表中.它们具有与使用reactive函数计算的事物相同的反应特性(因为当原始文档更改时,其他依赖于它们的反应将被触发),但是您可以使用它们的所有元素来进行操作.

I recommend storing the dataframes in a so-called reactiveValues list. They then have the same reactive properties as things calculated using reactive functions (in that other reactives that depend on them will be triggered when the originals change), but you can get at all of their elements to manipulate them.

通常,建议仅使用反应性函数才能解决的简单程序应该这样做,但是我确实发现,当事情变得更加复杂时,由于没有逻辑上的存储和存储空间,经常会导致死胡同.更新您的数据.

In general, it is recommended that simple programs that can get away with only using reactive functions should do so, but I do find that frequently leads to a dead end when things get more complex because there is no logical place to store and update your data.

请注意,reactiveValues是一个列表,您可以在其中存储多个内容,从而使相关内容保持在一起.该列表中的所有成员都是反应性的.

Note that the reactiveValues is a list, you can store more than one thing in there, and thus keep related things together. All of the members of that list will be reactive.

这是一个使用plotly的非常简单的示例:

Here is a very simple example using plotly:

library(plotly)
library(shiny)

ui <- shinyUI(fluidPage(
  plotlyOutput("myPlot"),
  actionButton("regen","Generate New Points")
))

server <- shinyServer(function(input, output) {

  n <- 100  
  rv <- reactiveValues(m=data.frame(x=rnorm(n),y=rnorm(n)))

  observeEvent(input$regen,{
    rv$m <- data.frame(x=rnorm(n),y=rnorm(n))
  })

  output$myPlot <- renderPlotly({
     plot_ly() %>%  add_markers(data=rv$m,x=~x,y=~y  )
  })
})
shinyApp(ui, server)

以下是有助于对其进行可视化处理的屏幕截图:

Here is a screen shot to help visualize it:

这篇关于使用闪亮的应用程序创建反应式数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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