我正在尝试制作一个Shiny应用程序,其中用户提供预算约束,该约束形成最大化蛋白质的最优化方程式(通过lpsolve包),并且优化方程式显示在蛋白质重的食物图表中( x轴)与服务/周(y轴)。
当我运行当前代码时,会弹出侧面板,但主面板中的图形不会出现。如果我删除了renderPlot()函数,图形将显示,但不是交互式的。
server.R :
<script>
function changeBackground(){
document.body.background = "<?=$varImage;?>";
}
</script>
ui.R :
shinyServer(
function(input, output) {
# user inputs max amt spent on food/wk
budget <- reactive({
as.numeric(input$budget)
})
output$myPlot <- renderPlot({
# set up optimization eq maximizing protein given constraints
maxProt <- lp("max",
SNAP2$protPerServ,
rbind(SNAP2$fatPerServ, SNAP2$fatPerServ, SNAP2$costPerServ, SNAP2$sodiumPerServ, SNAP2$fiberPerServ, SNAP2$sugarPerServ, SNAP2$calsPerServ, SNAP2$calsPerServ, SNAP2$fruit, SNAP2$vegs, SNAP2$grains, SNAP2$grains, SNAP2$meatProtein, SNAP2$dairy),
c("<=", ">=", "<=", "<=", ">=", "<=", ">=", "<=", ">=", ">=", ">=", "<=", ">=", ">="),
c(245, 140, budget(), 16100, 217, 262.5, 12600, 17850, 16, 28, 9, 25, 6.4, 24))
# drop any variables that have a 0 coefficient in the model
maxProtVec <- maxProt$solution
x <- data.frame(SNAP2$food, SNAP2$foodGroup, SNAP2$protPerServ, maxProtVec)
x <- x[!(x$maxProtVec == 0),]
x <- droplevels(x)
# plot the optimization equation, i.e. food on x-axis and no. servings
# of each food on y-axis
myPlot <- x %>%
ggvis(~SNAP2.food, ~maxProtVec, fill = ~SNAP2.foodGroup) %>%
layer_points(size := "400", opacity := "0.8") %>%
add_axis("x", title = "", ticks = 14) %>%
add_axis("y", title = "Servings Per Week") %>%
bind_shiny("myPlot", "plot_ui")
})
})