Reputation: 13
this is my first experience with shiny, and I've run into a stumbling block with reactivity. I also know this is a very clunky way of running the analysis, which I will redo at a later time.
I'm trying to get it so the histogram changes based on the user input.
The ui.R is:
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("United States Population Pyramid 2015-2090"),
sidebarPanel(
radioButtons("iradio", label = h3("Select year"),
choices = list("2015" = 2015, "2040" = 2040, "2065" = 2065, "2090" = 2090), selected = 2015))
,
mainPanel(
plotOutput('hist')
)
))
And the server.R is:
library(shiny)
library(plotrix)
xx.2015<-c(3.0, 3.1, 3.1, 3.1, 3.5, 3.3, 3.4, 3.0, 3.2, 3.2, 3.6, 3.5, 3.0, 2.6, 1.9, 1.4, 1.1, 0.8, 0.4, 0.1, 0.0)
xy.2015<-c(3.1, 3.3, 3.3, 3.3, 3.7, 3.5, 3.5, 3.1, 3.2, 3.2, 3.5, 3.4, 2.8, 2.4, 1.6, 1.1, 0.8, 0.5, 0.2, 0.0, 0.0)
xx.2040<-c(2.9, 2.9, 2.9, 3.0, 3.0, 3.0, 3.1, 3.1, 3.0, 3.2, 3.0, 2.9, 2.5, 2.6, 2.4, 2.5, 2.1, 1.4, 0.7, 0.2, 0.0)
xy.2040<-c(3.0, 3.0, 3.1, 3.1, 3.1, 3.1, 3.2, 3.1, 3.1, 3.4, 3.1, 2.9, 2.5, 2.5, 2.3, 2.2, 1.7, 1.0, 0.4, 0.1, 0.0)
xx.2065<-c(2.8, 2.8, 2.8, 2.9, 2.9, 2.9, 3.0, 3.0, 3.0, 3.0, 2.8, 2.9, 2.8, 2.6, 2.7, 2.3, 2.0, 1.4, 0.9, 0.4, 0.1)
xy.2065<-c(2.9, 2.9, 3.0, 3.0, 3.0, 3.0, 3.1, 3.1, 3.1, 3.0, 2.9, 2.9, 2.8, 2.6, 2.7, 2.2, 1.8, 1.2, 0.7, 0.2, 0.1)
xx.2090<-c(2.7, 2.7, 2.7, 2.7, 2.8, 2.8, 2.9, 2.9, 2.9, 2.8, 2.8, 2.8, 2.8, 2.7, 2.6, 2.4, 2.2, 1.7, 1.2, 0.6, 0.2)
xy.2090<-c(2.8, 2.8, 2.8, 2.9, 2.9, 2.9, 3.0, 3.0, 2.9, 2.9, 2.9, 2.9, 2.8, 2.7, 2.6, 2.3, 2.0, 1.5, 0.9, 0.4, 0.1)
agelabels<-c("0-4","5-9","10-14","15-19","20-24","25-29","30-34",
"35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74",
"75-79","80-44","85-89", "90-94", "95-99", "100+")
shinyServer(
function(input, output) {
if (input$iradio == 2015) {
xx.pop <- xx.2015
xy.pop <- xy.2015
}
if (input$iradio == 2040) {
xx.pop <- xx.2040
xy.pop <- xy.2040
}
if (input$iradio == 2065) {
xx.pop <- xx.2065
xy.pop <- xy.2065
}
})
output$hist<-renderPlot({
func()
par(mar=pyramid.plot(xy.pop,xx.pop,labels=agelabels,
lxcol="lightblue",rxcol="pink",
gap=0.5,show.values=TRUE))
})
The current error that I'm getting is Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
I realize that the if statements need to be in a reactive() but if someone could advise also if the histogram needs to be reactive as well.
Upvotes: 1
Views: 229
Reputation: 867
Couple of things to do :
output$hist
inside the shinyServer
functionMove the if
statements inside the output$hist
function
shinyServer(function(input, output) {
output$hist<-renderPlot({
if (input$iradio == 2015) {
xx.pop <- xx.2015
xy.pop <- xy.2015
}
if (input$iradio == 2040) {
xx.pop <- xx.2040
xy.pop <- xy.2040
}
if (input$iradio == 2065) {
xx.pop <- xx.2065
xy.pop <- xy.2065
}
par(mar=pyramid.plot(xy.pop,xx.pop,labels=agelabels,
lxcol="lightblue",rxcol="pink",
gap=0.5,show.values=TRUE))
})
})
OUTPUT
Upvotes: 1