Slides on xaringan with the ability to scroll horizontally

I am interested in displaying the output of an R command within a horizontally scrolling box. See below for an example:

library(ggplot2movies)
head(movies)
#                      title year length budget rating votes   r1   r2  r3   r4   r5   r6   r7   r8   r9  r10 mpaa Action Animation Comedy Drama Documentary Romance Short
# 1                        $ 1971    121     NA    6.4   348  4.5  4.5 4.5  4.5 14.5 24.5 24.5 14.5  4.5  4.5           0         0      1     1           0       0     0
# 2        $1000 a Touchdown 1939     71     NA    6.0    20  0.0 14.5 4.5 24.5 14.5 14.5 14.5  4.5  4.5 14.5           0         0      1     0           0       0     0
# 3   $21 a Day Once a Month 1941      7     NA    8.2     5  0.0  0.0 0.0  0.0  0.0 24.5  0.0 44.5 24.5 24.5           0         1      0     0           0       0     1
# 4                  $40,000 1996     70     NA    8.2     6 14.5  0.0 0.0  0.0  0.0  0.0  0.0  0.0 34.5 45.5           0         0      1     0           0       0     0
# 5 $50,000 Climax Show, The 1975     71     NA    3.4    17 24.5  4.5 0.0 14.5 14.5  4.5  0.0  0.0  0.0 24.5           0         0      0     0           0       0     0
# 6                    $pent 2000     91     NA    4.3    45  4.5  4.5 4.5 14.5 14.5 14.5  4.5  4.5 14.5 14.5           0         0      0     1           0       0     0

Is there a way to create horizontal scrolling for this output on a xaringan slide?

Answer №1

The answer provided by @Yihui Xie can be found on Github. I have taken that answer and created a working example here, with some key points to keep in mind:

1) You can specify CSS in code chunks in Rmarkdown or create your own CSS file following the guidelines at https://github.com/yihui/xaringan/wiki. Since this may be a one-time task, I am including the CSS directly in the Rmd file for simplicity.

2) In addition to setting attributes for the pre element, you also need to adjust the width option in R to a larger value to prevent wrapping of the output by head.

---
title: "Horizontal scroll for wide output"
output:
  xaringan::moon_reader:
    css: ["default"]
    nature:
      highlightLines: true
---

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)


{css, echo=FALSE}
pre {
  background: #FFBB33;
  max-width: 100%;
  overflow-x: scroll;
}


{r}
library(ggplot2movies)
op <- options("width"=250) # trick head into not wrapping text
head(movies)
options(op) # reset options back to default


---

{r}
head(movies) # default width causing text wrap, but you can still horizontally scroll due to the `pre` setting

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

Unlimited icon carousel crafted with CSS

Currently, I have a set of 10 icons that are scrolling horizontally from right to left. However, I am encountering an issue where at the end of the icon loop, there is a space causing the icons to jump back to the beginning. I am looking for a solution to ...

Owl carousel issue: Fixed position not functioning properly

To view the page with the issue, click here I am attempting to make the column (div) day/date headers sticky when they scroll out of view so that users can always see what day they are looking at. Instead of directly manipulating the header itself, I have ...

Utilizing jQuery, a captivating slideshow showcasing a series of images elegantly emerges from the center-right of

I've been working on creating a slideshow of images and I came across this tutorial: After downloading the zip file and getting started, everything was working fine. The only issue I encountered was trying to position those four dots at the bottom ri ...

Press anywhere outside the container to conceal it along with the button

Utilizing an Angular directive to hide div elements when the user interacts outside of them has been effective. However, there is a specific issue that arises when clicking outside of a div on a button that toggles the visibility of the div. The 'ang ...

The issue of Jquery .click event handler failing to work for class selector specifically in CHROME

Is there a way to achieve the same opacity effect on click as on mouseover? I've tried various selectors without success. Any help on this issue would be greatly appreciated. Thank you in advance! .container { position: absolute; background:url(&a ...

