I am updating my post earlier because I've been finding solutions as I learn Shiny but I am still getting an error. I hope this is not bad form in this forum.
My app reads an R script containing a function, and reads a second script with an observer executing the function with data entered by the user (a dataframe from the HD and a numeric input). I learnt today that when reading the second R scipt with observeEvent I need to take the dataframe output out of the observer so I made the dataframe a reactive and that solved the problems I was having in my last reply to my early post. Now I get no errors from ggplot.
But now I get an error that seems to be something different, still related to the dataframe but apparently not from ggplot.
To give a reprex I will reproduce my original post with the new app code:
# Create toy data and save it in 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)
# First script, saved as stuff1.R
stuff1 <- function(x,p)
{
y <- x^p
z <- log(y)
return(z)
}
# Second script, saved 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))
# The app.R code
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()
restab <- reactiveValues(outtab2=NULL)
observe({
req(input$Datastuff,input$Powerstuff)
datalist(list(Datastuff=input$Datastuff,
Powerstuff=input$Powerstuff))
})
observeEvent(input$runScript,{
source("Stuff2.R",local=list2env(datalist()))
restab$outtab2 <- outtab2
})
output$plot <- renderPlot({
req(input$Datastuff,input$Powerstuff,input$runScript)
p1 <- ggplot(data=restab$outtab2,
aes(x=factor(restab$outtab2$it),
y=restab$outtab2$out,
fill=restab$outtab2$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") +
theme(axis.text.x=element_text(angle=90))
})
}
shinyApp(ui=ui,server=server)
Thanks for any help.
Ruben