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:
After selecting the vehicle type (car or motorcycle), automatically select the rows that contain the selected vehicle type ( for example "cars")
After that, sum the values in profit column and divide each one by the total to calculate each brand name contribution in total profits.
Add new column that contains percentage of each brand name
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.
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) {})
Here a working example based on your idea with a few fixes and a little help from dplyr NSE
#Add stringsAsFactors = FALSE to data.frame to solve ghost factor issue downstream in 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"))), stringsAsFactors = FALSE)
shinyServer(function(session,input, output) {
observe({
print(input$ProductId)
#df2<-df1%>%filter(Vehicle_type==input$ProductId) %>% select(Brand_Name)
df2<-df1%>%filter(Vehicle_type==input$ProductId)
#updateSelectInput(session,"nameId","Vehicle Brand Name:",choices=unique(df2))
updateSelectInput(session,"nameId","Vehicle Brand Name:",choices=unique(df2$Brand_Name))
print(input$IndicatorId)
print(df2)
#IndicatorSum<- reactive({df2 %>% pull(input$IndicatorId) %>% sum()})
IndicatorSum<- df2 %>% pull(input$IndicatorId) %>% sum()
print(IndicatorSum)
#browser()
df3<-mutate(df2,!!quo_name(paste0(input$IndicatorId,'_perctcontcl')):=(!!sym(input$IndicatorId)/IndicatorSum)*input$bins)
print(df3)
#To check why we get ghost factor in plotly, we add stringsAsFactors = FALSE to data.frame to solve this issue
print(str(df3))
output$Test2 <- renderPlotly({
Test2 <- plot_ly(
df3, x = ~Brand_Name,
y = ~get(paste0(input$IndicatorId,'_perctcontcl')),
#Here I think you need the updated Indicator after we multiply by input$bin
#y = ~get(input$IndicatorId),
type = "bar",color=~Brand_Name)})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With