Reputation: 41
I have a question about reactive expressions. I am sure this has been asked in some variant before. Basically, I am defining a numericInput in my ui.R file, and then sending it to an external function, and then taking the output of that function and trying to run some code in renderPlot() to get a plot. However, I am getting all sorts of errors in renderPlot() section of server.R. The list is being passed in correctly, but I am unable to reference both fields with my notation, and the for loops are giving me all sorts of issues.
First, the call to barplot tells me "finite xlim values needed." This argument is not required outside of Shiny, so I don't know why it is needed. Anyway, after adding xlimits, I get an error at the following line:
lines(c(k-.5,k-.5), 100*c(mydata()$colu[i,1]+d[i], mydata()$colu[i,3]+d[i]), col=2, lty=3)
Error is: Error in xy.coords(x, y) : 'x' and 'y' lengths differ
Note that all of this code runs fine outside of Shiny, so I am stumped. I assume it has something to do with the for loop...?
Anyone have any thoughts? Thanks!
Here is my uI.r
shinyUI(pageWithSidebar(
headerPanel("App"),
sidebarPanel(
numericInput("num", "number", 1)
),
mainPanel(imageOutput("histogram"))
))
Here is my server.R
library(shiny)
source("main_algo.R",local=TRUE)
source("pred.interval.R",local=TRUE)
source("cov.matrix.R",local=TRUE)
shinyServer(
function(input, output) {
#actionButton("goButton","Create Plot")
mydata<-reactive({
output<-main_algo(input$num)
colu<-output[[1]]
numPts<-output[[2]]
list(numPts,colu) })
output$histogram<- renderPlot({
bardata=barplot(100*mydata()$colu[,8], width=1, space=0,ylim = c(-80,80), cex.axis = 1.5,
ylab = "%-change from baseline", cex.lab = 1.25, cex.main = 1.5,yaxt="n",
col="lightgray", main="response")
aa = c(-75,-50, -25, 0, 25, 50, 75)
axis(side=2,at=aa,labels=aa)
abline(h=c(-25, 25), lty=2, col=4)
# add error bars
k = 0
#d = (r.corr-r) # WARNING! THIS VARIABLE IS NOT PROPERLY SORTED
d = mydata()$colu[,8] - mydata()$colu[,9]
#d = rep(0, length(r))
for(i in 1:60){
k = k+1
lines(c(k-.5,k-.5), 100*c(mydata$colu[i,1]+d[i], mydata$colu[i,3]+d[i]), col=2, lty=3)
}
points(1:mydata()$numPts-.5, d*100 + 100*mydata()$colu[,1], col=2, pch="-", cex=2)
points(1:mydata()$numPts-.5, d*100 + 100*mydata()$colu[,3], col=2, pch="-", cex=2)
#points(1:n-.5, 100*mydata$colu[,9], col=1, pch="-", cex=2) #uptake time corrected data
pval = round(log10(mydata()$colu[,7]+10^-5),2)
abline(v = which.min(pval< log10(.05))-1, lty=2, lwd=2)
#abline(v = which.min(pval< log10(.05/60))-1, lty=2, lwd=2)
abline(v = 60 - which.min(pval[60:1]< log10(.05))+1, lty=2, lwd=2)
#abline(v = 60 - which.min(pval[60:1]< log10(.075/60))+1, lty=2, lwd=2)
text(43, -65, "sigma=1.36") # change for reactive output
arrows(3.5, -77, 3.5, -67, col = 2, length=.1)
arrows(21.5, -77, 21.5, -67, col = 2, length=.1)
})
})
Upvotes: 1
Views: 1398
Reputation: 12684
I think the problem stems from this line:
d = mydata()$colu[,8] - mydata()$colu[,9]
You are defining d
in terms of mydata()
, but mydata()
is reactive so d
must be reactive too. Try changing it to:
d <-reactive({ mydata()$colu[,8] - mydata()$colu[,9] })
and then refer to it as d()
in the rest of the code
Upvotes: 1