1

This is my code:

card_function <- function(title,paragraph,tags){
  div(
    class = "card",
    div(class = "header"),
    div(
      class = "info",
      p(class = "title", 'title'),
      p('paragraph')
    ),
    div(
      class = "footer",
      p(class = "tag", 'tags'),
      tags$button(id = "action", "Get started")
    ))
}

card_function(title = 'Titulo',paragraph = 'paragrafo',tags = 'HTML')

This is my error message:

Error in tags$button : $ operator is invalid for atomic vectors

I already tried to remove tags from tags$button but it didnt work.

Any help?

2
  • 5
    This is because you have defined tags = "HTML" as an argument in your function. R does not seem to work out that tags$button(id = "action", "Get started") refers to shiny::tags$button(). Call the argument tag_names or something and it should be fine.
    – SamR
    Commented Jan 5 at 12:06
  • 3
    ... or use shiny::tags$button inside your function
    – r2evans
    Commented Jan 5 at 12:25

1 Answer 1

3

Either u can use tags$button syntax directly or simply use the div function to create a button-like element. I replaced button(id = "action", "Get started") with div(id = "action", "Get started", class = "button"). This should work regardless of whether you are using Shiny or not.

card_function <- function(title, paragraph, tags) {
  div(
    class = "card",
    div(class = "header"),
    div(
      class = "info",
      p(class = "title", title),
      p(paragraph)
    ),
    div(
      class = "footer",
      p(class = "tag", tags),
      div(id = "action", "Get started", class = "button")
    )
  )
}

# Example usage
card_function(title = 'Titulo', paragraph = 'paragrafo', tags = 'HTML')

using library as directed by @SamR, you can avoid the conflict with the tags object from the shiny package, either by changing the argument name from tags to tag_names, or by using shiny::tags$button as directed by @r2evans, Here I changed tags to tag_names.

library(shiny)
card_function <- function(title, paragraph, tag_names) {
  div(
    class = "card",
    div(class = "header"),
    div(
      class = "info",
      p(class = "title", title),
      p(paragraph)
    ),
    div(
      class = "footer",
      p(class = "tag", tag_names),
      tags$button(id = "action", "Get started", class = "button")
    )
  )
}

# Example usage
card_function(title = 'Titulo', paragraph = 'paragrafo', tag_names = 'HTML')

Not the answer you're looking for? Browse other questions tagged or ask your own question.