1

I want to create a shiny app that plots a graph of the various data inside a table. I have created the shiny app which

  1. Tells the user to select a file to plot

  2. Loads the data file into a Data database

  3. Asks the user which column to plot

  4. Inserts various data into respective datframes

  5. Then plots the respective selected column

The code is as follows

    library(shiny)
library(ggplot2)
ui <- fluidPage(
   titlePanel("Creating a database"),
   sidebarLayout(
      sidebarPanel(
        textInput("name", "Company Name"),
        numericInput("income", "Income", value = 1),
        numericInput("expenditure", "Expenditure", value = 1),
        dateInput("date", h3("Date input"),value = Sys.Date() ,min = "0000-01-01",
                  max = Sys.Date(), format = "dd/mm/yy"),
        actionButton("Action", "Submit"),#Submit Button
        actionButton("new", "New")),
        mainPanel(
          tabsetPanel(type = "tabs",
                      tabPanel("Table", tableOutput("table")),
                      tabPanel("Download",
                               textInput("filename", "Enter Filename for download"),   #filename
                               helpText(strong("Warning: Append if want to update existing data.")),
                               downloadButton('downloadData', 'Download'), #Button to save the file
                               downloadButton('Appenddata', 'Append')),#Button to update a file )
                      tabPanel("Plot", 
                               actionButton("filechoose", "Choose File"),
                               br(),

                               selectInput("toplot", "To Plot", choices = c("Income" = "inc",
                                                                  "Expenditure" = "exp",
                                                                  "Gross Profit" = "gprofit",
                                                                  "Net Profit" = "nprofit"
                                                                  )),
                              actionButton("plotit", "PLOT"),
                              plotOutput("Plot"))
          )

      )
)
)
# Define server logic required to draw a histogram
server <- function(input, output){
  #Global variable to save the data
  Data <- data.frame()

  Results <- reactive(data.frame(input$name, input$income, input$expenditure,
                                 as.character(input$date),
                                 as.character(Sys.Date())))

  #To append the row and display in the table when the submit button is clicked
  observeEvent(input$Action,{
    Data <<- rbind(Data,Results()) #Append the row in the dataframe
    output$table <- renderTable(Data) #Display the output in the table
  })

  observeEvent(input$new, {
    Data <<- NULL
    output$table <- renderTable(Data)
  })

  observeEvent(input$filechoose, {
    Data <<- read.csv(file.choose()) #Choose file to plot
    inc <- as.numeric(Data[ ,2]) 
    exp <- as.numeric(Data[ ,3]) 
    date <- Data[,4]
    gprofit <- exp - inc
    nprofit <- (exp - inc) * 0.06
    output$table <- renderTable(Data) #Display the choosen file details
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$filename , ".csv", sep="")}, # Create the download file name
    content = function(file) {
      write.csv(Data, file,row.names = FALSE) # download data
    })

  output$Appenddata <- downloadHandler(
    filename = function() {
      paste(input$filename, ".csv", sep="")}, 
    content = function(file) {
      write.table( Data, file=file.choose(),append = T, sep=',',
                   row.names = FALSE, col.names = FALSE) # Append data in existing
    })

  observeEvent(input$plotit, {
    bab <- input$toplot
               output$Plot <- renderPlot("Plot",
                                         ggplot()+ geom_bar(data = Data, aes(x= input$toplot,
                                                            y= date)))})
}

# Run the application 
shinyApp(ui = ui, server = server)

but when i press the "plot" button, it gives the error

