2

I'm almost done creating my very first web app with Shiny, but I'm stuck at the very last step, where I'm rendering the plot. What I am trying to do is to reactively select the y axes in my renderPlot() function, based on an input from the ui.R .

To elaborate on the problem, my server.R looks like this:

library(shiny)
library(plyr)
library(sqldf)
library(reshape2)
library(ggplot2)
suppressWarnings(library(RODBC))



user=c(260, 2670, 3209, 7992, 8176, 10001, 10254, 11576, 17801, 17801)
var1=c(3, 2, 42, 1, 6, 4, 2, 9, 1, 4)
var2=c(65, -3680, -2457, 5200, -100, 1000, -50, 630, -124, 439)
date=c("2014-07-01", "2014-07-02", "2014-07-03", "2014-07-04", "2014-07-05", "2014-07-05", "2014-07-05", "2014-07-06", "2014-07-07", "2014-07-08")
id=c(41, 48, 41, 48, 50, 41, 48, 41, 41, 50)

data2mutate<-data.frame(user, var1, var2, date = as.Date(date), id)


#The Shiny Server starts here:

shinyServer(
  function(input, output) {
dataInput<-reactive({

  mutate(
    merge(
#First data frame in the merge function
      setNames(count(data2mutate, c('date', 'id')), c('date', 'id', 'number_users'))
#Second data frame in the merge function
      , setNames(aggregate(cbind(var1, var2) ~ date+id, data2mutate , sum), c('date', 'id', 'var1','var2'))
#By:
      , by=c('date', 'id'), all.x=T),
#Variables created with the mutate function
    var3=round(var1/number_users, 1), var4=round(var2/number_users, 1))[,c(1,3,4,6,5,7,2)] #At the end I reorder the columns so that their place will match the input number from ui.R

})

# Create the plot
output$plot1<- renderPlot({
                print(ggplot(dataInput() , aes(x=as.Date(date), y=number_users)) + 
                geom_line() + 
                labs(x = "Date", y="Number of Users")
  )

    }
)        

So, in the server.R everything works fine if I assign a given variable for my x axis:

output$plot1<- renderPlot({
                print(ggplot(dataInput() , aes(x=as.Date(date), y=number_users)) + 
                geom_line() + 
                labs(x = "Date", y="Number of Users")
  )

Nevertheless, I want to reactively select the y axes from the dataInput() based on input from the ui.R (called selectKPI). My best guess is this one:

output$plot1<- renderPlot({
                print(ggplot(dataInput() , aes(x=as.Date(date_played), y=dataInput()[input$selectKPI[1]]))) + 
                geom_line() + 
                labs(x = "Date", y=as.symbol(names(dataInput())[input$selectKPI[1]]))
  )

I can solve that by using if statements for each of the ui.R inputs, but I find this to be a bit dull + I'm curious if you could select a column from reactive function.

Nikolay Nenov
  • 547
  • 1
  • 8
  • 27
  • Possible duplicate of [How to use a variable to specify column name in ggplot](http://stackoverflow.com/questions/22309285/how-to-use-a-variable-to-specify-column-name-in-ggplot) – Chris Oct 03 '16 at 18:27
  • @Chris , you have a point - this post partially solves the problem. With the addition that in the labs(), y = paste(input$selectKPI[1]) – Nikolay Nenov Oct 05 '16 at 09:09

0 Answers0