Shorten/Hold Constant Length of renderPrint Output

Hello,

I am working on a Shiny project where I have a tab for a regression output using renderPrint. I am using a selectInput to allow the user to choose multiple independent variables to include in the model. The output can grow longer based on the number of independent variables the user selects for the model. Here is an example of what I'm doing:


library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)
library(DT)
library(ggplot2)
library(zoo)
library(dplyr)
library(htmltools)
library(formattable)

IndVarChoices<-c("cyl","disp","hp","drat","wt","qsec","vs","am", "gear","carb")

# Define UI for application
ui <- fluidPage(
  navbarPage("Example",
             tabPanel("Regression",
                      tabname="regression",
                      icon=icon("chart-line"),
                      fluidPage(column(width=6,
                                       selectInput(inputId = "indep", 
                                            label = "Choose Independent Variable(s) to Include:", 
                                            multiple = TRUE, 
                                            choices = as.list(IndVarChoices), 
                                            selected = IndVarChoices[1:2]),
                          verbatimTextOutput(outputId = "RegOut"))))))
                  

# Define server logic 
server <- function(input, output) {
  
  
  #Reactive portions of this tab
  recipe_formula <- reactive(mtcars %>%
                              recipe() %>%
                              update_role(mpg,new_role = "outcome") %>%
                              update_role(!!!input$indep,new_role = "predictor") %>% 
                              formula())
  
  lm_reg <- reactive(
    lm(recipe_formula(),data = mtcars)
  )
  
  #Output portions of this tab 
  output$RegOut <- renderPrint({
    summary(lm_reg())
    
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

I want to be able to force the length of the output to stay at a certain length. I would ideally like to have a vertical scroll bar appear that allows the user to scroll down to the bottom of the output. I plan to have this output next to another plot that doesn't grow, so it'll look a little weird when one output is longer than the other if the user selects a bunch of independent variables in the model.

Can someone help point me in the right direction? Any help would be appreciated! Thank you!

...

you can set dimensions of an element with CSS, and controll the overflowing data behaviour (including adding scroll bar).
https://www.w3schools.com/css/css_overflow.asp

Can you provide an example of how I could use this?

library(shiny)

ui <- fluidPage(
  sliderInput("sen_num","how many sentences ?",
               min=1,
               max=10,
               value=5),
  tags$style("#mytext {white-space: pre-line;
                       height:10em;
                       overflow:auto;}"),
             verbatimTextOutput("mytext")
  

)

server <- function(input, output, session) {
  output$mytext <- renderPrint({
    
    sentences <- rep("this is a sentence\n",req(input$sen_num))
    length(sentences)
    cat(paste0(1:length(sentences)," ",sentences))
  })
}

shinyApp(ui, server)
1 Like

Thanks for this example! I tried to incorporate this into my code, but this created a whole new problem. Now, when using this, my output is no longer arranged cleanly in a column.

Here's an example of what happened:

library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)

IndVarChoices<-c("cyl","disp","hp","drat","wt","qsec","vs","am", "gear","carb")

# Define UI for application
ui <- fluidPage(
  navbarPage("Example",
             tabPanel("Regression",
                      tabname="regression",
                      icon=icon("chart-line"),
                      fluidPage(selectInput(inputId = "indep", label = "Choose Independent Variable(s) to Include:", 
                                                    multiple = TRUE, choices = as.list(IndVarChoices), selected = IndVarChoices[1:2])),
                      tags$style("#RegOut {white-space: pre-line;height:40em; overflow:auto;}"),
                      verbatimTextOutput("mytext"),
                                        verbatimTextOutput(outputId = "RegOut"))))
                               

# Define server logic 
server <- function(input, output) {
  
  recipe_formula <- reactive(mtcars %>%
                              recipe() %>%
                              update_role(mpg,new_role = "outcome") %>%
                              update_role(!!!input$indep,new_role = "predictor") %>% 
                              formula())
  
  lm_reg <- reactive(
    lm(recipe_formula(),data = mtcars)
  )
  
  output$RegOut <- renderPrint({
    summary(lm_reg())
    
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

How can I adjust this code so the regression output displays cleanly in columns as it normally would have had I not added the tags$style code?

change pre-line to pre

1 Like

It works! Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.