Search code examples
rrstudio

RStudio Visual editor - data.frame preview too narrow, does not resize


I'm trying to use the RStudio Visual Editor more for Quarto notebooks.

I really like it, but I there's an issue that makes it unusable: when I print a data.frame from a chunk, it gets printed in a very narrow rectangle that does not scale up (as it would with the normal source editor).

This makes it unusable because often I can't see the full variable names or the full values.

enter image description here

I tried looking at the options (Global Options > R Markdown > Visual) but I can't find anything that changes this: Editor content width only changes the width of the text and code, not the chunk output.

Coercing a data.frame to tibble also doesn't fix this.

I'm using RStudio 2022.07.2 Build 576.


Solution

  • Alright I know I answered this question before. I can't find that question or my answer in Stack Overflow, but I found the script I used to create & test the answer. I tested it because I think it was a few years ago. Everything seems to work.

    This isn't a permanent solution. It will persist until you restart RStudio.

    Step 1: Right-click anywhere in RStudio and select 'Inspect Element' from the dropdown to open developer tools. If you weren't aware, it's just like a browser that way.

    enter image description here

    Step 2: You need to use the console. Whether you use the console drawer (three dots) or the console tab (top of the inspector window, second from the left).

    enter image description here enter image description here

    The first JS function

    At the cursor, you'll paste two javascript functions. I store these in an RMD, within a JS chunk. They won't do anything when you knit.

    allOf = document.querySelectorAll('[data-ordinal]'); //find them all
    if (allOf==undefined || allOf==null) {
      allOf = document.querySelector('[data-ordinal]');  // if there is only one
    }
    
    sAdd = "max-width: none;"           // create style to add
    
    try{
      for(i = 0, n = allOf.length; i < n; i++){ //loop through them all
        styler = allOf[i].getAttribute("style");
        if (styler==undefined || styler==null) {    // if there are no HTML styles set
          styler = "";  
          console.log("No style attributes found for ", i);
        }
        if (styler!="width: 100%; max-width: 800px;") {// if this isn't a chunk output as expected
          continue;
          console.log("Attributes not changed for ", i);
        }
        allOf[i].setAttribute("style",styler+sAdd);    
        console.log("Attributes set for ", i);
    }} catch(error) {
        console.error(error);
    }
    

    The second JS function

    allMore = document.querySelectorAll('.ace_lineWidgetContainer > div');  // first child of class
    if (allMore==undefined || allMore==null) {
      allMore = document.querySelector('.ace_lineWidgetContainer > div');   // if there is only one
    }
    
    sAdd2 = "overflow: visible;"           // create style to add
    
    try{
      for(j = 0, m = allMore.length; j < m; j++){ //loop through them all
        styler2 = allMore[j].getAttribute("style");
        if (styler2==undefined || styler2==null) {    // if there are no HTML styles set
          styler2 = "";  
          console.log("No styles were found for ", j)
        }
        allMore[j].setAttribute("style",styler2+sAdd2); // append new styles
        allMore[j].style.height = null;                 // remove the height style
        console.log("Attributes set for ", j); 
    }} catch(error) {
        console.error(error);
    }
    

    The console after entering the functions.

    enter image description here

    The inline rendered chunks before and after side by side

    enter image description here enter image description here