0

I have the shiny app below in which I want the user to select 1 to 3 choices from the input and then add the relevant vectors to the existing dataframe graph1.data as new columns.

For example if he selects all 3 choices he should have a dataframe with 6 new columns. But instead of the vector names these columns should be named in that case x,y,x1,y1,x2,y2 regardless of which choice is selected first. For example if Gross Sales is selected first then GrossSalesx2 should be the x and GrossSalesy2 should be the y.

## app.R ##
library(shiny)
library(shinydashboard)
#library(plotly)
library(tidyr)
library(dplyr)
library(DT)
BRAND<-c("CHOKIS","CHOKIS","CHOKIS","CHOKIS","CHOKIS","CHOKIS","LARA CHOCO CHIPS","LARA CHOCO CHIPS","LARA CHOCO CHIPS")
BRAND_COLOR<-c("#8050f0","#8050f0","#8050f0","#8050f0","#8050f0","#8050f0","#f050c0","#f050c0","#f050c0")

SellOut.x<-c(23,34,56,77,78,34,34,64,76)
SellOut.y<-c(43,54,76,78,87,98,76,76,56)
GrossProfit.x1<-c(23,34,56,75,78,34,34,64,76)
GrossProfit.y1<-c(33,54,76,76,87,98,76,76,56)
GrossSales.x2<-c(53,34,56,77,78,34,34,84,76)
GrossSales.y2<-c(63,54,76,78,87,98,76,76,86)
r<-c(58,46,76,76,54,21,69,98,98)


graph1.data<-data.frame(BRAND,r)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    selectInput("metric","Metric",c('Gross Sales','Gross Profit','Sell Out'),multiple = T,selected = "Sell Out")
  ),
  dashboardBody(
    DTOutput("df")
  )
)

server <- function(input, output) {

  output$df<-renderDT({})

}

shinyApp(ui, server)
firmo23
  • 7,490
  • 2
  • 38
  • 114

1 Answers1

1

Try the following one:

library(shiny)
library(shinydashboard)
#library(plotly)
library(tidyr)
library(dplyr)
library(DT)
BRAND<-c("CHOKIS","CHOKIS","CHOKIS","CHOKIS","CHOKIS","CHOKIS","LARA CHOCO CHIPS","LARA CHOCO CHIPS","LARA CHOCO CHIPS")
BRAND_COLOR<-c("#8050f0","#8050f0","#8050f0","#8050f0","#8050f0","#8050f0","#f050c0","#f050c0","#f050c0")

SellOut.x<-c(23,34,56,77,78,34,34,64,76)
SellOut.y<-c(43,54,76,78,87,98,76,76,56)
GrossProfit.x1<-c(23,34,56,75,78,34,34,64,76)
GrossProfit.y1<-c(33,54,76,76,87,98,76,76,56)
GrossSales.x2<-c(53,34,56,77,78,34,34,84,76)
GrossSales.y2<-c(63,54,76,78,87,98,76,76,86)
r<-c(58,46,76,76,54,21,69,98,98)

graph1.data<-data.frame(BRAND,r)

# data frame containing columns to be added
df6 <- data.frame(SellOut.x, SellOut.y, GrossProfit.x1, GrossProfit.y1, GrossSales.x2, GrossSales.y2) 

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    selectInput("metric","Metric",c('Gross Sales','Gross Profit','Sell Out'),multiple = T,selected = "Sell Out")
  ),
  dashboardBody(
    verbatimTextOutput("selections"), # optional
    DTOutput("df")
  )
)

server <- function(input, output) {
  

  
  choices <- reactive({
   c3 <- gsub(" ", "", input$metric) # remove space
   
   return(c3)
  })
  
  reactiveDf <- reactive({
    if(length(choices()) > 0){
      # if choices match column names of df6
      g1 <- apply(sapply(X = choices(), FUN = grepl, colnames(df6)), MARGIN =  1, FUN = any)
      
      addedDf <- df6[, g1] # columns to be added
      colnames(addedDf) <- c("x", "y", "x1", "y1", "x2", "y2")[1:ncol(addedDf)] # change column names
      
      combinedDf <- cbind(graph1.data, addedDf) # add columns
      return(combinedDf)
    }else{
      return(graph1.data) 
    }
    
    
  })
  
  output$selections<- renderPrint({
    choices()
  })
  
  output$df<-renderDT({
    reactiveDf()
  })
  
}

shinyApp(ui, server)
bdedu
  • 383
  • 1
  • 8
  • in the right direction but the colnames of the dataframe should follow this logic. The first 2 should always be named "x","y" -the second 2 "x1","y1" and the third 2 "x2","y2" regardless of the order that the inputs from the widget will be selected. – firmo23 Jun 01 '22 at 18:35
  • 1
    Then you only need to change the names of the added columns. See the modified code. – bdedu Jun 01 '22 at 19:03
  • follow up Q based on your answer https://stackoverflow.com/questions/72476053/processing-a-reactive-dataframe-gives-invalid-null-left-side-of-assignment-err – firmo23 Jun 02 '22 at 12:10