What are the best strategies for resolving the problem of shiny misalignment in a datatable?

I am attempting to achieve the following with my datatable:

  • Ensure all columns are uniform width
  • Left-align both the header and content of the datatable
  • Enable horizontal scrolling when it reaches the mainPanel width

However, I'm facing issues where the datatable is automatically centered within the mainPanel and the alignment of the header and content is off.

For example:

library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

   titlePanel("Test Example"), 

   mainPanel(
     width = 10, 
     dataTableOutput("cars.table")
   )
)

server <- function(input, output) {
   output$cars.table <- renderDataTable({
      t(cars[1:10, ]) %>% 
       datatable(class = "compact small", 
                 options = list(columnDefs = list(list(width = "25px", targets = "_all")), scrollX = TRUE, autoWidth = TRUE, 
                                paging = FALSE, ordering = FALSE, searching = FALSE))
   })
}

shinyApp(ui = ui, server = server)

Update 2019/05/03:

It seems that this question suggests that the issue could be due to autoWidth = TRUE. However, there isn't a solution provided in the question. It appears we can't remove autoWidth = TRUE if we need to adjust column width as well.

Answer №1

To ensure proper alignment, you can utilize the className = dt-left.

It appears that setting the argument `fillContainer = T` is responsible for enabling scrolling functionality.

library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

  titlePanel("Test Example"), 

  mainPanel(
    width = 10, 
    dataTableOutput("cars.table")
  )
)

server <- function(input, output) {
  output$cars.table <- renderDataTable({
    t(cars[1:10, ]) %>% 
      datatable(class = "compact small", fillContainer = T, 
                options = list(columnDefs = list(list(className = 'dt-left', width = "25px", targets = "_all")), scrollX = TRUE, 
                               paging = FALSE, ordering = FALSE, searching = FALSE))
  })
}

shinyApp(ui = ui, server = 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

Issues encountered with the .submit() function while making a $.ajax request

Unfortunately, I am unable to provide a jsFiddle link for this specific issue. It seems that the problem lies in passing the correct parameters through the success: function. Interestingly, when running a script on a form, it only submits once. Our goal i ...

Here comes a new vectorized loop in R: YAVL - Yet Another Vector

Currently, I am in the process of learning how to vectorize code using R and would greatly appreciate any links to useful examples. My main goal right now is to optimize a loop that compares values in three different columns (a, b, and c) to find any numbe ...

What is the method for performing a synchronous call using Backbone's fetch function?

Is it possible to make a synchronous call using the fetch function in JavaScript? I know with jQuery ajax, you can use {async: false}. Can this option be passed to the fetch function? ...

The Selenium tool is failing to activate the cookie acceptance button

Having trouble getting Selenium to click on the cookie accept button. Can anyone provide assistance? driver = webdriver.Chrome(path) driver.get('https://www.bisnode.de/upik/') try: buttonlink = WebDriverWait(driver, 10).until( EC.elem ...

Tips for accessing a jQuery tab UI that is on a separate page by utilizing AJAX techniques

My project is a web application built in Eclipse using JSP, AJAX, and jQuery. It consists of two main JSP pages - home.jsp and second.jsp. Additionally, there are separate folders for css and js files. In second.jsp, I have implemented tab panels using di ...

When using jQuery each method, it may return an [object Object]

Having an issue with the html variable displaying [object Object] instead of actual elements. Any suggestions on what I should change? var html = ''; $.each(data.response, function(index, value) { var tr = $('<tr>'); var ...

Embedding a Bootstrap Popover

I am struggling to hide the popover when clicking outside the modal. I have been able to hide it on close, but not when clicking outside the modal. Here is the code snippet: $('.close, .close-button').click(function(){ $(".popover.bo ...

What is the best way to dynamically adjust the hover color of a bar in a Highchart

The below code is used to set the hover color of the bar: plotOptions: {column: {states: {hover: {color: '#000000'}}}} Is there a way to dynamically change the bar hover color? ...

Is there a way to make a button on a single div only affect that specific div

I have a PHP query that echoes a div for each row in the table. I want the div to slide up and then, when the user clicks the "read more" button, the div slides down. However, since it is echoed in a loop, all the divs have the same IDs and classes. I wo ...

Can the .scroll function be turned off when a user clicks on an anchor link that causes the page to scroll?

I am currently developing a timeline page and I want to implement a feature similar to the chronological list of years displayed on the right side of this webpage: As part of this feature, I have set up a click event which adds a circle border around the ...

adjusting three sections within a flexible navigation bar

Having difficulties creating a dynamic navbar that changes on scroll and keeping the elements within the nav fixed when the menu options appear. For reference, you can check out this codepen: http://codepen.io/timothyfernandez/pen/azXQPV Any assistance w ...

ReactStrap: Rows and columns causing components to stack on top of each other

When I opt not to utilize Row and Col: <FormGroup> <InputGroup> <InputGroupAddon addonType="prepend"> <InputGroupText> ...

Utilizing jQuery Ajax to simultaneously insert and fetch data upon submission, with the fetched data automatically posted on the URL opened by window.open

When the #user_form_cert is submitted, the data will be inserted and fetched at the same time. I have used this code where the insert works fine but the fetch is not working and shows undefined on the window.open URL. $(document).on('submit', &a ...

Angular material tables are displaying borders that do not extend the full length

I am currently working on designing a visually appealing table. Due to the upcoming content in the matrix section, I am unable to make the table smaller for mobile devices. Therefore, I have added overflow:auto to enable a scroll-bar. The issue I am facin ...

Resizing the parent container when the height of its absolutely positioned children is unknown and dynamic upon window resize

https://codepen.io/umbriel/pen/MxazRE One interesting feature of my slideshow is that users can change the slide by swiping their mouse cursor horizontally over the slide container. Since the children (slides) are absolutely positioned, I faced a challen ...

Placing a pseudoelement amidst the track and thumb of a range input type

I'm trying to position a pseudo element with a specific z index that is lower than the thumb of an input type range but higher than its track. This is what I have so far: HTML: <div class="range"> <input type="range" name="" class="progre ...

Choosing the year and month from the datepicker in Selenium Webdriver: A step-by-step guide

I am attempting to choose the Year and Month from a drop-down menu that appears as a datepicker. I am having trouble selecting it by ID, as it is being selected by class instead. Can someone please provide me with a sample Java code using the JavaScript ex ...

Remove a row if it meets certain conditions related to a separate identification code

I am working with a dataset that contains multiple IDs, and my goal is to selectively modify rows with IDs 7 or 9 while leaving the rest untouched. Specifically, I want to remove a row from ID 7 or 9 if there is no corresponding variable for it. For examp ...

The contact form is set up to send emails through contact.php, but I prefer not to have it redirect to another page. Sending emails works

Don't worry, I have already searched many places and tried to watch other forums and videos. I'm not looking for an easy solution. When I submit a form on my page it redirects to www.mypag.com/contact.php. I'm trying to use AJAX so that the ...

Save the values of the first and second characters in two separate variables

Can someone help me with storing the first and second characters of a value in separate variables, id and ccMain, using either jQuery or JavaScript? Below is the HTML code needed to set up the environment: <span id="ccMain">GM</span> ...