我目前的应用程序有一个使用uiOutputrenderUI动态呈现的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()似乎并不重要;无论哪种方式,我都会收到错误.

我遗漏了什么?

推荐答案

fontawesome包的帮助下,您可以将图标包括在标签中.

不要在观察器中使用render函数,不推荐这样做.

library(shiny)
library(fontawesome)

# Define UI
ui <- fluidPage(
  #Have a button to press
  actionButton("test_button",
               uiOutput("button", inline = TRUE))
)
# 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.
  output$button <- renderUI({
    if(reactives1$icon_switch %% 2 == 0) {
    tags$span("Press the button", fa_i("arrow-left"))
    } else {
    tags$span("Press the button", fa_i("arrow-right"))
    }
  })
}
# Run the application 
shinyApp(ui = ui, server = server)

R相关问答推荐

使用%in%时如何应用多个条件?

用dDeliverr用第二个表更新一个表

创建计数(带重置)变量

强制相关图以显示相关矩阵图中的尾随零

更改网格的crs以匹配简单要素点对象的crs

卸载安装了BRM的模型发出的警告

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

如何按排序顺序打印一个框架中所有精确的唯一值?

将年度数据插入月度数据

隐藏e_mark_line的工具提示

如何使用按钮切换轨迹?

R for循环返回到先前值

用相同方法得到不同函数的ROC最优截断值

线性模型斜率在减少原始数据时提供NA

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

如何将使用rhandsontable呈现的表值格式化为百分比,同时保留并显示完整的小数精度?

在不对R中的变量分组的情况下取两行的平均值

创建在文本字符串中发现两个不同关键字的实例的数据框

使用ifElse语句在ggploy中设置aes y值

抽样变换-REXP与RWEIBUR