R shiny:如何为通过HTML创建的actionButton提供inputID

时间:2014-03-01 15:29:41

标签: html r shiny

如果我创建一个.htlm文件来定义一个actionButton,我如何给按钮一个inputID(为了根据它的值做其他事情)?

正如您将在.html中看到的,我尝试给它一个“id”,但它不起作用。我相信实际给出一个inputID应该在ui.R文件中完成。

以下是我的UI,服务器和.htlm文件:

1 / UI:

library(shiny)
shinyUI(pageWithSidebar(
headerPanel("test"),

sidebarPanel(),

mainPanel(
includeHTML("static.html"),
uiOutput("x")
)
))

2 /服务器:

shinyServer(function(input,output){
output$x<-renderUI(h4(input$button1))            
})

3 / static.html

<p> <button class="btn btn-large btn-primary" type="button" id="button1"><i class="icon-star"></i>Large button</button>

</p> 

任何建议都将受到高度赞赏。

干杯

1 个答案:

答案 0 :(得分:6)

我认为问题在于你的html正在创建一个普通的“按钮”,而不是一个闪亮的“动作按钮”。如果您的目标是创建一个包含自定义类的操作按钮,则可以使用:

tags$button(id="button1", 
            type="button", 
            class="btn action-button btn-large btn-primary", 
            HTML('<i class="icon-star"></i>Large button'))

这相当于对actionButton(...)的调用,但允许您明确设置class=...属性。

因此,此代码会生成一个页面,其中的操作按钮的样式与static.html文件一样。

library(shiny)
shinyUI = pageWithSidebar(
  headerPanel("test"),
  sidebarPanel(HTML(paste('<p>Click Count: ',textOutput("count"),'</p>'))),
  mainPanel(
#    includeHTML("static.html"),
#    uiOutput("x"),
    tags$button(id="button1", 
                type="button", 
                class="btn action-button btn-large btn-primary", 
                HTML('<i class="icon-star"></i>Large button'))
  )
)
shinyServer = function(input,output){
 # output$x     <- renderUI(input$button1)
 output$count <- renderText(input$button1)
}

runApp(list(
  ui = shinyUI,
  server = shinyServer
))

如您所见,您可以使用shinyServerinput$button1中引用它。我还建议您加载最新版本的包shiny

编辑(对@JulienNavarre评论的回应)

结果证明这也有效:

library(shiny)
shinyUI = pageWithSidebar(
  headerPanel("test"),
  sidebarPanel(HTML(paste('<p>Click Count: ',textOutput("count"),'</p>'))),
  mainPanel(
        includeHTML("static.html")
  )
)
shinyServer = function(input,output){
  output$count <- renderText(input$button1)
}

runApp(list(
  ui = shinyUI,
  server = shinyServer
))

IF ,您更改static.html,如下所示:

<button class="btn action-button btn-large btn-primary" type="button" id="button1">
   <i class="icon-star"></i>
      Large button
</button>

换句话说,您必须在班级列表中加入action-button,然后移除<p></p>代码。