Search code examples
rshinyscatter-plotlattice

How to wrap text within reactive splom lattice plot?


Within my shiny app, I am having my users select n number of attributes to plot within my splom() call, which essentially follows these steps:

  1. User chooses a number.
  2. User chooses variables.
  3. Code subsets my data set based on users choices.
  4. Splom is called in a very simple call, similar to: lplot <- splom(data_subset).

However, since the panels within splom() are being labeled based on the attribute name (which is reactive, so I can't label ahead of time) and the names are very long you can't read them.

Does anyone know if there is a way to wrap text within the splom panel outputs? The wrapping would be done on the attribute reactive value, so it wouldn't be done on a string.

This is what my output looks like:

splom


Solution

  • After subsetting in step 3, you can manipulate the names of the data subset to wrap appropriately.

    ## Subset the data in whatever way shiny does (here's a reproducible example)
    irisSub <- subset(iris, select = grepl("Width", names(iris)))
    

    Then

    1. Replace "word" separators with spaces. In your example, you probably just need "_"

      names(irisSub) <- gsub("\\.|_", " ", names(irisSub))
      
    2. Wrap these strings at a fairly narrow width. You might have to experiment with other widths here.

      wrapList <- strwrap(names(irisSub), width = 10, simplify = FALSE)
      
    3. Paste these strings back together with a linebreak (\n) in between and assign them to names of data subset

      names(irisSub) <- vapply(wrapList, paste, collapse = "\n", FUN.VALUE = character(1))
      
    4. splom should respect the line breaks

      splom(irisSub)
      

      enter image description here