How to set default eventReactive variables in shiny?

I want to change the value of colour parameter only when user clicks on the action button, but on the start of the shinyApp I want to have default colours.

I got this:

library(shiny)
library(ggplot2)

#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))


ui <- fluidPage(
  sliderInput("slider1","slider1", value=5, min=1, max=10),
  sliderInput("slider2","slider2", value=0.8, min=0, max=1),
  radioButtons("check1","check1",choices = c("red","blue","green")),
  actionButton("refreshColours","refreshColours"),
  plotOutput("rys")
)



server <- function(input,output){

  col1 <- eventReactive(input$refreshColours,{input$check1}) 

  pp <- reactive({ ggplot(dt, aes(x,y)) + 
      geom_point(size=input$slider1, 
                 alpha=input$slider2, colour=col1()) }) 

  output$rys <- renderPlot({ pp() }) }

shinyApp(ui=ui, server=server)

and I just want to see the plot at the start of the shinyApp, without clicking at the action button so I need default value in "colours" variable at the begening.

Upvotes: 2

Views: 1954

Answers (2)

Jordan Mandel
Jordan Mandel

Reputation: 498

In addition to the answers given here which condition on the numeric value of refreshColors there, you can just set ignoreNULL = FALSE in the call to eventReactive, and specify selected = "blue" (or whichever color you prefer) in the call to radioButtons.

#this is basically the code from the question with the above changes added
library(shiny)
library(ggplot2)

#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))

ui <- fluidPage(
    sliderInput("point_size","Point Size", value=5, min=1, max=10),
    sliderInput("point_alpha","Point Alpha", value=0.8, min=0, max=1),
    #here is where you select the default value with selected
    radioButtons("point_color","Point Color",choices = c("red","blue","green"),selected = "blue"),
    actionButton("refreshColours","Refresh Colors"),
    plotOutput("plot_out")
)

server <- function(input,output){
#set igoreNULL = FALSE here 
    col1 <- eventReactive( input$refreshColours,{input$point_color},ignoreNULL  = FALSE) 
    
    pp <- reactive({ ggplot(dt, aes(x,y)) + 
            geom_point(size=input$point_size, 
                                 alpha=input$point_alpha, colour=col1()) }) 
    
    output$plot_out <- renderPlot({ pp() }) }

shinyApp(ui=ui, server=server)

You can also use selected = character(0) in the call to radioButtons so that none of the radio buttons are selected to start, and use a function (color_with_default below) that returns a default color of your choosing if the value from the radioButtons is null, as it is when there is no selection. That default color is used in the initial plot that is rendered when the app starts because ignoreNULL is false in the call to eventReactive like it was in the previous example.

library(shiny)
library(ggplot2)

#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))


ui <- fluidPage(
    sliderInput("point_size","Point Size", value=5, min=1, max=10),
    sliderInput("point_alpha","Point Alpha", value=0.8, min=0, max=1),
    #use selected = character(0) to start with nothing selected
    radioButtons("point_color","Point Color",choices = c("red","blue","green"),selected = character(0)),
    actionButton("refreshColours","Refresh Colors"),
    plotOutput("plot_out")
)



server <- function(input,output){
    #here is the function that returns a default grey color if no button is selected    
    color_with_default <- function( color_in  ){
        if(is.null(color_in)){
            color_in <- "grey"
        } 
        return(color_in)
    }
    
    col1 <- eventReactive(eventExpr =  input$refreshColours, valueExpr = {input$point_color},ignoreNULL = FALSE) 
    
    pp <- reactive({
        ggplot(dt, aes(x,y)) + 
            geom_point(size=input$point_size, 
                                 #the call to the color_with_default function is here
                                 alpha=input$point_alpha, colour= color_with_default(col1()))
    }) 
    
    output$plot_out <- renderPlot({ pp() }) }


shinyApp(ui=ui, server=server)

Upvotes: 0

Ryan Morton
Ryan Morton

Reputation: 2695

You could simply put a default in the ggplot for the initial state:

pp <- reactive({ ggplot(dt, aes(x,y)) + 
      geom_point(size=input$slider1, 
                 alpha=input$slider2, 
                 colour= if(input$refreshColours == 0) "red" else col1() ) 
    })

Upvotes: 2

Related Questions