Search code examples
rshinyshinyappsselectinput

How to multiply a sliderinput with a selectinput where result is shown as a text output


New to using shinyapp and new to asking for help. Here is a sample of the df called Data1:

+--------+-------+-----+-------+--------+-------------+
|  Manu  | Model | Des | Trans |  Fuel  | costpermile |
+--------+-------+-----+-------+--------+-------------+
| ABARTH |     1 | a   | SAT5  | Diesel | 0.12        |
| ABARTH |     2 | b   | 6AT   | Petrol | 0.14        |
| ALFA   |     3 | c   | M6    | Petrol | 0.13        |
| ALFA   |     4 | d   | M6    | Petrol | 0.11        |
+--------+-------+-----+-------+--------+-------------+

I am trying to create an R shiny app where the columns are selectinputs. The user then uses a sliderinput to choose how many miles they want. I would like a result to show [costpermile] * [sliderinput value] as a text output.

So far my code gives the values for all the rows which isn't what i need. I dont know how to go about just getting the one value.

Example scenario: Person chooses from drop down menus: ABARTH, 1, a, SAT5, Diesel, 0.12 Then chooses 100 miles in the slider result I would like to appear: (0.12*100) = 12

Code so far: 

    library(openxlsx)
    library(googlesheets4)
    library(tidyverse)
    library(tidyr)
    library(plyr)
    library(dplyr)
    library(googledrive)
    library(googlesheets)
    library(shiny)

    ui <- fluidPage(
      selectInput("Select1", "Manu", unique(Data1$Manu)),
      selectInput("Select2", "Model", choices = NULL),
      selectInput("Select3", "Des", choices= NULL),
      selectInput("Select4", "Trans", choices= NULL),
      selectInput("Select5", "Feul", choices= NULL),
      selectInput("Select6", "costpermile", choices= NULL),
      sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50),
      textOutput( "total")

    )

    server <- function(input, output,session) {
      observeEvent(input$Select1,{
        updateSelectInput(session,'Select2',
                          choices=unique(Data1$Model[Data1$Manu==input$Select1]))
    }) 

    observeEvent(input$Select2,{
      updateSelectInput(session,'Select3',
                        choices=unique(Data1$Des[Data1$Manu==input$Select1 & 
                                                           Data1$Model==input$Select2]))
    }) 

    observeEvent(input$Select3,{
      updateSelectInput(session,'Select4',
                        choices=unique(Data1$Trans[Data1$Manu==input$Select1 & 
                                                            Data1$Model==input$Select2 & 
                                                            Data1$Des==input$Select3]))
    }) 

    observeEvent(input$Select4,{
      updateSelectInput(session,'Select5',
                        choices=unique(Data1$Fuel[Data1$Manu==input$Select1 & 
                                                           Data1$Model==input$Select2 & 
                                                           Data1$Des==input$Select3 & 
                                                           Data1$Trans==input$Select4]))
    }) 

    observeEvent(input$Select5,{
      updateSelectInput(session,'Select6',
                        choices=unique(Data1$costpermile[Data1$Manu==input$Select1 &
                                                           Data1$Model==input$Select2 &
                                                           Data1$Des==input$Select3 &
                                                           Data1$Trans==input$Select4 &
                                                           Data1$Fuel == input$Select5
                                                           ]))
    }) 


    output$out <- renderUI({
      if (input$Select6 == TRUE){
      sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50)
    }})


    values <- reactiveValues()

    observe({values$complete <- Data1$costpermile * input$num

    })

    output$total <- renderText({ values$complete})

    }

    shinyApp( ui= ui, server= server)

Solution

  • you need to replace the last part of your code:

    values <- reactiveValues()
    
    observe({values$complete <- Data1$costpermile * input$num})
    
    output$total <- renderText({ values$complete})
    

    With only the output$total as follow:

    output$total <- renderText({as.numeric(input$Select6) * input$num})
    

    So the full code should be:

    library(openxlsx)
    library(tidyr)
    library(plyr)
    library(dplyr)
    
    library(shiny)
    
    Data1 = data.frame(Manu = c("ABARTH", "ABARTH", "ALFA", "ALFA"),
                       Model = c(1,2,3,4),
                       Des= c("a", "b", "c", "d"),
                       Trans = c("SAT5", "6AT", "M6", "M6"),
                       Fuel = c("Diesel", "Petrol", "Petrol", "Petrol"),
                       costpermile = c(0.12, 0.14, 0.13, 0.11))
    
    ui <- fluidPage(
      selectInput("Select1", "Manu", unique(Data1$Manu)),
      selectInput("Select2", "Model", choices = NULL),
      selectInput("Select3", "Des", choices= NULL),
      selectInput("Select4", "Trans", choices= NULL),
      selectInput("Select5", "Feul", choices= NULL),
      selectInput("Select6", "costpermile", choices= NULL),
      sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50),
      textOutput( "total")
    
    )
    
    server <- function(input, output,session) {
      observeEvent(input$Select1,{
        updateSelectInput(session,'Select2',
                          choices=unique(Data1$Model[Data1$Manu==input$Select1]))
      }) 
    
      observeEvent(input$Select2,{
        updateSelectInput(session,'Select3',
                          choices=unique(Data1$Des[Data1$Manu==input$Select1 & 
                                                     Data1$Model==input$Select2]))
      }) 
    
      observeEvent(input$Select3,{
        updateSelectInput(session,'Select4',
                          choices=unique(Data1$Trans[Data1$Manu==input$Select1 & 
                                                       Data1$Model==input$Select2 & 
                                                       Data1$Des==input$Select3]))
      }) 
    
      observeEvent(input$Select4,{
        updateSelectInput(session,'Select5',
                          choices=unique(Data1$Fuel[Data1$Manu==input$Select1 & 
                                                      Data1$Model==input$Select2 & 
                                                      Data1$Des==input$Select3 & 
                                                      Data1$Trans==input$Select4]))
      }) 
    
      observeEvent(input$Select5,{
        updateSelectInput(session,'Select6',
                          choices=unique(Data1$costpermile[Data1$Manu==input$Select1 &
                                                             Data1$Model==input$Select2 &
                                                             Data1$Des==input$Select3 &
                                                             Data1$Trans==input$Select4 &
                                                             Data1$Fuel == input$Select5
                                                           ]))
      }) 
    
    
      output$out <- renderUI({
        if (input$Select6 == TRUE){
          sliderInput("num", "Choose miles", value = 100, min = 0, max = 200, step= 50)
        }})
    
    
    
      output$total <- renderText({as.numeric(input$Select6) * input$num})
    
    }
    
    shinyApp( ui= ui, server= server)