R Shiny - Reset renderText

413 Views Asked by At

In following code, the goal is to reset the value of textoutput based on the selected value AFTER button1 is clicked:

library(shiny)
library(shinythemes)
library(DT, warn.conflicts = FALSE) #shiny also uses: dataTableOutput & renderDataTable
library(shinyjs)
library(shinyBS) #for tooltips on an input or output.
library(htmltools)

ui <- fluidPage(theme = shinytheme("lumen"),

                useShinyjs(),

                titlePanel(""),

                selectInput(
                  inputId = "test1",
                  label = strong("pick something"),
                  choices = c("a - button shows", "b - button hidden", "c - button hidden"),
                  selected = "b",
                  selectize = TRUE,
                  width = "175px",
                  multiple = FALSE),

                hidden(actionButton("button1", "Click Me")),

                htmlOutput(outputId = "textoutput")
)

server <- function(input, output, session) {

  output$textoutput <- renderText({

    if (input$test1 == "a - button shows"){
      print("a")
      shinyjs::show("button1")
      paste("<i><span style='color:blue'>You chose a. You should click the button.</span></i>")
    }

    else if (input$test1 == "b - button hidden"){
      print("b")
      shinyjs::hide("button1")
      paste("<i><span style='color:green'>You chose b. No button.</span></i>")
    }

    else {
      shinyjs::hide("button1")
      print("c")
      paste("<i><span style='color:orange'>The button is hidden.</span></i>")
    }

  })

  observeEvent(input$button1,{
    print(input$test1)
    output$textoutput <- renderText({ paste("<i><span style='color:purple'>You clicked the button.</span></i>")})
    shinyjs::hide("button1")
  })

}

# Create Shiny object
shinyApp(ui = ui, server = server)

Running the code as is and clicking the button, the text changes to You clicked the button.. The part I'm having trouble with is when the value of the drop down changes to "b" or "c" (after clicking the button), the text does not change. I know why it's not changing, but haven't been able to come up with a solution.

1

There are 1 best solutions below

1
On BEST ANSWER

I think you would want a single output$textoutput that is updated based on various changes in inputs.

Perhaps a reactive value could contain the message you wish to render. For example:

server <- function(input, output, session) {

  msg_txt <- reactiveVal("")

  observe({
    if (input$test1 == "a - button shows"){
      print("a")
      shinyjs::show("button1")
      msg_txt("<i><span style='color:blue'>You chose a. You should click the button.</span></i>")
    }

    else if (input$test1 == "b - button hidden"){
      print("b")
      shinyjs::hide("button1")
      msg_txt("<i><span style='color:green'>You chose b. No button.</span></i>")
    }

    else {
      shinyjs::hide("button1")
      print("c")
      msg_txt("<i><span style='color:orange'>The button is hidden.</span></i>")
    }

  })

  output$textoutput <- renderText({ msg_txt() })

  observeEvent(input$button1,{
    print(input$test1)
    msg_txt("<i><span style='color:purple'>You clicked the button.</span></i>")
    shinyjs::hide("button1")
  })

}

Does this have the behavior you were looking for?