0

I have a list from 1 to 100, with common difference of 1.

So, my slider have a range from min=1 and max=100 too.

And the range I selected will store in the selected_value (Global Environment).

And here is my code :

library(shiny)
ui <- fluidPage(
  fluidRow(
    column(4,
           sliderInput("slider","Slider Range", 
                       min = 0, max = 100, value = c(40, 60)))),

  fluidRow(
    column(4, verbatimTextOutput("range"))))

server <- function(input, output) {

  output$range <- renderPrint({input$slider})
  observe(selected_value <<- input$slider)}

shinyApp(ui,server)

My problem is let's say I selected the range of 33 to 40 in the slider, but the selected_value will only show 33 and 40 only.

But what I want is it shows all the observations : 33, 34, 35, 36, 37, 38, 39, 40.

What should I add so that I can have the desired outcome? Will appreciate very much if anyone could give me a helping hand...

Gambit
  • 77
  • 1
  • 11
  • 1
    can you do something like `selected_value <<- input$slider[1]:input$slider[2]`? – Ben Mar 27 '20 at 14:27
  • @Ben is right, you can replace `input$slider` by `input$slider[1]:input$slider[2]` in `renderPrint`. Not sure why you need an `observe` statement though – bretauv Mar 27 '20 at 14:31
  • @Ben it works! Thank you very much Ben, really appreciate it <3 – Gambit Mar 27 '20 at 14:31
  • @bretauv is there a better way to store it in the Global Environment? – Gambit Mar 27 '20 at 15:26
  • you can put `input$slider[1]:input$slider[2]` inside a `reactive` expression and then call this `reactive` expression in `renderPrint`. That way, you will be able to re-use this `reactive` expression somewhere else – bretauv Mar 27 '20 at 17:08
  • @bretauv can you show me an example? I am so sorry because I am really new to R... really will be thankful to you... – Gambit Mar 27 '20 at 17:27
  • @Gambit added an example below, is that what you want? – bretauv Mar 29 '20 at 10:58
  • @bretauv thanks for the example ! But I can't see the range selected store in the Global Environment... or I missed the part? ^^ – Gambit Mar 31 '20 at 05:53
  • why do you want to store it in Global Environment? This is not required for a shiny app – bretauv Mar 31 '20 at 07:30
  • @bretauvI want to store it in Global Environment because I need to. In my GUI I still have other thing else later. I didn't show everything at here. So I am just asking is there any better way to store it in Global Environment or not only. – Gambit Mar 31 '20 at 08:44
  • @bretauv btw very thanks for your quick response ^^ :D – Gambit Mar 31 '20 at 08:44
  • I don't know how to do it, but maybe [this post](https://stackoverflow.com/questions/20333399/are-there-global-variables-in-r-shiny) will help you, or [this one](https://stackoverflow.com/questions/39436713/r-shiny-reactivevalues-vs-reactive). But I still don't understand why you need to store it in global environment, is it because you want to re-use this variable outside of the shiny app? – bretauv Mar 31 '20 at 09:02
  • @bretauv Yup, I still need to re-use this variable inside/outside of the shiny app. But I think this is very inconvenient. Because I am quite new to R, and to Shiny.. But I wish I can enhance the code for that it is better, if I don't need to store in Global Environment, which part of the code I can modify? I am sorry if I asked too much bretauv. – Gambit Mar 31 '20 at 09:08
  • I don't understand, if you don't need to store it in global env, then my answer below should be enough. – bretauv Mar 31 '20 at 09:15
  • @bretauv thank you very much, btw I have one more question can you help me out there? At here... [link](https://stackoverflow.com/questions/60944401/how-to-read-the-only-valid-observations-in-the-range-of-sliderinput-in-shiny-in) – Gambit Mar 31 '20 at 09:26

1 Answers1

0

Here's an example showing how to put the range in a reactive environment. This is necessary if you want to re-use this range somewhere else than in your first renderPrint. Here, for example, you can use it in two renderPrint environments:

library(shiny)
ui <- fluidPage(
  fluidRow(
    column(4,
           sliderInput("slider","Slider Range", 
                       min = 0, max = 100, value = c(40, 60)))),

  fluidRow(
    column(4, 
           verbatimTextOutput("range"),
           verbatimTextOutput("range_2"))))

server <- function(input, output) {

  the_range <- reactive({
    input$slider[1]:input$slider[2]
  })

  output$range <- renderPrint({
    the_range()
  })

  output$range_2 <- renderPrint({
    the_range()
  })

}

shinyApp(ui,server)

Since reactive expressions are really something basic and important in Shiny, I suggest you to follow some tutorials, such as this one.

bretauv
  • 7,756
  • 2
  • 20
  • 57