Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny: how to give an inputID to an actionButton created via HTML

Tags:

html

r

shiny

If I create a .htlm file to define an actionButton, how can I give an inputID to the button (in order to do other things based on its value) ?

As you will see in the .html, I try to give it an "id" but it does not work. And I believe that actually giving an inputID should be done in the ui.R file.

Here are my UI, server, and .htlm file:

1/ui:

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

sidebarPanel(),

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

2/server:

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> 

Any suggestion would be highly appreciated.

Cheers

like image 600
user1431694 Avatar asked Mar 20 '23 09:03

user1431694


1 Answers

I think the problem is that your html is creating a plain vanilla "button", not a shiny "action button". If your goal is to create an action button with custom classes, you can do it with:

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

This is equivalent to a call to actionButton(...) but allows you to set the class=... attribute explicitly.

So this code produces a page with an action button styled as in your static.html file.

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

As you can see, you can reference it in shinyServer using input$button1. I'd also recommend you load the most recent version of package shiny.

EDIT (Response to @JulienNavarre comment)

Turns out this works as well:

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 you change static.html as follows:

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

In other words, you have to include action-button in the class list, and remove the <p></p> tags.

like image 182
jlhoward Avatar answered Mar 22 '23 23:03

jlhoward