Filtered data inside sliderInput() in shiny?

Refresh

April 2019

Views

205 time

1

I have a sliderInput() which the max value I would like to be changed based on the users input. I am trying to create a wordcloud and the code for the wordcloud is working, just my slider need adjustments. So my dataset looks like this:

document   term    count
1          code    1
1          help    28
1          stupid  4
1          shock   7
2          fire    2
2          fly     9
2          money   1
2          free    3
.          .       .
.          .       .
20

What I want is that my max value inside the sliderInput() function should be automatically setted with the highest count of a selected document. So the user can select between different documents "1", "2", till "20". Based on the selection the max count should be assigned to the max value of the sliderInput(). In my example: If selecting document 1 -> max value = 28. If selecting document 2 -> max value = 9. I have a filtered function which I use in the server.R. If I could use this function inside the ui.R my problem is solved but its not allowed using functions from server insider ui. How can I solve this problem?

Code ui.R:

        # Select document
        box(
          title = "Document Control",
          status = "primary",
          solidHeader = TRUE,
          width = 4,
          selectInput("doc", label="Select Document", choices = c(1:20), selected = 1)
        ),

        # Slider
        box(
          title = "Frequency Control",
          status = "primary",
          solidHeader = TRUE,
          width = 4,
          height = 142,
          sliderInput("minFreq", label = "Minimum Frequency", min = 1, max = ... , value = 15)
        ),

            box(
              title = "Number Control",
              status = "primary",
              solidHeader = TRUE,
              width = 4,
              height = 142,
              sliderInput("maxNum", label = "Maximum Number of Words", min = 1, max = 400, value = 150)

            ),

Server code:

  filtered <- reactive({
    Wcloud.Data.filtered <- Wcloud.Data %>%
      filter(document == input$doc)
  }) 

  output$plotWcloud <- renderPlot({

    wordcloud(words = filtered()$term, freq = filtered()$count, 
              min.freq = input$minFreq, max.words = input$maxNum, random.order=FALSE, 
              rot.per=0.35, colors=brewer.pal(8, "Dark2"))

  })

1 answers

1

The sliderInput is created in the server function with a renderUI. Every time you reload the app, a new dataset is created and the slider updates min/max/ and takes the 5 value of the data.

library(shiny)

ui <- fluidPage(
    uiOutput("slider")
)

server <- function(input, output) {
  output$slider <- renderUI({
    data = round(runif(10, 1, 100),2)
    sliderInput("sliderInp", label = "Select a value", min = min(data), max = max(data), value = data[5])
  })
}

shinyApp(ui, server)

Implemented in your example would result in:

ui:

# Select document
box(
  title = "Document Control",
  status = "primary",
  solidHeader = TRUE,
  width = 4,
  selectInput("doc", label="Select Document", choices = c(1:20), selected = 1)
),

# Slider
box(
  title = "Frequency Control",
  status = "primary",
  solidHeader = TRUE,
  width = 4,
  height = 142,
  uiOutput("slider")
),

server:

filtered <- reactive({
  Wcloud.Data.filtered <- Wcloud.Data %>%
    filter(document == input$doc)
}) 

output$slider <- renderUI({
  sliderInput("minFreq", label = "Minimum Frequency", min = min(filtered()), max = max(filtered()), value = 15)
})

output$plotWcloud <- renderPlot({
  wordcloud(words = filtered()$term, freq = filtered()$count, 
            min.freq = input$minFreq, max.words = input$maxNum, random.order=FALSE, 
            rot.per=0.35, colors=brewer.pal(8, "Dark2"))
})