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