r - Reactivity/renderPlot() not working in ggvis + Shiny -
i trying make shiny app user provides budgetary constraint, constraint formulates optimization equation maximizing protein (via lpsolve package), , optimization equation displayed in graph of protein-heavy foods (x-axis) vs. servings/wk (y-axis).
when run current code, side panel pops up, graph in main panel doesn't appear. if delete renderplot() function, graph show up, isn't interactive.
server.r:
shinyserver( function(input, output) { # user inputs max amt spent on food/wk budget <- reactive({ as.numeric(input$budget) }) output$myplot <- renderplot({ # set 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 variables have 0 coefficient in model maxprotvec <- maxprot$solution x <- data.frame(snap2$food, snap2$foodgroup, snap2$protperserv, maxprotvec) x <- x[!(x$maxprotvec == 0),] x <- droplevels(x) # plot optimization equation, i.e. food on x-axis , 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") }) })
ui.r:
shinyui(pagewithsidebar( titlepanel(title = h2("visualizing snap", align = "center")), sidebarpanel( helptext("the usda , aha recommend following daily guidelines..."), sliderinput("budget", label = "what weekly grocery budget? (snap benefits designed cover 1/3 of total food expenditure):", min = 0, max = 100, value = 50), uioutput("plot_ui") ), mainpanel( div(style = "width:800px; height:600px;", ggvisoutput("myplot") ) ) ))
Comments
Post a Comment