Error in *: non-numeric argument to binary operator`

Where i am wrong? Also i have used the as.numeric to convert the data into numeric so as to remove the error. Open for suggestions to change it also. Please Help. Thank You.

The data is like enter image description here

OzanStats
  • 2,756
  • 1
  • 13
  • 26
rahul yadav
  • 432
  • 3
  • 20
  • on your `renderPlot` part seems you need to specify it as `geom_bar(aes(x = input$toplot, y = date))` – Thor6 Jul 04 '18 at 10:31
  • Yeah i saw that error and corrected it. Still does not work. Same error – rahul yadav Jul 04 '18 at 10:37
  • What error i feel is that input$toplot does not have same class as x= in geom_bar(). Is it correct? How do i rectify it? – rahul yadav Jul 04 '18 at 10:40
  • can you provide an example of the Data you feeding in the ggplot? – Thor6 Jul 04 '18 at 10:45
  • Added in the edit @Thor6 – rahul yadav Jul 04 '18 at 10:52
  • its hard to reproduce the problem like this. When you create the `inc, exp, date` variables these are vectors outside your `Data` object. So when you call `geom_bar(data = Data,aes(x=input$toplot, y= date)))})` there is no column named as `inc` or `exp` in you data. – Thor6 Jul 04 '18 at 13:17

1 Answers1

1

Use switch case from switch() statement usage or How to use the switch statement in R functions? to aid your choice option.

library(shiny)
library(ggplot2)
ui <- fluidPage(
  titlePanel("Creating a database"),
  sidebarLayout(
    sidebarPanel(
      textInput("name", "Company Name"),
      numericInput("income", "Income", value = 1),
      numericInput("expenditure", "Expenditure", value = 1),
      dateInput("date", h3("Date input"),value = Sys.Date() ,min = "0000-01-01",
                max = Sys.Date(), format = "dd/mm/yy"),
      actionButton("Action", "Submit"),#Submit Button
      actionButton("new", "New")),

    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Table", tableOutput("table")),
                  tabPanel("Download",
                           textInput("filename", "Enter Filename for download"),   #filename
                           helpText(strong("Warning: Append if want to update existing data.")),
                           downloadButton('downloadData', 'Download'), #Button to save the file
                           downloadButton('Appenddata', 'Append')),#Button to update a file )
                  tabPanel("Plot", 
                           actionButton("filechoose", "Choose File"),
                           br(),

                           selectInput("toplot", "To Plot", choices = c("Income" = "inc",
                                                                        "Expenditure" = "exp",
                                                                        "Gross Profit" = "gprofit",
                                                                        "Net Profit" = "nprofit"
                           )),
                           actionButton("plotit", "PLOT"),
                           plotOutput("Plot")
                  )
      )

    )
  )
)
# Define server logic required to draw a histogram
server <- function(input, output){
  #Global variable to save the data
  Data <- data.frame()

  Results <- reactive(data.frame(input$name, input$income, input$expenditure,
                                 as.character(input$date),
                                 as.character(Sys.Date())))

  #To append the row and display in the table when the submit button is clicked
  observeEvent(input$Action,{
    Data <<- rbind(Data,Results()) #Append the row in the dataframe
    output$table <- renderTable(Data) #Display the output in the table
  })

  observeEvent(input$new, {
    Data <<- NULL
    output$table <- renderTable(Data)
  })

  observeEvent(input$filechoose, {
    Data <<- read.csv(file.choose()) #Choose file to plot
    output$table <- renderTable(Data) #Display the choosen file details
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$filename , ".csv", sep="")}, # Create the download file name
    content = function(file) {
      write.csv(Data, file,row.names = FALSE) # download data
    })

  output$Appenddata <- downloadHandler(
    filename = function() {
      paste(input$filename, ".csv", sep="")}, 
    content = function(file) {
      write.table( Data, file=file.choose(),append = T, sep=',',
                   row.names = FALSE, col.names = FALSE) # Append data in existing
    })

  observeEvent(input$plotit, {
    inc <- c(Data[ ,2]) 
    exp <- c(Data[ ,3]) 
    date <- c(Data[,4])
    gprofit <- c(Data[ ,3]- Data[ ,2])
    nprofit <- (exp - inc) * 0.06
    y = input$toplot

    switch(EXPR = y ,
           inc = output$Plot <- renderPlot(ggplot(data = Data, aes(x= date, y= inc))+
                                             geom_bar(stat = "identity",
                                                      fill = "blue")+xlab("Dates")+
                                             ylab("Income")),
           exp = output$Plot <- renderPlot(ggplot(data = Data, aes(x= date, y= exp))+
                                             geom_bar(stat = "identity",
                                                      fill = "blue")+xlab("Dates")+
                                             ylab("Expenditure")),
           gprofit = output$Plot <- renderPlot(ggplot(data = Data, aes(x= date, y= gprofit))+
                                                 geom_bar(stat = "identity",
                                                          fill = "blue")+xlab("Dates")+
                                                 ylab("Gross Profit")),
           nprofit =  output$Plot <- renderPlot(ggplot(data = Data, aes(x= date, y= nprofit))+
                                                  geom_bar(stat = "identity",
                                                           fill = "blue")+xlab("Dates")+
                                                  ylab("Net Profit")))})
}

# Run the application 
shinyApp(ui = ui, server = server)
mrigank shekhar
  • 544
  • 3
  • 15