3

My apache passes LDAP login to variable X-Remote-User in header:

enter image description here

but I don't know how to get it in Shiny app. Any ideas? Maybe solution could be some java script?

0

3 Answers 3

3

Ok I resolved it. Firstly include in ui.R js function:

www/js/getLogin.js:

function requestLogin() {
var client = new XMLHttpRequest();
client.open("GET", "yourshinyhostname", true);
client.send();
client.onreadystatechange = function() {
    
var resposne = client.getResponseHeader("X-Remote-User");
Shiny.onInputChange("getLogin", resposne);
}; }

Then you can get X-Remote-User value for example by clicking button:

ui.R:

includeScript("www/js/getLogin.js"),
uiOutput("login_btn"),
verbatimTextOutput("text")

server.R :

output$login_btn <- renderUI({
  
  HREF <- sprintf('
                  <button id="get_login_btn" value="test" onclick="requestLogin();" >
                  <font color="black">
                  <i class="fa fa-user"></i> Get login
                  </font> 
                  </button>
                  ')
HTML(HREF)
}) 

jsOutput <- reactive({
  input$getLogin
})

output$text <- renderPrint({ 
  jsOutput()
})
5
  • Is there a way to do the same thing but without requiring any clicking by the user?
    – wdkrnls
    Commented Oct 15, 2019 at 14:16
  • @wdkrnls you can try something like this gist.github.com/Tazovsky/3e1b68a7fe14dc242b4a85549b2575f2 but I'm not sure it will work.
    – Taz
    Commented Oct 15, 2019 at 14:42
  • Thanks, but so far no luck. I even tried to add a Shiny.setInputValue("getUsername", response); with no success.
    – wdkrnls
    Commented Oct 15, 2019 at 15:01
  • @Taz: what is 'www/js/getLogin.js' ? Shoul I write a seperate js-file?
    – maniA
    Commented Nov 19, 2021 at 8:25
  • @maniA it is JS script starting with function requestLogin() {...
    – Taz
    Commented Nov 29, 2021 at 16:33
0

Add a call to the js function in server.R. This will fetch the header without a button click. For e.g:

output$text <- renderPrint({ 
  js$requestLogin()
  jsOutput()
})
0

You can access header variables with session$request. Here's an interactive example adapted from GitHub.

library(shiny)

server <- function(input, output, session) {
    
    output$summary <- renderText({
        ls(env=session$request)
    })
    
    output$headers <- renderUI({
        selectInput("header", "Header:", ls(env=session$request))
    })
    
    output$value <- renderText({
        if (nchar(input$header) < 1 || !exists(input$header, envir=session$request)){
            return("NULL");
        }
        return (get(input$header, envir=session$request));
    })
    
}

ui <- pageWithSidebar(
    headerPanel("Shiny Client Data"),
    sidebarPanel(
        uiOutput("headers")
    ),
    mainPanel(
        h3("Headers passed into Shiny"),
        verbatimTextOutput("summary"),
        h3("Value of specified header"),
        verbatimTextOutput("value")
    )
)

shinyApp(ui, server)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.