Home > OS >  Add new columns to a dataframe and rename them accordingly based on shiny widget selections
Add new columns to a dataframe and rename them accordingly based on shiny widget selections

Time:06-02

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)

CodePudding user response:

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("selctions"), # 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$selctions <- renderPrint({
    choices()
  })
  
  output$df<-renderDT({
    reactiveDf()
  })
  
}

shinyApp(ui, server)
  • Related