Long variable names in shiny app

Hi,

I am building a shiny app for a dataset derived from a survey that had long questions such as: "How many times did the board meet during 2017?"

As thing stand now I have title and or legend of plots that have text so long it gets truncated.

I know I can do something like HTML("I want a line break here <br/> since the text is long"

But then I will have breaks even when the screen is wide enough. I was wondering is there any way to make this adjust automatically depending on the width of the screen?

I'm not aware of any package for doing this, but you can use something like this example, and fine tune the screen ratio/number of characters relationship for your specific application

library(shiny)
library(stringr)
library(ggplot2)

shinyApp(
    ui = basicPage(
        tags$head(tags$script('
                                var dimension = [0, 0];
                                $(document).on("shiny:connected", function(e) {
                                    dimension[0] = window.innerWidth;
                                    dimension[1] = window.innerHeight;
                                    Shiny.onInputChange("dimension", dimension);
                                });
                                $(window).resize(function(e) {
                                    dimension[0] = window.innerWidth;
                                    dimension[1] = window.innerHeight;
                                    Shiny.onInputChange("dimension", dimension);
                                });
                            ')),
        mainPanel(
            textInput("title",
                      label = h5("Plot Title"),
                      value = "How many times did the board meet during 2017"),
            plotOutput("plot")
        )
    ),
    server = function(input, output) {
        output$plot <- renderPlot({
            wraped_title <- str_wrap(input$title,
                               width = input$dimension[1]/10)
            iris %>% 
                ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
                geom_point() +
                labs(title = wraped_title)
            
        })
    }
)
1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.