【问题标题】:Using lead/lag in a Shiny App在闪亮的应用程序中使用领先/滞后
【发布时间】:2016-03-16 20:30:16
【问题描述】:

我觉得这个问题适用于关于如何在闪亮上下文中操作数据的一般查询,但我的具体问题与dplyr 中的超前/滞后函数的使用有关。现在我面临两个问题。

第一个问题是定义数据集中的变量lead/lag。我正在使用dplyr 方法,我将滞后变量命名为yvar。显然这是不正确的,因为 R 无法找到 yvar。那么任何人都可以推荐一种更改/创建变量的方法,以便结果通过闪亮的应用程序向下传输并可供调用?

第二个问题是指定领先/落后于 Y 变量的行数。在 ui.R 文件中很容易指定一个带有超前/滞后位置的输入框。但是,如何将输入框中的整数作为参数放入 lead 函数中?

这是ui.R

library(shiny)
library(ggplot2)
library(dplyr)


dataset <- iris

shinyUI(pageWithSidebar(

  headerPanel("Iris Data Explorer"),

  sidebarPanel(


    selectInput('x', 'X', names(dataset), names(dataset)[[2]]),
    selectInput('y', 'Y', names(dataset), names(dataset)[[4]]),
    selectInput('offset', 'Offset Level of Y', 0:5, 0),
    selectInput('species', 'Species', levels(dataset$Species), "virginica")
  ),

  mainPanel(
    plotOutput('plot')
  )
))

这里是 server.R

library(shiny)
library(ggplot2)
library(dplyr)


shinyServer(function(input, output) {

  dataset <- reactive({
    iris %>%
      filter(Species==input$species) 
    #%>% mutate(yvar=lead(input$y, paste0(input$offset)))
  })



  output$plot <- renderPlot({


    dataset<-dataset()
    p <- ggplot(dataset, aes_string(x=input$x,
                                    #y=yvar)
                                    y=input$y
    )) +
      geom_point() 

    print(p)

  }, height=500)

})

【问题讨论】:

    标签: r ggplot2 shiny dplyr


    【解决方案1】:

    我想这就是你想要的。为了让它工作,我做了以下工作:

    • 放弃了mutate,因为它显然很难使用动态名称(请参阅:R - dplyr - mutate - use dynamic variable names),只是按照那里的建议使用数据框索引。
    • 将其合并到一个文件中,以便我可以一次查看所有内容(适合小东西)
    • 添加了print 语句以在每次更改时转储数据集,以便您查看正在发生的情况。
    • yvar 变量添加了引号,以便它与aesstring 一起使用
    • as.numeric 添加到偏移变量作为lead 的输入

    这是生成的代码:

    library(shiny)
    library(ggplot2)
    library(dplyr)
    
    dataset <- iris
    
    u <- shinyUI(pageWithSidebar(
      headerPanel("Iris Data Explorer"),
      sidebarPanel(
        selectInput('x', 'X', names(dataset), names(dataset)[[2]]),
        selectInput('y', 'Y', names(dataset), names(dataset)[[4]]),
        selectInput('offset', 'Offset Level of Y', 0:5, 0),
        selectInput('species', 'Species', levels(dataset$Species), "virginica")
      ),
      mainPanel(
        plotOutput('plot')
      )
    ))
    s <- shinyServer(function(input, output) {
      dataset <- reactive({
        df <- iris %>% filter(Species==input$species) 
        df[["yvar"]] <- lead(df[[input$y]],as.numeric(input$offset))
        return(df)
      })
      output$plot <- renderPlot({
        dataset<-dataset()
        print(dataset)
        ggplot(dataset, aes_string(x=input$x,y="yvar")) + geom_point(na.rm=T) 
      }, height=500)
    })
    shinyApp(ui=u,server=s)
    

    这就是它的样子:

    【讨论】:

    • 实际上使用带有动态名称的mutate 是微不足道的——只需切换到mutate_ 并提供一个带有转换的命名向量。
    • 那么那个链接(哈德利打开的)已经过时了?那么它应该怎么看呢?你的描述对我来说太简短了。
    • 没有过时,只是不完整。使用mutate_ 的OP 代码如下所示:mutate_(yvar = bquote(lead(.(as.name(y)), .(as.numeric(offset))))) — 通常,只需传递一个表达式或公式,并注入所需的名称。我本人偏爱bquote,但 dplyr 还附带了lazyeval,这使得构建这些表达式很方便(显示在链接的答案中)。
    • 谢谢。如果他们愿意,OP 可以决定使用它。有人(也许是你?)可能还需要为其他帖子添加新答案。
    • 我认为最好采用此处提供的答案。 lazyeval 可能有点混乱。就这样我理解认为input$x 不需要在aes_string 中使用引号,因为它已经是一个字符串?这是这里的格式吗?
    猜你喜欢
    • 2020-04-20
    • 2015-08-03
    • 2013-07-08
    • 2023-04-04
    • 2014-11-10
    • 1970-01-01
    • 2016-02-09
    • 2018-12-26
    • 1970-01-01
    相关资源
    最近更新 更多