Pavel
Pavel

Reputation: 69

In R using Shiny: how to make a weblink work in the output

I'm using RStudio's Shiny to make a basic MBTI personality test. A user answers four questions, and gets his personality type (e.g. ENTJ) with a corresponding link to Wikipedia to read more on his type (e.g. https://en.wikipedia.org/wiki/ENTJ).

To do this, first, I'm using actionButton, as described here. Second, I'm using a bunch of functions in server.R to make a working weblink:

shinyServer(function(input, output) {

    # After the Submit button is clicked 
    init <- reactiveValues()
    observe({
            if(input$submit > 0) {
                    init$pasted <- isolate(paste("Your personality type is ", 
                                                 input$b1, input$b2, input$b3, input$b4, sep=""))
                    init$link <- paste("https://en.wikipedia.org/wiki/", 
                                               input$b1, input$b2, input$b3, input$b4, sep="")
                    init$linktext <- a("Find out more about it here", href=init$link, target="_blank")
            }
    })

    # Output
    output$text1 <- renderText({ 
            init$pasted
    })

    output$text2 <- renderText({ 
            init$linktext
    })

})

The problem is that, when I run the app, init$pasted works just fine, while init$linktext doesn't - saying

    Error in cat(list(...), file, sep, fill, labels, append) : 
  argument 1 (type 'list') cannot be handled by 'cat'

Any ideas how to fix that? Thanks!

Upvotes: 3

Views: 1407

Answers (1)

NicE
NicE

Reputation: 21425

The output of a(...) is a list and cannot be rendered using renderText. You can use htmlOutput in the ui.R and renderUI on the server side, here's an example:

server <- function(input, output) {
        output$html_link <- renderUI({
                a("Find out more about it here", href=paste("https://en.wikipedia.org/wiki/","a","b","c","d", sep=""), target="_blank") 
        })
}

ui <- shinyUI(fluidPage(
        htmlOutput("html_link")
))

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions