处理反应性数据帧会给出分配错误的无效(NULL)左侧
Processing a reactive dataframe gives invalid (NULL) left side of assignment error
我有下面这个闪亮的应用程序,用户可以在其中从侧边栏的下拉列表中选择一个指标并修改 dataframe.Now 我想创建一个点线图 dataframe.Im 试图创建一些更多条件以便处理数据并创建绘图但是我得到:invalid (NULL) left side of assignment
当我尝试处理我创建的反应性数据帧时。
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)
reactive({})
调用的结果不能在其他地方修改。您要么按照下面的方式创建本地副本(请参阅 Df <- req(reactiveDf())
),要么使用 reactiveVal
/reactiveValue
代替:
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)
我有下面这个闪亮的应用程序,用户可以在其中从侧边栏的下拉列表中选择一个指标并修改 dataframe.Now 我想创建一个点线图 dataframe.Im 试图创建一些更多条件以便处理数据并创建绘图但是我得到:invalid (NULL) left side of assignment
当我尝试处理我创建的反应性数据帧时。
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)
reactive({})
调用的结果不能在其他地方修改。您要么按照下面的方式创建本地副本(请参阅 Df <- req(reactiveDf())
),要么使用 reactiveVal
/reactiveValue
代替:
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)