Troubleshooting issue: Bootstrap 4 input group not properly showing invalid feedback text

I've been exploring the features of Bootstrap 4 - beta and I've noticed that when using .is-invalid with .input-group, it doesn't seem to be visible. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.cs ...

Is there a way to ensure uniform font display across all browsers?

Is there a way to ensure consistent font display across all browsers? I am facing an issue with font consistency on my webpage. While browsers like Internet Explorer Edge and 11, Chrome, and Firefox display the desired font correctly, Internet Explorer 8 ...

Adaptable images - Adjusting image size for various screen dimensions

Currently, my website is built using the framework . I am looking for a solution to make images resize based on different screen sizes, such as iPhones. Can anyone suggest the simplest way to achieve this? I have done some research online but there are t ...

Maximize margin usage by extending to full width

Check out this JS Fiddle example Is there a way to align these boxes in the example so they are the same size and positioned next to each other on one line? Additionally, how can we ensure that if the window is resized and one box drops down, it will be c ...

Avoiding the text being emphasized when hovering over the navigation bar list

Whenever I hover over the text in the navigation bar, the space above the text is highlighted instead of the text itself. This issue could be related to my CSS code: ul { list-style-type: none; margin: 0; padding: 0; width: 200px; background-color: silver ...

When hovering over the menu, the page's opacity decreases to 0.5

Feeling lost on how to create a navigation menu that reduces the transparency of the entire page to 50% when hovered over. Any advice on the CSS code needed for this scenario? Check out jsfiddle.net/websensation/uMMPa for more insights. ...

Implementing the 'bootstrap tour' feature in a Ruby on Rails application

I have integrated bootstrap-tour.min.css and bootstrap-tour.min.js into my project. <script type="text/javascript" src='bootstrap-tour.min.js'></script> <link rel="stylesheet" type="text/css" href="bootstrap-tour.min.css"> Her ...

Using the MVC framework, create a hyperlink with custom CSS styling by

Having trouble getting a specific class to override the default styles for a:link @Html.ActionLink("Back to List", "Index", null, new { @class = "htmlactionlink" }) CSS a:link, a:visited, a:active, a:hover { color: #333; } .htmlactionlink { co ...

Every outcome was displayed within a table format rather than as individual rows

I'm having trouble with my PHP code for search. The result is not displaying in a single table with rows, as expected. I attempted to modify the PHP by changing `'<td>','<div>' . $row['$accountcode']. '&l ...

AngularJS tiger striping for a hidden rows table

I'm facing an issue where I need to add zebra stripes to a table with hidden rows using ng-hide. The problem is that when I try to stripe the rows with either CSS3 or jQuery, the coloring does not work correctly. You can see an example below. In this ...

What could be causing the hover effect to malfunction in this code snippet?

I have been working on creating a hover effect for a list of items where an underline emerges from the center. While I have done something similar in the past, there seems to be an issue preventing it from working properly this time. I have included the HT ...

Establishing updated file path for resources in JavaScript based on environment

I'm currently facing an issue with my external Javascript file that uses the getScript() function to execute another JS file. Both files are located on static.mydomain.com, as I am trying to set up a Content Delivery Network (CDN) for the first time. ...

What is the best way to ensure that the question text will wrap onto a new line if it becomes too lengthy

I am currently working on developing ASP.NET MVC applications. However, I am facing a design issue with the HTML, CSS, and Bootstrap elements on one of my pages. The problem arises when Question 8 exceeds the border and does not wrap to a new line as inte ...

Configuring local fonts in Puppeteer using npm

Looking to incorporate locally loaded fonts when generating a PDF using the Puppeteer node module. I successfully implemented web fonts, but this does not meet the specified requirements. Environment Details: Node: v10.18.1, Puppeteer: "puppeteer": "^2 ...

What is the best way to align two buttons horizontally together?

I've created an HTML form using JavaScript, but I'm facing an issue where the cancel and submit buttons are displayed on separate lines. How can I align them horizontally? Here is the code snippet I'm currently working with: var new_commen ...