1
votes

In my ui.R I have an actionButton.

actionButton("myLoader", "Load Data")

Update: the server.R side:

output$myLoader <- reactive({
  cat("clicked!!") #I never see this logged to the console!
})

I cannot see the click being registered.

Further more, In my server.r, I am not sure how to wire it up, since there is nothing in the UI that DIRECTLY depends on the tasks it will carry out. I want it to 'source' some R files which will load data. The data (for simplicity), ends up in a dataframe called 'myDf'.

The issue is that the ui is already updated by reactive functions, like.:

MainDataset <- reactive({ 
... #subset(myDf) based on other input controls like sliders etc.
})

output$myplot <- renderChart2({
    ... #use MainDataset()
})

How can I wire up the action button so that: - it can load the data it needs into myDf - have it somehow trickle through to the existing reactive functions and finally the plots? Not looking for exact solution, just pointers in the structure of what the server side of the actionButton should look like...

I am asking because all the examples seem to be updating a label in the UI, that's not along the lines of what I want to do.

Thanks!

2
This question might have an example of something similar to what you want to doNicE
Thanks, @NicE I have had a look but not sure how to tie that solution into what I'm doing. Actually, investigating further, I have bigger problems. I can't even trace the call to my button click. I'll update my question accordingly.rstruck

2 Answers

1
votes

You can create a reactive expression with a dependency to the button that will load the data you need. For example:

loadHandler <- reactive({
  #creates a dependency on the button 
  #when the button is clicked, 1 is added to input$myLoader
  #so the if statement will only be executed once the button is clicked.
  if(input$myLoader){
       #load your data here
 }
})

If you do not need what is returned by the reactive expression, you can use observe instead.

0
votes

After lots of trial and error, this is what seems to work for me:

in server.R:

loadHandler <- reactive({
  input$myLoader #create a dependency on the button, per Shiny examples.

  #load data.
  #this is a func that uses 'source()' to run a whole bunch of R files
  #each of which loads data (eg csv file), and sets global dataframes
  #that are then subsetted via other UI elements like sliders etc.
  myloadingFunc(input$input1, input$input2)

  #updates a label. Shouldn't need to do this?
  #just put this here in case a reactive() needs to return something...
  "loaded dataset XYZ"
})

update: the only problem I have now is that 'loadHandler' runs at startup, instead of waiting for the button to be clicked. :-(