hejseb
hejseb

Reputation: 2164

HTML code that changes with user's input in Shiny

Here's the basics of my problem:

  1. In simplified terms, suppose that the user in ui.R gets to insert the name of an artist using a regular text input. This is then saved in input$artist_name.
  2. Switching to server.R, some stats about the artist's top track are then displayed using some intermediate calls and computations. I then use renderPlot()to put a plot in output$Plot.
  3. Here is where I run into trouble. I would like to include a player (using just basic HTML, this is not the problem) which allows the user to play the track. However, it seems to me as if I need to use the information I got in server.R (since here is where I learn what the most popular track is and, subsequently, its URL) to "push" the URL of the track back to ui.R so that I can include the player in a panel using the HTML() function. Is this possible? To me, it kind of seems as if what I want to do is similar to what you can do with uiOutput() and renderUI(), although obviously not for making a custom UI depending on the user's input but rather some custom HTML code depending on the input.

Any help is greatly appreciated!

Upvotes: 1

Views: 852

Answers (1)

Carl
Carl

Reputation: 5779

I am not sure exactly what you are asking, but it is not too hard to create arbitrary HTML in the server and send it to UI. Here is an example:

library(shiny)

ui =fluidPage(
    textInput("test","Write here"),
    uiOutput("example")
  )

server=shinyServer(function(input, output, session) {


    output$example <- renderUI({
      HTML(paste0("<div style='color:red;'>",input$test,"</div>"))
    })

})

shinyApp(ui=ui,server=server)

Upvotes: 2

Related Questions