2

I am trying to update text and plot both with ActionButton click.

My Attempt-

library(shiny)
library(ggplot2)
library(shinyWidgets)

  ui <- fluidPage(
    actionGroupButtons(
      inputIds = c("Bar", "Histogram", "Line"),
      labels = list("Bar", "Histogram","Line"),
      status = "danger",
      fullwidth = T
    ),
   plotOutput('plot',height = '563px'),
   verbatimTextOutput('text')

  )

  server <- function(input, output) {
    output$plot <- renderPlot({

      if(req(input$Bar)!=0) {
      isolate({
        data <- iris
       ggplot(data, aes(Species,Petal.Length)) +
        geom_bar(stat="identity") 
      })

      } else if(req(input$Histogram)>0){
        isolate({
          data <-  iris
          ggplot(data, aes(Petal.Length)) +
            geom_histogram()

        })

      }  else if(req(input$Line)>0){
        isolate({
          data <-  iris
          ggplot(data, aes(Petal.Length,Sepal.Length)) +
            geom_line()

        })
      }

    })


    output$text <- renderText({

      if(req(input$Bar)!=0) {
        "Bar"

      } else if(req(input$Histogram)>0){

        "Histogram"

      }  else if(req(input$Line)>0){
        "Line"
      }

    })
  }

  shinyApp(ui, server)

I want to change plot and text when the appropriate action button is clicked.

1 Answer 1

12

Here would be one way to do it. In it's essence the approach is pointed out in the action button example no. 3 from RStudio.

library(shiny)
library(ggplot2)
library(shinyWidgets)

ui <- fluidPage(

  actionGroupButtons(
    inputIds = c("Bar", "Histogram", "Line"),
    labels = list("Bar", "Histogram","Line"),
    status = "danger",
    fullwidth = T
  ),

  plotOutput('plot',height = '563px'),
  verbatimTextOutput('text')

)

server <- function(input, output) {

  v <- reactiveValues(data = iris,
                      plot = NULL,
                      text = NULL)

  observeEvent(input$Bar, {
    v$plot <- ggplot(v$data, aes(Species,Petal.Length)) +
                geom_bar(stat="identity") 
    v$text <- "Bar"
  })

  observeEvent(input$Histogram, {
    data <- iris
    v$plot <- ggplot(v$data, aes(Petal.Length)) +
                 geom_histogram()
    v$text <- "Histogram"
  })  

  observeEvent(input$Line, {
    data <- iris
    v$plot <- ggplot(v$data, aes(Petal.Length,Sepal.Length)) +
                  geom_line()
    v$text <- "Line"
  })  

  output$plot <- renderPlot({
    if (is.null(v$plot)) return()
    v$plot
  })


  output$text <- renderText({

    if (is.null(v$text)) return()
    v$text

  })
}

shinyApp(ui, server)


Update

In case you are using Input filters on your data in a reactive, then you have to adjust the Approach above a litte:

library(shiny)
library(ggplot2)
library(shinyWidgets)

ui <- fluidPage(

    selectInput(inputId = "species", label = "Select species:",
                choices = unique(as.character(iris$Species)),
                selected = "setosa"),

    sliderInput("sepal_length", "Limit sepal length:",
                round = 0,
                min = range(iris$Sepal.Length)[1], max = range(iris$Sepal.Length)[2],
                range(iris$Sepal.Length),
                step = 0.1),

    actionGroupButtons(
        inputIds = c("Bar", "Histogram", "Line"),
        labels = list("Bar", "Histogram","Line"),
        status = "danger",
        fullwidth = T
    ),

    plotOutput('plot',height = '563px'),
    verbatimTextOutput('text')

)

server <- function(input, output) {

    data <- reactive({

        temp <- subset(iris, Species == input$species)
        subset(temp, Sepal.Length < input$sepal_length)

    })

    v <- reactiveValues(plot = NULL,
                        text = NULL)

    observeEvent(input$Bar, {
        v$plot <- ggplot(data(), aes(Species,Petal.Length)) +
            geom_bar(stat="identity") 
        v$text <- "Bar"
    })

    observeEvent(input$Histogram, {
        v$plot <- ggplot(data(), aes(Petal.Length)) +
            geom_histogram()
        v$text <- "Histogram"
    })  

    observeEvent(input$Line, {
        v$plot <- ggplot(data(), aes(Petal.Length,Sepal.Length)) +
            geom_line()
        v$text <- "Line"
    })  

    output$plot <- renderPlot({
        if (is.null(v$plot)) return()
        v$plot
    })


    output$text <- renderText({

        if (is.null(v$text)) return()
        v$text

    })
}

shinyApp(ui, server)
3
  • what if my data is coming from reactive function. Is it possible to do like this v <- reactiveValues(data = data(), plot = NULL, text = NULL) Jul 29, 2019 at 13:10
  • 1
    @Rushabh: This won't work, since you cannot place a reactive expression inside a reactiveValue. In this case see my updated answer above.
    – TimTeaFan
    Jul 29, 2019 at 13:42
  • This is helpful !! Jul 29, 2019 at 14:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.