Like in a previous question: How to listen for more than one event expression within a Shiny eventReactive handler
I'm wanting to listen for two events in my eventReactive expression, however in this case one event is more complicated than a single input and I cant get both the simple first event and the second more complicated event to both work together.
The first event is input$start
an actionButton that self deletes once clicked using removeUI()
and the second requires two inputs to trigger; input$nxt
which is an action button which requires a box to be ticked on a radioButtons widget (input$choice
) to trigger the event.
Both events trigger the same bit of code, which is a function I've written to randomly generate 2 photos from a database. The user then has to choose the which of the two photos they like most (the radioButton input$choice
) and click the actionButton input$nxt
to proceed.
The line I'm struggling with is
rv <- eventReactive(input$start |{req(input$nxt, isolate(input$choice))}, mysample(filenames))
It currently only reacts to the second expression {req(input$nxt, isolate(input$choice))}
.
If I don't include the isolate(input$choice)
and have:
rv <- eventReactive(input$start |input$nxt, mysample(filenames))
then it reacts fine to both.
Any help would be hugely appreciated:
My full code is as follows:
orig.filenames <- 1:10
filenames <- orig.filenames
mysample <- function(x){
tmp <- sample(x,2)
filenames <<- setdiff(filenames, tmp)
if(length(filenames) < 3) filenames <<- orig.filenames
tmp
}
ui <- fluidPage(
fluidRow(uiOutput(outputId = "uiimg1"), uiOutput(outputId = "uiimg2")),
fluidRow(uiOutput("radio")),
fluidRow(uiOutput("nxt")),
fluidRow(tags$div(HTML("<center>"),
actionButton("start", "Start"),
'id' = "strtbtn")))
server <- function(input, output) {
rv <- eventReactive(input$start |{req(input$nxt, isolate(input$choice))},
mysample(filenames))
observeEvent(input$start,
{output$uiimg1<- renderUI(column(6, HTML("<center>Left Image"),
fluidRow(imageOutput(outputId = "img1"))))})
observeEvent(input$start,
{output$uiimg1<- renderUI(column(6, HTML("<center>Right Image"),
fluidRow(imageOutput(outputId = "img2"))))})
observeEvent(input$start,
{output$nxt <- renderUI(wellPanel(HTML("<center>"),
actionButton("nxt","Next")))})
observeEvent(input$start,
{output$radio<- renderUI(
wellPanel(HTML("<center>"),
radioButtons(inputId = "choice",
label = "Which photo do you prefer?",
c("Left", "Right"),
inline = TRUE, selected = character (0)
)))})
observeEvent(input$nxt,
{output$radio<- renderUI(
wellPanel(HTML("<center>"),
radioButtons(inputId = "choice",
label = "Which photo do you prefer?",
c("Left", "Right"),
inline = TRUE, selected = character (0)
)))})
observeEvent(input$start,
removeUI(selector = "div:has(> #strtbtn)", immediate = TRUE))
output$img1 <- renderImage({
filename1<- normalizePath(path=
paste('/Users/Ben/Documents/Masters/Stats/Shiny/v8/www/',
paste(rv()[1], '.jpg', sep = ''), sep =''))
list(src = filename1, width=325, height=214)
}, deleteFile= FALSE)
output$img2 <- renderImage({
filename2<- normalizePath(path=
paste('/Users/Ben/Documents/Masters/Stats/Shiny/v8/www/',
paste(rv()[2], '.jpg', sep = ''), sep =''))
list(src = filename1, width=325, height=214)
}, deleteFile= FALSE)
}
shinyApp(ui = ui, server = server)