Convert server file into HTML

Hi all,

Wanted to check if I can convert server.R file into HTML. Sample server file below

server.R

library(shiny)

shinyServer(function(input, output) {

  output$greeting <- renderText({
    paste0("Hello, ", input$name, "!")
  })
  print(getwd())
  })

Hi,

Could you explain in a bit more detail what the goal of this is an what you mean by 'convert into HTML'. Do you want to see the exact same text and formatting in HTML form, or do you want to see the html that is generated by some of the functions?

In Shiny, the HTML is all inside the UI function. The server can generate HTML, but it will be put into the UI once it's finished. You can run your app, right-click in the window and click 'view page source' if you like to see all HTML.

Hope this helps,
PJ

1 Like

Hi,

Could you please elaborate on what the purpose of this is and how you want to use it. If this is possible, it really depends on how you want to use it and in which context

PJ

Hi,

I'm sorry but this is all still very confusing. I don't see what you want to accomplish here.
First of all, you declare all inputs yourself, so you can just write them out in your message:

observeEvent(input$button1, {    
    showModal(modalDialog(
      title = "Button pressed",
      "you pressed button 1, but here is a list of all inputs: button1, button2"
    ))
  }, ignoreInit = T)

If you don't want to write them out (in case of many), you can list all the inputs as well:

 # observe button 1 press.
  observeEvent(input$button1, {
    showModal(modalDialog(
      title = "Button pressed",
      paste("You pressed button 1, but here is a list of all inputs:", 
            paste(names(input), collapse = ", "))
    ))
  }, ignoreInit = T)

Neither case will generate an action if you press button 2 through, it's just sitting there as a dummy.
In case you want to do the latter you can so this:

 observeEvent(c(input$button1, input$button2), {
    showModal(modalDialog(
      title = "Button pressed",
      paste("You pressed a button, but here is a list of all inputs:", 
            paste(names(input), collapse = ", "))
    ))
  }, ignoreInit = T)

Or if you want to go crazy:

buttonClicked = reactiveValues(btn1 = 0, btn2 = 0)

 observeEvent(c(input$button1, input$button2), {
    
    if(input$button1 > buttonClicked$btn1){
      buttonClicked$btn1 = buttonClicked$btn1 + 1
      myButton = 1
    } else {
      buttonClicked$btn2 = buttonClicked$btn2 + 1
      myButton = 2
    }
    
    showModal(modalDialog(
      title = "Button pressed",
      paste("You pressed button", myButton, ", but here is a list of all inputs:", 
            paste(names(input), collapse = ", "))
    ))
  }, ignoreInit = T)

Hope any of these will help :slight_smile:
PJ

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

Hi ,

Thanks for the time.

Do you want to see the exact same text and formatting in HTML form, or do you want to see the html that is generated by some of the functions?

Yes. Can we have both??:blush:

Great. Happy to explain.

If you observe the server file above, there is 1 output called greetings and this is been rendered based on input called name. So I am planning to create logic, where it pairs both input and output id's.

Another example.

We have called observerEvent to click a actionbuttonand inside this function we have multiple input id's . So the logic I create should automatically calculate under this actionbutton , what are inputs id's are declared.

Makes sense ?:slight_smile:

Hi,

Thanks for the explanation, but I'm not with you yet :slight_smile: Why don't you create a reprex for me so we can work from that. Shiny has a lot of magic hidden in its code and it seems unlikely you'd need the raw server or UI code to figure out stuff that's going on.

Please use this guide and don't forget to describe the problem and what you want as well:

Good luck :slight_smile:
PJ

Hi. No problem

Below is the reprex. Let me explain in detail. If you observe server section, inside observeEvent we have input$button1 as a first argument and in the second argument there is no inputs declared. So for this, I am expecting under observeEvent what all inputs we have declared. Hope this helps. Thanks. Not sure if there is way to find other converting to HTML. In case if we convert HTML, we can easily find what comes after specific word right? This is my thinking. Please share your thoughts as well

library(shiny)

ui <- fluidPage(
  headerPanel("Example reactive"),
  
  mainPanel(
    
    # action buttons
    actionButton("button1","Button 1"),
    actionButton("button2","Button 2")
  )
)

server <- function(input, output) {
  
  # observe button 1 press.
  observeEvent(input$button1, {
    # The observeEvent takes no dependency on button 2, even though we refer to the input in the following line.
    showModal(modalDialog(
      title = "Button pressed",
      "You pressed one of the buttons!"
    ))
  })
}

shinyApp(ui = ui, server = server)