我有一个包含3个选项卡的应用程序,每个选项卡具有3个不同的输出,我正在尝试使用其中一个选项卡的输出保存.csv文件,但出现错误Warning: Error in $.shinyoutput: Reading objects from shinyoutput object not allowed.
我需要保存的输出是在用户按下“ solveButton”及其在输出$ summary中后生成的。
从我看来,我似乎需要创建一个反应性值,并在顶部创建了一个solution
,并将其传递给output$summary
,但是仍然出现错误。
感谢您的帮助
require(shiny)
library(dplyr)
library(ggplot2)
library("lpSolveAPI")
setwd("C:/Users/Juan.Lozano/Documents")
runApp(
list(
ui = fluidPage(
headerPanel('Campaign Optimizer'),
sidebarPanel(
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
dateRangeInput('dateRange',
label = 'Filter campaigns by date',
start = as.Date(Sys.Date()) , end = as.Date(Sys.Date())),
numericInput("Budget", label="Budget", value=""),
numericInput("Max_Budget", label="Max Budget", value=""),
numericInput("Increment", label="Increment in %", value=""),
numericInput("Budget_Solution", label="Solve Budget", value=""),
actionButton("plotButton", "PLOT"),
actionButton("solveButton", "SOLVE"),
downloadButton("downloadData", "Download")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Table", tableOutput("table")),
tabPanel("Solve Summary", tableOutput("summary"))
))),
server = function(input, output) {
values <- reactiveValues()
solution <- reactive({
output$summary})
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = input$header)
})
observeEvent(input$plotButton,
{
inFile <- input$file1
data <- read.csv(inFile$datapath, header = input$header)
data$report_date<- as.Date(data$report_date , "%m/%d/%Y")
#
start_date = input$dateRange[1]
end_date = input$dateRange[2]
#
data_filtered <- dplyr::filter(data, report_date >= start_date & report_date <= end_date)
data_filtered <- data_filtered %>%
group_by(name) %>%
summarise( demand_est_sales= sum(demand_est_sales), demand_max_cost = sum(demand_max_cost), demand_units = mean(demand_units), demand_unit_cost = sum(demand_unit_cost))
data_filtered <- dplyr::mutate(data_filtered, agg_demand_roi = (demand_est_sales-demand_max_cost)/demand_max_cost)
data_filtered <- dplyr::mutate(data_filtered, agg_demand_unit_cost = demand_max_cost/demand_units)
data_filtered$agg_demand_roi[is.na(data_filtered$agg_demand_roi)] <- 0
data_filtered$agg_demand_unit_cost[is.na(data_filtered$agg_demand_unit_cost)] <- 0
data_filtered <- dplyr::arrange(data_filtered, desc(agg_demand_roi))
data_filtered <- dplyr::mutate(data_filtered, agg_demand_est_sales = agg_demand_roi * demand_max_cost)
data_filtered_final <- data_filtered[ c(1,4,7,6) ]
data_filtered_final <- dplyr::mutate(data_filtered_final, Max_Budget = demand_units * agg_demand_unit_cost)
data_filtered_final <- data_filtered_final[ c(1,5,4) ]
from = input$Budget
to = input$Max_Budget
percentage = (input$Increment)/100
by = from*percentage
dat <-seq(from,to, by)
df <- data.frame(matrix(ncol = 2, nrow = length(dat)))
x <- c("value", "max_sales")
colnames(df) <- x
df$value <- dat
df<- dplyr::mutate(df, percentage = ((value/from)-1)*100)
dat1 <- data_filtered_final
n <- nrow(dat1)
b = 1
while (b <= length(dat)) {
dat2<- dat1
model <- make.lp(n+1, n)
control <- lp.control(model, sense = "max")
set.objfn(model, dat2$agg_demand_roi)
set.bounds(model, upper = dat2$Max_Budget)
add.constraint(model, rep(1, n), "<=", df[b,1])
solve(model)
get.variables(model)
results <- get.variables(model)
dat2$optimal <- results
dat2<- dplyr::mutate(dat2, Max_sales = (agg_demand_roi*optimal))
df[b,2] <- sum(dat2$Max_sales)
b = b +1
}
output$plot <- renderPlot({
ggplot() +
geom_line(data = df, aes(x = percentage, y = max_sales), color = "blue")+ geom_line(data = df, aes(x = percentage, y = value), color = "red")
})
output$table <- renderTable({
df
})
})
observeEvent(input$solveButton,
{
inFile <- input$file1
data <- read.csv(inFile$datapath, header = input$header)
data$report_date<- as.Date(data$report_date , "%m/%d/%Y")
#
start_date = input$dateRange[1]
end_date = input$dateRange[2]
#
data_filtered <- dplyr::filter(data, report_date >= start_date & report_date <= end_date)
data_filtered <- data_filtered %>%
group_by(name) %>%
summarise( demand_est_sales= sum(demand_est_sales), demand_max_cost = sum(demand_max_cost), demand_units = mean(demand_units), demand_unit_cost = sum(demand_unit_cost))
data_filtered <- dplyr::mutate(data_filtered, agg_demand_roi = (demand_est_sales-demand_max_cost)/demand_max_cost)
data_filtered <- dplyr::mutate(data_filtered, agg_demand_unit_cost = demand_max_cost/demand_units)
data_filtered$agg_demand_roi[is.na(data_filtered$agg_demand_roi)] <- 0
data_filtered$agg_demand_unit_cost[is.na(data_filtered$agg_demand_unit_cost)] <- 0
data_filtered <- dplyr::arrange(data_filtered, desc(agg_demand_roi))
data_filtered <- dplyr::mutate(data_filtered, agg_demand_est_sales = agg_demand_roi * demand_max_cost)
data_filtered_final <- data_filtered[ c(1,4,7,6) ]
data_filtered_final <- dplyr::mutate(data_filtered_final, Max_Budget = demand_units * agg_demand_unit_cost)
data_filtered_final <- data_filtered_final[ c(1,5,4) ]
from = input$Budget
to = input$Max_Budget
percentage = (input$Increment)/100
by = from*percentage
dat <-seq(from,to, by)
df <- data.frame(matrix(ncol = 2, nrow = length(dat)))
x <- c("value", "max_sales")
colnames(df) <- x
df$value <- dat
df<- dplyr::mutate(df, percentage = ((value/from)-1)*100)
dat1 <- data_filtered_final
n <- nrow(dat1)
b = 1
dat2<- dat1
model <- make.lp(n+1, n)
control <- lp.control(model, sense = "max")
set.objfn(model, dat2$agg_demand_roi)
set.bounds(model, upper = dat2$Max_Budget)
add.constraint(model, rep(1, n), "<=", input$Budget_Solution)
solve(model)
get.variables(model)
results <- get.variables(model)
dat2$optimal <- results
dat2<- dplyr::mutate(dat2, Max_sales = (agg_demand_roi*optimal))
output$summary <- renderTable({
dat2
})
})
output$downloadData <- downloadHandler(
filename = function() {
paste("download_data", ".csv", sep = "")
},
content = function(file) {
write.csv(solution(), file, row.names = FALSE)
}
)
}
)
)