0

Question: Is there a workaround how I can simulate clicking of an actionButton within the server code?

Background: I've got a shiny app with quite a few parameters that the user is supposed to type in. This part of the app is divided into several UIs, each one created with renderUI. At the beginning, the user only sees the first UI. Once she has filled in the fields, she is supposed to click on an actionButton and the second UI is dynamically generated, whereby the fields and the text of the second UI depend on the content of the fields of the first UI.

Once the user has filled in both UIs, I want her to be able to save the content of all the fields as a template and to load the template next time she will use the app. Saving is implemented as simply writing all the values into a csv in a structured way, and loading is done with update*-functions (such as updateNumericInput, updateTextInput etc.).

The problem is that I have somehow to simulate the clicking on the actionButton because at the time the template is loaded, the second UI hasn't been created yet. It doesn't help to add a dependency on the template upload button to the second UI, as the second UI needs some values from the first one.

AnjaM
  • 2,941
  • 8
  • 39
  • 62
  • the actionbuttons have a count. If you are doing the way I understand then update the action button with count +1, eg. input$button == 0 at the start, but you can probably force it to 1 if you want. Alternative to a .csv file would be https://github.com/johndharrison/shinyStorage – Pork Chop Jul 13 '15 at 13:36
  • @pops Are you sure it's possible to modify `input` objects within `server.R` except of with the `update*` functions? I tried it in the past and I didn't succeed. – AnjaM Jul 13 '15 at 14:01

1 Answers1

1

You can do this with a short javascript function that simply makes a call to the click() JS function. Here's a short simple shiny app that demonstrates how to do this.

library(shiny)
library(shinyjs)

jscode <- "shinyjs.click = function(id) { $('#' + id).click(); }"

runApp(shinyApp(
  ui = fluidPage(
    useShinyjs(),
    extendShinyjs(text = jscode),
    actionButton("btn1", "When you click me, I print the time"),
    actionButton("btn2", "When you click me, I'll click the other button")
  ),
  server = function(input, output, session) {
    observeEvent(input$btn1, {
      print(Sys.time())
    })
    observeEvent(input$btn2, {
      js$click("btn1")
    })
  }
))

Note that I'm using the shinyjs package to make interacting with JS easy

DeanAttali
  • 25,268
  • 10
  • 92
  • 118