How to read the only valid observations in the rage sliderInput in Shiny in R

谁都会走 提交于 2020-04-18 05:47:36

问题


I have randomly generated a reproducible example of range. And I wish that my input will store in the Global Environment (selectednumbers). So when I run the shinyApp, let's say I choose the slidebarInput between 1 and 100, the verbatimTextOutput will shows (which I satisfied):

[1]  1  2  3  6  8 11 15 17 20 21 22 27

But when I checked for the selectednumbers (Global Environment), it shows (which I don't want):

  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
 [21]  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40
 [41]  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60
 [61]  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80
 [81]  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100

And this is my desired outcome for the Global Environment :

[1]  1  2  3  6  8 11 15 17 20 21 22 27

Which part I have do the mistake? Can anyone help me? Will appreciate it a lot...

This is my code :

library(shiny)

range <- c( 1 , 2 , 3 , 6 , 8 , 11 , 15 , 17 , 20 , 21 , 22 , 27 )

ui <- fluidPage(
  fluidRow(
    column(12,
           sliderInput("slider","Slider Range", 
                       min = 1, 
                       max = 100, 
                       value = c(40, 60)))),

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

server <- function(input, output) {

  selected_values <- reactive({
    range[range >= input$slider[1] & range <= input$slider[2]]})

  output$range <- renderPrint(selected_values())

  observe(selectednumbers <<- input$slider[1]:input$slider[2])

}

shinyApp(ui,server)

来源:https://stackoverflow.com/questions/61181894/how-to-read-the-only-valid-observations-in-the-rage-sliderinput-in-shiny-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!