0

I'd like to add a short description of a dataset that's been previously selected for visualization (via selectInput). I am struggling to create a reactive output so that that a chunk of text (3-4 sentences) that corresponds to the selected input is shown, ideally in the sidebar panel. I am also not sure where to load the written descriptions so as not to overwhelm the code with text.

df <- data.frame(
      indicator = c("Fruits", "Vegetables"),
      description = c("This basket contains apples, oranges and lemons.",
                       "This basket contains cucumbers, onions and cauliflower.")
      )

ui <- tagList(
  
  navbarPage("Fruit salad",
    
    tabPanel("Pick your ingredients",
             
             sidebarPanel(
               
               # First input: user-chosen variable
               selectInput(inputId = "variable.name",
                           choices = indicators),
               
                
               # This should print the description based on the selected indicator
               textOutput(outputId = "short.descript")
               
      )
    )
  )
)

server <- function(input, output) {

 output$short.descript <- renderText({
  
  # here a function printing the description based on the "variable.name"
  
 })

}

I tried adding textOutput to the sidebar panel, but I don't know how to connect it with the previously selected input.

Thanks for any tips! P.S. I'm new to Shiny, no making fun of me please :-)

  • 1
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a minimal example of your shiny app and if needed a snippet of your data or some fake data. – stefan Apr 11 '23 at 14:11
  • 1
    Thanks Stefan! I now tried to illustrate my problem with an example. – biodegradable.glitter Apr 11 '23 at 18:28

1 Answers1

0

To achieve your desired result filter your dataset for the chosen indicator and pull the value of the corresponding description for which I use subset from base R. If you prefer the tidyverse then do df |> filter(indicator %in% input$variable.name) |> pull(description).

library(shiny)

df <- data.frame(
  indicator = c("Fruits", "Vegetables"),
  description = c(
    "This basket contains apples, oranges and lemons.",
    "This basket contains cucumbers, onions and cauliflower."
  )
)

indicators <- unique(df$indicator)

ui <- tagList(
  navbarPage(
    "Fruit salad",
    tabPanel(
      "Pick your ingredients",
      sidebarPanel(
        # First input: user-chosen variable
        selectInput(
          inputId = "variable.name",
          label = "Choose an ingredient",
          choices = indicators
        ),
        # This should print the description based on the selected indicator
        textOutput(outputId = "short.descript")
      )
    )
  )
)

server <- function(input, output) {
  output$short.descript <- renderText({
    req(input$variable.name)
    subset(df, indicator %in% input$variable.name, description, drop = TRUE)
  })
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:6657

stefan
  • 90,330
  • 6
  • 25
  • 51