Home > OS >  Changing text and plots with selected input in R shiny
Changing text and plots with selected input in R shiny

Time:11-11

I want to change the text in the box depending on what parameter is selected for the plot. So that text and plot change at the same time

So in that working example:

I wish to have the textoutput: "bliblablub" if the dropdown menu selects Sepal.width.

I wish to have the textoutput: "some text" if the dropdown menu selects Sepal.Length.

I wish to have the textoutput: "complete different stuff" if the dropdown menu selects Petal.Length.

I wish to have the textoutput: "new stuff" if the dropdown menu selects Petal.Width.

As I have in my original dataframe 15 parameters, I would love to get tips to handle the text input for each parameter as well. Ideally I would take the text from a second dataframe (having the same columnnames?).

So far I did not understand how to combine select Input, a textbox and a plotbox.

Thanks a lot for any help!

library(shiny)
library(shinydashboard)
#data
dat <-iris

#producing parameters for select input
param <- colnames(dat)
param <- param[1:4]


#ui
Header<- dashboardHeader(title = "Iris")

Sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Parametro", tabName = "parametro", icon = icon("car"))
  )
)

Body <- dashboardBody(
  tabItems(
    tabItem(tabName = "parametro",
            column(width = 12,
                   box(
                     plotOutput("boxplot_species")),
                   fluidRow(
                     box(
                       selectInput("parametro", "Parametros:",
                                   param), width = 6),
                     box(
                       textOutput("description"))
                   )    
            ))
  ))

ui <- dashboardPage(Header,
                    Sidebar,
                    Body)

server <- function(input, output) {
  output$description <- renderText({
    "bliblablub"
  })
  output$boxplot_species <- renderPlot({
    ggplot(dat)  
      aes(x = Species, fill = Species)  
      aes_string(y = input$parametro)  
      geom_boxplot()
  })
}
shinyApp(ui, server)

CodePudding user response:

You can use switch to update the text based on dropdown selection and replace aes_string with .data since aes_string is deprecated.

library(shiny)
library(shinydashboard)
library(ggplot2)

Header<- dashboardHeader(title = "Iris")

Sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Parametro", tabName = "parametro", icon = icon("car"))
  )
)

Body <- dashboardBody(
  tabItems(
    tabItem(tabName = "parametro",
            column(width = 12,
                   box(
                     plotOutput("boxplot_species")),
                   fluidRow(
                     box(
                       selectInput("parametro", "Parametros:",
                                   param), width = 6),
                     box(
                       textOutput("description"))
                   )    
            ))
  ))

ui <- dashboardPage(Header,
                    Sidebar,
                    Body)

server <- function(input, output) {
  output$description <- renderText({
    text <- switch(input$parametro,
            Sepal.Length = 'x', 
            Sepal.Width = 'y',
            Petal.Length = 'other',
            Petal.Width  = 'more'
    )
    paste(text, "is blabla")
  })
  output$boxplot_species <- renderPlot({
    ggplot(dat)  
      aes(x = Species, y = .data[[input$parametro]], fill = Species)  
      geom_boxplot()
  })
}
shinyApp(ui, server)

enter image description here

  • Related