Home > database >  Processing a reactive dataframe gives invalid (NULL) left side of assignment error
Processing a reactive dataframe gives invalid (NULL) left side of assignment error

Time:06-03

I have the shiny app below in which the user selects a metric from the dropdown in the sidebard and modifies the dataframe.Now I want to create a point&line chart of this dataframe.Im trying to create some more conditions in order to rocess the data and create the plot but I get:invalid (NULL) left side of assignment when I try to process the reactive dataframe I create.

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,BRAND_COLOR,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(
    DTOutput("df"),
    plotlyOutput("plot")
  )
)

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
      combinedDf$label<-paste(combinedDf$BRAND,"-",input$metric)
      return(combinedDf)
    }else{
      graph1.data$label<-paste(graph1.data$BRAND,"-",input$metric)
      
      return(graph1.data) 
    }

  })
  
  
  
  output$df<-renderDT({
    reactiveDf()
  })
  
  
  output$plot<-renderPlotly({
    req(reactiveDf())
    metric<-input$metric
    brand.colors <- reactiveDf()$BRAND_COLOR
    names(brand.colors) <- reactiveDf()$BRAND
    data<-unique(reactiveDf()$BRAND)
    ind=which(names(brand.colors) %in% data)
    
    if(length(metric) == 1) {
      for ( i in 1:length(brand.colors))
      {
        reactiveDf()$BRAND[i]=paste(reactiveDf()$BRAND[i],metric)
      }
      if (metric!="Sell Out")
      {
        for (i in ind)
        {
          brand.colors[i]="gray"
          reactiveDf()[i]="Insignificant"
        }
      }
      
      names(brand.colors) <- reactiveDf()$BRAND

      p <- reactiveDf() %>%
        ggplot2::ggplot(aes(x, y, color = BRAND))
      p <- p   
        ggplot2::geom_line(aes(x))   
        # warnings suppressed on text property
        suppressWarnings(ggplot2::geom_point(aes(x, y, size = r, 
                                                 #text = hovertext
                                                 ), show.legend = TRUE))  
        ggplot2::scale_color_manual(values = brand.colors)
      
    }
    
  })

}

shinyApp(ui, server)

CodePudding user response:

The result of a reactive({}) call can't be modified in other places. Either you create a local copy as done below (see Df <- req(reactiveDf())) or you use a reactiveVal/reactiveValue instead:

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,BRAND_COLOR,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(
    DTOutput("df"),
    plotlyOutput("plot")
  )
)

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
      combinedDf$label<-paste(combinedDf$BRAND,"-",input$metric)
      return(combinedDf)
    }else{
      graph1.data$label<-paste(graph1.data$BRAND,"-",input$metric)
      
      return(graph1.data) 
    }
    
  })
  
  
  
  output$df<-renderDT({
    reactiveDf()
  })
  
  
  output$plot<-renderPlotly({
    Df <- req(reactiveDf())
    metric<-input$metric
    brand.colors <- reactiveDf()$BRAND_COLOR
    names(brand.colors) <- reactiveDf()$BRAND
    data<-unique(reactiveDf()$BRAND)
    ind=which(names(brand.colors) %in% data)
    
    if(length(metric) == 1) {
      for ( i in 1:length(brand.colors))
      {
        Df$BRAND[i]=paste(reactiveDf()$BRAND[i],metric)
      }
      if (metric!="Sell Out")
      {
        for (i in ind)
        {
          brand.colors[i]="gray"
          Df[i]="Insignificant"
        }
      }
      
      names(brand.colors) <- Df$BRAND
      
      p <- Df %>%
        ggplot2::ggplot(aes(x, y, color = BRAND))
      p <- p   
        ggplot2::geom_line(aes(x))   
        # warnings suppressed on text property
        suppressWarnings(ggplot2::geom_point(aes(x, y, size = r, 
                                                 #text = hovertext
        ), show.legend = TRUE))  
        ggplot2::scale_color_manual(values = brand.colors)
      
    }
    
  })
  
}

shinyApp(ui, server)
  • Related