in shiny, how to plot from sliderinput that contains adding columns based on selectinputs?

I am new to R and am trying to create Shiny app.
The app contains sliderInput to determine the investment amount.
The selectInput to determine vehicle type to invest in (car or motorcycle)
and another selectInput to pick the brand name.

My requests are:

  1. After selecting the vehicle type (car or motorcycle), automatically select the rows that contain the selected vehicle type ( for example "cars")
  2. After that, sum the values in profit column and divide each one by the total to calculate each brand name contribution in total profits.
  3. Add new column that contains percentage of each brand name
  4. Then add new column that contains multiplication of the sliderInput value to this percentage to calculate the expected profit if we invested the amount in sliderInput.
  5. Then create bar plot for each brand name with their profits

Here is the code :

    library(shiny)
    library(dplyr)
    library(plotly)

    df1<-
    data.frame (
    Vehicle_type=c("CAR","CAR","CAR","MOTORCYCLE","MOTORCYCLE","MOTORCYCLE"),Brand_Name=c("ford","Honda","Audi","SYM","Honda","Harley"),revenues=(as.integer(c("526","552","445","222","223","300"))),cost=(as.integer(c("426","427","325","172","178","235"))),profit=(as.integer(c("100","125","120","50","45","65"))),branch1=(as.integer(c("10","15","12","6","5","5"))),branch2=(as.integer(c("3","4","7","6","4","9"))))
    shinyUI(fluidPage(titlePanel("Vehicles"),
    sidebarLayout(sidebarPanel(sliderInput("bins", "investment:", min = 1000,max = 10000,value = 5000),
    selectInput("ProductId","Vehicle type:",choices=unique(df1$Vehicle_type)),
    selectInput("nameId","Vehicle Brand Name:",choices=NULL),
    selectInput("IndicatorId","Select Indicator:",choices=c("profit","cost","revenues",selected="revenues"))),
    mainPanel(plotlyOutput("Test2", height = "250px")))))

server

library(shiny)
library(dplyr)
library(plotly)

shinyServer(function(session,input, output) {

observe({
print(input$ProductId)
df2<-df1%>%filter(Vehicle_type==input$ProductId) %>% select(Brand_Name)
updateSelectInput(session,"nameId","Vehicle Brand Name:",choices=unique(df2))
IndicatorSum<- reactive({df2 %>% pull(input$IndicatorId) %>% sum()})
df3<-mutate(df2,perctcontcl=input$IndicatorId/IndicatorSum)
df4<-mutate(df3,perctcontout=perctcontcl*input$bins)
output$Test2 <- renderPlotly({
  Test2 <- plot_ly(
  df4, x = ~Brand_Name, y = ~get(input$IndicatorId), type = "bar",color=~Brand_Name)})
  })
 })

    shinyServer(function(input, output) {})

I suggest you start by running some simple tutorial examples, it seems you don't really have a basic understanding of how shiny works.

This runs, not sure what you are trying to do, maybe this will get you started. You need to save this script before you can run the app.

library(shiny)
library(dplyr)
library(plotly)

df1<-data.frame (
		Vehicle_type=c("CAR","CAR","CAR","MOTORCYCLE","MOTORCYCLE","MOTORCYCLE"),
		Brand_Name=c("ford","Honda","Audi","SYM","Honda","Harley"),
		revenues=(as.integer(c("526","552","445","222","223","300"))),
		cost=(as.integer(c("426","427","325","172","178","235"))),
		profit=(as.integer(c("100","125","120","50","45","65"))),
		branch1=(as.integer(c("10","15","12","6","5","5"))),
		branch2=(as.integer(c("3","4","7","6","4","9"))))

ui <- fluidPage(
	titlePanel("Vehicles"),
	sidebarLayout(
		sidebarPanel(
			sliderInput("bins", "investment:", min = 1000,max = 10000,value = 5000),
			selectInput("ProductId","Vehicle type:",choices=unique(df1$Vehicle_type)),
			selectInput("nameId","Vehicle Brand Name:",choices=NULL),
			selectInput("IndicatorId","Select Indicator:",choices=c("profit","cost","revenues"),selected="revenues")
			),
		mainPanel(
			plotlyOutput("Test2", height = "250px")
		)
	)
)

server <- function(input, output, session) {

	df2 <- reactiveVal() # df2 will change so must be a reactiveVal

	observeEvent(input$ProductId, {
		df2(df1 %>%
			filter(Vehicle_type==input$ProductId)) # this is how you change df2
		updateSelectInput(session, "nameId", choices=unique(df2()$Brand_Name))
	})

	output$Test2 <- renderPlotly({
		req(input$IndicatorId) # make sure there is a value of indictaorId
		print(input$IndicatorId)
		temp <- df2() # get df2
		IndicatorSum <- sum(temp[[input$IndicatorId]]) # you can reference a column like this
		temp$perctcontcl <- temp[[input$IndicatorId]] / IndicatorSum
		temp$perctcontout <- temp$perctcontcl*input$bins
		print(temp)
        # not sure what you want on the y axis
		plot_ly(temp, x = ~Brand_Name, y = ~perctcontcl, type = "bar", color=~Brand_Name)
	})
}

shinyApp(ui, server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.