0
votes

I'm fairly new to R Shiny but am stuck with the following problem for some time now and hope you can provide some help:

  • I try to refresh a plot in R shiny that should be refreshed if either a new input argument is entered OR an action button is pressed. This should be straightforward, but unfortunately I can't solve it, despite googling/reading instructions for some time. Any advice would be recommended. Any solutions on the web seem to put the whole renderplot function inside the observeEvent function, but I also need the renderplot in addition outside of it to account for the possibility of just entering inputs without pressing the action button.

  • I have no trouble creating a (render)plot that either exclusively is refreshed when entering a new input or exclusively refreshed when pressing a button.

  • However when doing both at the same I fail: I first tried to copy the renderplot function including the resulting output twice one time within an observeEvent function (to account for clicking the action button) and one time outside of an observeEvent (to account for only refreshing the inputs to the plot) but this leads only to a greyed out graph that refreshes after ~10 seconds delay when pressing the action button. I imagine adding the reactive click input generated from clicking the action button directly to the renderplot outside of observe event , but so far I couldn't get it to run. Any recommendations would be much appreciated.

Thank you in advance.

1
You are unlikely to get much help without a minimal working example code in your post. Consider adding one.Shree

1 Answers

1
votes

Like this?:

Edit: No need to pass the selectInput to the reactive Vals.. this does the same:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId="select", label="title", choices=LETTERS[1:3], selected = "A"),
      actionButton(inputId="btn", label="refresh")
    ),
    mainPanel(
      plotOutput("scatterPlot")
    )
  )
)

server <- function(input, output) {

  plotSettings <- reactiveValues()

  observeEvent(c(input$btn, input$select), {
    plotSettings$values <- runif(100,1,100)
    # plotSettings$title <- input$select
  }, ignoreNULL = FALSE)

  output$scatterPlot <- renderPlot({
    plot(plotSettings$values, main=input$select)
  })
}

shinyApp(ui = ui, server = server)