我目前的应用程序有一个使用uiOutput
和renderUI
动态呈现的actionButton
,因此它的标签会根据用户的活动而变化.这是一个非常愚蠢的重述:
library(shiny)
# Define UI
ui <- fluidPage(
#Have a button to press
actionButton("test_button",
uiOutput("button_label"),
icon = icon("forward"))
)
# Define server
server <- function(input, output) {
#Have a reactive value serving as a switch
reactives1 <- reactiveValues(label_switch = 0)
#Increment the switch upon every button press
observeEvent(input$test_button, {
reactives1$label_switch = reactives1$label_switch + 1
})
#Depending on whether the switch is odd or event, dynamically render the button label.
observeEvent(reactives1$label_switch, {
if(reactives1$label_switch %% 2 == 0) {
output$button_label <- renderUI({ "I'm happy!" })
} else {
output$button_label <- renderUI({ "I'm sad!" })
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
然而,令人沮丧的是,我似乎想不出如何用按钮上的图标做同样的事情.例如,这不起作用:
library(shiny)
# Define UI
ui <- fluidPage(
#Have a button to press
actionButton("test_button",
"Press this button to change the icon!",
uiOutput("button_icon"))
)
# Define server
server <- function(input, output) {
#Have a reactive value serving as a switch
reactives1 <- reactiveValues(icon_switch = 0)
#Increment the switch upon every button press
observeEvent(input$test_button, {
reactives1$icon_switch = reactives1$icon_switch + 1
})
#Depending on whether the switch is odd or event, dynamically render the button icon.
observeEvent(reactives1$icon_switch, {
if(reactives1$icon_switch %% 2 == 0) {
output$button_icon <- renderUI({ shiny::icon("arrow-left") })
} else {
output$button_icon <- renderUI({ shiny::icon("arrow-right") })
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
R SHINY似乎没有意识到正在调用icon()
;我收到了一个错误,我需要使用shiny::icon()
来呈现图标,尽管这就是我正在做的事情.我已经看到了here个我可能需要动态呈现整个按钮的例子,但我宁愿不这样做,除非迫不得已.它也不能解释为什么我可以使用按钮标签而不是按钮图标来使用这个技巧.我在其他地方看到,renderUI()
调用可能覆盖了我的图标对象的类/形式,但在哪里调用icon()
似乎并不重要;无论哪种方式,我都会收到错误.
我遗漏了什么?