0
votes

I'm trying to make a plot with reactive data from the server. Unfortunately I can't get the plot to work. I'm getting an error like: "Error:EXPR must be a length 1 vector". I tried different styles of plots and different libraries: Quantmod, ggplot, so on. Any suggestions?

Server:
library(shiny)

Dat<-read.csv("A:\\home\\Documents\\Franchise_Failureby_Brand2011.csv", sep=';')
names(Dat)[1]<-paste("Brand")
names(Dat)[2]<-paste("Failure")
names(Dat)[3]<-paste("Disbursement")
names(Dat)[4]<-paste("Disb$X$1000")
names(Dat)[5]<-paste("Chgoff")
Dat1<-Dat[is.na(Dat)==FALSE,]
Dat<-Dat1[1:578,]

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  DatSv <- reactive({
    Value <- switch(input$Value,
                    "Failure"= Dat$Failure[1:10],
                    "Disbursement"=Dat$Disbursement[1:10],
                    "Disb$X$1000"=Dat$`Disb$X$1000`[1:10],
                    "Chgoff"=Dat$Chgoff[1:10])
    Brand<-Dat$Brand[1:10]
    Brand(input$Value)
  })

# Generate plot 
  output$plot1 <- renderPlot({
    library("quantmod")
    hist(DatSv(), 
         main=paste('r', Value, '(', Brand, ')', sep=''))
  })

  # Generate summary of data

  output$summary<-renderPrint({
    summary(Dat)
  }) 

})


UI:
library(shiny)
shinyUI(fluidPage(
  titlePanel("Plot Franchise Failure"),
  sidebarLayout(
    sidebarPanel(
      radioButtons("n", "Chose output Y Axis:",
                   c("Failure" ,
                     "Disbursement",
                     "Disb$X$1000" ,
                     "Chgoff" )),

      checkboxInput("show_xlab", "Show/Hide X Axis Label", value=TRUE),
      checkboxInput("show_ylab", "Show/Hide Y Axis Label", value=TRUE),
      checkboxInput("show_title", "Show/Hide Title")
    ),
  mainPanel(
    tabsetPanel(
      type = "tabs",
      tabPanel("Plot", plotOutput("plot1")),
      tabPanel("Summary", verbatimTextOutput("summary"))
  )
  )
)
)
)
1
Your problem probably commes from the switch statment. try adding req(input$Value) before the switch. - Bertil Baron

1 Answers

1
votes

Hi the problem comes from connecting the inputs in the UI with the server. In the UI you have given the inputid = "n" for the radioButtons. That means we can get the Value of the Radiobuttons with input$n and not input$Value. The later is always NULL since there is no input with inputid = "Value". I had some other small problems with your code but here is a working version of the server code. I didn't modify the UI

library(shiny)

Dat<-read.csv("A:\\home\\Documents\\Franchise_Failureby_Brand2011.csv", sep=';')
names(Dat)[1]<-paste("Brand")
names(Dat)[2]<-paste("Failure")
names(Dat)[3]<-paste("Disbursement")
names(Dat)[4]<-paste("Disb$X$1000")
names(Dat)[5]<-paste("Chgoff")
Dat1<-Dat[is.na(Dat)==FALSE,]
Dat<-Dat1[1:578,]

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  DatSv <- reactive({
    switch(input$n,
          "Failure"= gsub("%","",as.character(    Dat$Failure)),
          "Disbursement"=Dat$Disbursement,
          "Disb$X$1000"=gsub("\\$","",as.character(    Dat$`Disb$X$1000`)),
          "Chgoff"=gsub("%","",as.character(Dat$Chgoff)))

  })

  # Generate plot 
  output$plot1 <- renderPlot({
    library("quantmod")
    hist(as.numeric(DatSv()),
         main=paste('Histogram of ',input$n, sep=''),
         xlab = input$n)
  })

  # Generate summary of data

  output$summary<-renderPrint({
    summary(Dat)
  }) 

})