【问题标题】:R Shiny - Reset renderTextR Shiny - 重置渲染文本
【发布时间】:2020-04-26 07:52:58
【问题描述】:

在下面的代码中,目标是在 button1 被点击后根据所选值重置 textoutput 的值:

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)

按原样运行代码并单击按钮,文本变为You clicked the button.。我遇到问题的部分是当下拉列表的值更改为“b”或“c”(单击按钮后)时,文本不会改变。我知道为什么它没有改变,但无法提出解决方案。

【问题讨论】:

    标签: r shiny shinyjs


    【解决方案1】:

    我认为您需要一个 output$textoutput,它会根据输入的各种变化进行更新。

    也许reactive 值可能包含您希望呈现的消息。例如:

    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")
      })
    
    }
    

    这是否具有您正在寻找的行为?

    【讨论】:

    • 太棒了!这正是我正在寻找的行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2021-03-20
    • 2021-11-04
    • 2021-02-05
    • 2019-03-07
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多