Dear Shiny experts, I think you will fild my error quickly. I am new to Shiny and have been developing my app for a few days and I am close but currently stuck with an error that shows in the main panel: Error: object 'outtab2' not found. The real app will load 3 data files, input a few numeric constants, and then run two R scripts to show a plot made with ggplot. When I press Run App in RStudio the error message appears. Here is a toy code and app that has the same structure as the real code and app but it is reduced to the bare minimum, it is a reprex as requested in the rules of this community:
Start reprex.
#Create toy data and save it to the HD:
intab <- data.frame(this=c(rep("A",3),rep("B",3)),
that=rep(1:3,2),
now=runif(6,2,8),
then=runif(6,3,9))
write.csv(intab,"intab.csv",row.names=FALSE)
R script that contains one function (the real script has many functions but the output is one data.frame as in this case).
Save it as 'Stuff01.R'
stuff1 <- function(x,p)
{
y <- x^p
z <- log(y)
return(z)
}
This is the second script, simply executing the stuff1 function with data. Save it as 'Stuff2.R':
Datastuff$now2 <- stuff1(x=Datastuff$now,p=Powerstuff)
Datastuff$then2 <- stuff1(x=Datastuff$then,p=Powerstuff)
outtab2 <- data.frame(when=c(rep("now2",6),rep("then2",6)),
it=paste(rep(Datastuff$this,2),rep(Datastuff$that,2),sep="-"),
out=round(c(Datastuff$now2,Datastuff$then2),3))
And finally this is the app:
library(shiny)
library(ggplot2)
source("Stuff1.R")
ui <- fluidPage(
titlePanel(
list(tags$head(tags$style(".span2 {background-color: black;}")),
"CapCarga: Bioenergetic carrying capacity for mussel open sea cultivation"
)
),
sidebarLayout
(
sidebarPanel
(
fileInput(inputId="Datastuff",label="Choose stuff data file",accept=".csv"),
numericInput(inputId="Powerstuff",label="Enter the power stuff",value=NULL),
actionButton("runScript","Run", icon("paper-plane"),style="color: #fff; background-color: #337ab7; border-color: #2e6da4")
),
mainPanel
(
plotOutput(outputId="plot",width="100%")
)
)
)
server <- function(input,output,session)
{
datalist <- reactiveVal()
observe(
{
datalist(
list(
Datastuff=input$Datastuff,
Powerstuff=input$Powerstuff
)
)
}
)
observeEvent(
input$runScript,{
source("Stuff2.R",local=list2env(datalist()))
}
)
output$plot <- renderPlot({
#browser()
p1 <- ggplot(data=outtab2,aes(x=factor(it),y=out,fill=when))
p1 + geom_bar(stat="identity",position=position_dodge(0.9)) +
labs(x="it",y="out",cex=0.5) +
ggtitle("now and then") +
scale_fill_discrete(name="it") +
geom_text(aes(label=out),colour="black",size=3,nudge_x=0) +
theme(axis.text.x=element_text(angle=90))
})
}
shinyApp(ui=ui,server=server)
End of reprex.
I've checked that the R part works fine, from data to plot. I think my problem is that I haven't found the way to make the renderPlot function WAIT for input so when I load the app it tries to find the data.frame output that should be coming from execution of the R scripts.
Thanks in advance for your help.
Ruben