2
votes

I recently start building shiny app but I got stuck. Please help me.Thank you in advance

I am trying to create a bar chart to show the count for different type of cash and different term. This part, the code went well.

And I also want to create the box plot to show the numeric summary for different variables selected by the user. I created a selectInput called "metric" and then create a reactive called "metric1" in server.R. and then use "metric1" as the variables I selected to create box plot in server.R.

But it keep saying "cannot find the function "metric1". I don't know why it regards "metric1" as a function? it should be a vector-name of the variable selected by the user. And if I use input$metric in ggplot to create box plot directly, it still say Error: " object 'input' not found". Why cannot find input? I have paste the code below. It is not a long code. And please help me!

library(shiny)
library(ggplot2)

cash <- read.csv("cash 042014-032015.csv")
cash$TERM <- as.numeric(cash$TERM)

shinyServer(function(input, output) {

  dataset <- reactive({cash[cash$mapped_name %in% (input$model),]})
  metric1 <- reactive({input$metric})

  output$caption <- renderText({
    input$model
  })

  output$countPlot <- renderPlot({    
    p <- ggplot(dataset(), aes(Incentive.Type, fill=factor(Incentive.Type))) + geom_bar()+ facet_grid(~TERM, margins=TRUE)+theme(axis.text.x =     element_blank(),axis.ticks=element_blank(),legend.text =     element_text(size=20))+guides(fill=guide_legend(title="Incentive     Type"),title.theme=element_text(size=30))+scale_x_discrete(limits=c("Standard","Standard+Captive","Standard+Customer","Standard+Captive+Customer","Special","Special+Captive","Special+Customer","Special+Captive+Customer"))
    print(p)    
  })

  output$summaryPlot <- renderPlot({
    p <- ggplot(dataset(),aes(factor(Incentive.Type), metric1()))+geom_boxplot()
    print(p)
  })
})

Here is the ui.R

library(shiny)
library(ggplot2)

dataset <- cash

shinyUI(

  fluidPage(    

# Give the page a title
titlePanel("Incentives by Model"),

# Generate a row with a sidebar
  sidebarPanel(
    checkboxGroupInput("model", "Select Models:", 
                choices=c("370Z","Altima","Armada","Crew","Cube","Frontier","GTR","Juke","Leaf",
                          "Maxima","Murano","NV","Other","Pathfinder","Quest","Rogue","Sentra","Titan","Versa","Xterra"),selected="Altima"),
    selectInput("metric","Please select an option below:", choices=c("Dealer Commission Amount"="DLR_COMM_AMT", "Total Monthly Payment"="TOT_MO_PMT","Original Loan Amount"="ORIG_LN_AMT", "Rate"="RATE"), 
                selected="DLR_COMM_AMT"),
    width=2  
  ),


mainPanel(
  h3(textOutput("caption", container=span)),
  plotOutput("countPlot"),  
  plotOutput("summaryPlot")

) ))

1
Maybe because you inadvertently typed metric1() in the second ggplot call :) - Mike Wise
please remember to accept the answer if it helped you. - Mike Wise

1 Answers

3
votes

Try changing metric1() in the second ggplot call to metric1. As in:

  p <- ggplot(dataset(),aes(factor(Incentive.Type), metric1))+geom_boxplot()

Actually I think you will have to use something like:

  p <- ggplot(dataset(),aes_string(factor("Incentive.Type"), "metric1"))+geom_boxplot()

In order to get it to see the value of your variable metric1 and properly interpret the use of string variables inside of ggplot.