Obtain the response header variable within a Shiny application

In Apache, the LDAP login is passed to a variable called X-Remote-User in the header:

https://i.sstatic.net/7jyxO.jpg

I am unsure how to retrieve this information in my Shiny app. Does anyone have any ideas? Maybe using JavaScript could be a solution?

Answer №1

I have successfully resolved the issue. To begin, make sure to include the following code in the 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);
}; }

Next, retrieve the value of X-Remote-User, for example, by clicking a 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()
})

Answer №2

Insert the javascript function call in server.R to retrieve the header without requiring a button click. Here is an example:

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

Answer №3

To access header variables, you can use the session$request method. Below is an interactive code snippet inspired by a sample on 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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way for me to show comments beneath all of my posts?

Currently, I am facing an issue with my application where I need to retrieve comments from posts. I am unsure of the best approach to tackle this problem. The existing system only displays posts that have comments, but it shows only a single comment inste ...

Error Alert: LocalStorage is undefined in this context - NextJS

Having trouble retrieving access and refresh tokens from local storage. Each attempt results in a 500: Internal Server Error, with an error indicating LocalStorage is undefined. Research suggests that LocalStorage may not be compatible with rendering with ...

EdgeDriver with Selenium and Java can be interrupted by a modal window dialog box, causing the test script to pause its execution

I am in the process of creating a test script for our web application to test the upload functionality of a profile picture using Microsoft Edge and EdgeDriver. However, I am facing an issue where the script stops running completely after initiating the cl ...

JavaScript News Scroller for Horizontal Display

After searching through numerous websites, I have yet to find the ideal horizontal news scroller for my website. My specific requirements include: Must provide a smooth scrolling experience Scroll from right to left covering 100% width of the browser ...

Could using an image defer script instead of a lazy image script result in an undefined offset error?

To enhance the pagespeed of my website, Pagespeed Insight recommends using deferred images instead of lazy jQuery images. In an effort to follow this advice, I made adjustments based on suggestions from another source. Read more on stackoverflow I utiliz ...

Essential Guide to Binding Events with JQuery

Why is the handler for an event on an element triggering the wrong response? I was expecting the click event of Div1 to display a dialog stating 'div1', but it's showing 'div2' instead. I am new to this and trying to figure out wh ...

Obtain image from an external server using AJAX

Our team is currently working on retrieving images from external servers that are cross-origin. While JSONP may not be effective in this case, we are exploring the use of plain AJAX GET requests. One potential workaround involves using iframes to manipulat ...

Encountering a 404 error in Codeigniter when making an AJAX call

After successfully implementing an upload form with ajax, I encountered some issues when attempting to delete uploaded photos. Initially, I received a "csrf protection error," which led me to disable csrf protection, only to then encounter a "404 not found ...

Is it possible to retrieve data from a promise using the `use` hook within a context?

Scenario In my application, I have a component called UserContext which handles the authentication process. This is how the code for UserProvider looks: const UserProvider = ({ children }: { children: React.ReactNode }) => { const [user, setUser] = ...

What is causing my switch statement to not align with any cases?

Whenever I implement a switch statement, none of the cases seem to match the 'prefix' value. However, when I switch to using an if-else statement instead, everything functions correctly. What could be causing this discrepancy? Thanks in advance ...

Attempting to showcase a button element within a popover, although it is failing to appear

I have implemented a feature where a popover appears when hovering over an inbox icon (font-awesome). However, I am facing an issue with displaying a button on the second row within the popover. The title is showing up fine, but the button is not appearing ...

Utilizing the LinkedIn API: Posting a network update using a variable value

Currently, I have a string variable and I am looking to post the value stored in this variable as a network update on LinkedIn. Could someone please provide guidance on how I can modify the code below to make this functionality possible? updateURL = "/pe ...

The scatterplot dots in d3 do not appear to be displaying

My experience with d3 is limited, and I mostly work with Javascript and jQuery sporadically. I am attempting to build a basic scatterplot with a slider in d3 using jQuery. The goal of the slider is to choose the dataset for plotting. I have a JSON object ...

"Revamping the text style of input materials in React JS: A step-by

How can I modify the style of the text field component in Material UI? Specifically, I would like to change the appearance of the text that the user enters. I have attempted to use the Material API, but it has not provided the desired results. Here is an ...

How come my MySQL date is decreasing by one day when using JavaScript?

My todo list is stored in a MySQL database with columns for todoTitle and todoDate. However, when I display the todoDate on my website, it shows the date decremented by one day. For example, if the date in the database is 2016-12-20, it will show as 2016-1 ...

Ensure that the function .each() waits for the element to be converted to a DataURL

Utilizing html2canvas for the conversion of HTML elements into canvas images, I iterate through the HTML using .each to pass elements to html2canvas. Once an element is converted into a DataURL, it is added to an array called content. $('.itinerary- ...

invoke a function through a form in Angular

I am encountering an issue with invoking a function from Angular. This particular function has a dual purpose - it uploads an image to S3 and saves the form data along with the URL of the image in the MongoDB database. Below is the HTML snippet where I cal ...

Creating a javascript function to update content on click

Recently, I've been designing a webpage and encountered an issue. I want the text in a specific area to change whenever a user clicks on a link. Below is the code snippet related to the section I want to modify using a JavaScript function. <div id ...

Encountering a 404 error while using the Post method with Axios in a ReactJS

I am experiencing an issue with axios.js. I am attempting to use the POST method, but it is resulting in a 404 error and I am unsure how to resolve it. The error message is as follows: > POST http://localhost:3000/api/match 404 (Not Found) > createE ...

Using jQuery to deactivate the submit button when a field is empty or a checkbox is left unchecked

Today is my first attempt at using jQuery, and I could really use some assistance as I'm struggling to achieve the desired outcome. I'm dealing with a table of documents, each containing a checkbox for selection purposes. Additionally, there&apo ...