0
votes

Simple question I think. I have a shiny app with min-max range slider input widgets. I pass the widget values individually to a function that applies an algorithm.

I'd like to first assign the min-max values to a 2 column matrix or dataframe. And then pass that object to the function.

Can somebody suggest the shiny syntax to do that? Thx. SteveM

Edit: The function is launched from an action button not from slider value changes.

1
Is the assigning of variables triggered by a button or reactive to the changing of the slider? As in do you want to continuously update the DF every time the sliders are changed or only when a button is pushed?Chabo
Maybe I'm not understanding your question, but wouldn't it just be as.matrix(c(input$my_slider[1], input$my_slider[2]))?tblznbits

1 Answers

0
votes

We only have to use a reactive environment when initially accessing these variables and calling functions that use said variables. In this case we assign the min and max as reactive variables since they change with time. These reactive varaibles we use to create a reactive DF, this leaves us with min(),max(), and DF(). From here we use these when calling functions, but don't need to specify the reactivity once in the function. Example: if Test(min) is a function with one argument min being passed, and min() is a reactive varaible, we can call the function and pass a reactive variable with Test(min()). See below for all the applied examples to your case.

library(shiny)

#Basic UI Setup
ui <- shinyUI(
  fluidPage(
    sidebarPanel(
      sliderInput("Slider", "Min/Max", 0, 10, c(2,8) )

    ),
    mainPanel(
      tableOutput("DF")
    )

    ))

Fully reactive example, triggered by changes to slider

 server <- function(input, output, session) {

 #You can access the inputs reactivity this way...
 Slider<-reactive({input$Slider})
 min<-reactive({Slider()[1]})
 max<-reactive({Slider()[2]})
 DF<-reactive({data.frame(min(),max())})

#This is how you would use the min/max values in a function
 Test<-function(min,max){
   Test<-min
   Test2<-max
   Fin<-data.frame(Test,Test2)
   names(Fin)<-c("min","max")
   return(Fin)
 }

  #renderTable is reactive, calling the Test function with min() and max()
  output$DF<-renderTable({
    Test(min(),max())
  })
 }
shinyApp(ui = ui, server = server)

enter image description here

Using the reactive DF with two columns in a function

#Using the premade reactive DF in a function
More_Tests<-function(DF){
  Df_Test<-DF
  return(DF)
}

...

output$DF2<-renderTable({
More_Tests(DF())
})

Using a button ID'ed "GO", downside of this option is that the values created here can only be used inside the observeEvent environment. If that does not work for you, see the final example.

observeEvent(input$Go,{
 Slider<-input$Slider
 min<-Slider[1]
 max<-Slider[2]
 DF<-data.frame(min,max)

 Tests<-function(DF){
   Df_Test<-DF
   return(DF)
  }


 output$DF2<-renderTable({
   Tests(DF)
 })
})

Last option is to use eventReactive in order to return the DF to the environment. This is unlike observeEvent in that we can access the value outside of the observe environment, but the values are now reactive.

server <- function(input, output, session) {

DF<-eventReactive(input$Go,{
 Slider<-input$Slider
 min<-Slider[1]
 max<-Slider[2]
 DF<-data.frame(min,max)
})

  Tests<-function(DF){
    Df_Test<-DF
    return(DF)
  }

  output$DF2<-renderTable({
    Tests(DF())
  })