I feel like this question applies to a general query about how to manipulate data within a Shiny context but my specific question relates to the usage of the lead/lag functions in dplyr
. Right now I am faced with two problems.
The first problem is defining the variables in the dataset that have been lead/lag
. I am using a dplyr
approach where I name the lagged variable yvar
. CLearly this isn't correct as R isn't able to find yvar
. So can anyone recommend a way to change/create a variable so that the result is transferred down through the shiny app and available to be called?
The second problem is specifying the number of rows to lead/lag the Y variable. It is easy enough in the ui.R file to specify an input box with lead/lag positions. But then how do I place the integer from that input box as an argument in the lead
function?
Here is the 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')
)
))
And here is the 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)
})