R Shiny: Wrap text for long UI options - r

I'm using prettyCheckboxGroup from shinyWidgets within a sidebar panel in Shiny. Some of the items are unavoidably a bit long and so they extend out of the edge of the sidebar.
I'd like to know how to either wrap long text or limit the width for text to force it on to the next line.
I assumed that the width argument would achieve this but it only seems to effect the caption and not the choices text.
The standard checkboxGroupInput did this as standard so I'm wondering if I am missing something with the fancier ShinyWidgets version?
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
sidebarPanel(
width = 2,
uiOutput('boxpick')
)
)
server<- function(input, output) {
output$boxpick <- renderUI(
prettyCheckboxGroup('boxpick', 'Pick an item:',
choices= c('really really long',
'really really really really long',
'really really really really really really really really long')
)
)
}
shinyApp(ui,server)

Related

How to divide two words that appear in navigation bar tabs , in shiny app, html?

I am trying to make two lines of the words that appear in each tab, in navigation bar but cannot in any way.
Here it is the pics with the words I am trying to divide in two lines.
And here is a snippet of a code in r script
gene_expressions_sign_tab <- shiny::tabPanel(
"Gene Expression",
icon = icon("chart-line"),
value = "Gene",
wellPanel(
fluidRow("etc")
############################################################################Adding extra info after I have been given an answer bellow
###########################################################################
And it possible to get the two words in two lines in one tab, is it possible to centre the words? the picture reveals the fact the words aren't centered.
Use HTML and embed <br/> in your titles.
library(shiny)
shinyApp(
ui = fluidPage(
tabsetPanel(
tabPanel(HTML("hello<br/>world")),
tabPanel(HTML("hello<br/>again"))
)
),
server = function(input, output, session) {}
)

Aligning the label and radioButton in a fluidRow() in R Shiny

I am trying to align the label and radio button in the same line in the application but I'm unable to achieve that.
This is the code used for displaying the label and radio button
fluidRow(
align = 'center',
column(5,"Choose Metric: "),
column(11, radioButtons("typeRadio", "",list("Project Count", "Project Efforts"), inline = TRUE))
)
But I'm getting this output
How can I get the label and radioButton in the same line?
Can anyone provide a proper solution to achieve the expected output?
It's very hacky, but this works for me:
library(shiny)
ui <- fluidPage(
fluidRow(
align="center",
column(2, "Choose Metric: "),
column(4, radioButtons("typeRadio", "",list("Project Count", "Project Efforts"), inline = TRUE))
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Your original code defines 16 colulmns. That will cause problems for any solution: the maximum column count is 12.
My solution works because specifying label="" causes Shiny to output the HTML for a label, which is invisible (because it's the empty string), but which still occupies vertical space. Specifying label=NULL suppresses all output associated with the label, and hence it occupies no vertical space.
Put another way, your solution does align the tops of the two widgets. It's just that the top of the radio group is invisible.
Incidentally, it's always best to provide a simple, self-contained example, even for a set up as obvious as this.

Make infobox font size adapt dynamically to content length

I am passing an input selection through to an infobox, using shinydashboard. I am encountering an issue because some of the inputs are short and some are a bit longer. Ideally I want the text to fill the box, but if I set it to the right size for something quite short, then the longer text expands the box in an untidy way. My current approach is therefore to keep the text smaller, but that does mean that when the short input is selected, it is quite diminutive-looking.
I learnt how to change text size with this answer. However, this sets the size in a way that doesn't respond to different inputs - so presumably the percentage relates to the default size rather than to the box dimensions.
Possibly CSS? Simply because so much styling goes that way - but I'm not very proficient here. Or possibly some sort of if statement, or using conditionalPanel or validate - but I'm not sure what the logic would be? I have tried without any luck on those (and my attempts have been quite bulky anyway).
Example code (with text on the larger size):
library(shiny)
library(shinydashboard)
sidebar <- dashboardSidebar(
selectInput(
"text_select", label = "Please select",
choices = c("Short", "A longer option to choose")
)
)
body <- dashboardBody(
fluidRow(infoBoxOutput("selected")
)
)
ui <- dashboardPage(dashboardHeader(title = "Example"),
sidebar,
body
)
server <- function(input, output) {
output$selected<- renderInfoBox({
infoBox(title = "Selected",
value = tags$p(input$text_select,
style = "font-size: 190%;"),
icon = icon("bullseye"), color = "red")
})
}
shinyApp(ui, server)

Giving Titles to side plots in Shiny with Fluidrow

How to give titles in Shiny for two side plots, I am trying to do something like following, which is definitely giving me error
shinyUI(
titlePanel("Hello Shiny!"),
mainPanel(
fluidRow(
splitLayout(cellWidth(50%,50%),
tableOutput("Table1", tags$b("Title1")),
plotOutput("Plot",tags$b("Title2)))))
Here is the solution for you:
library(shiny)
ui <- basicPage(
fluidRow(
column(6,box("Title 1",tableOutput("Table1"))),
column(6,box("Title 2",plotOutput("Plot")))))
server <- function(input, output) {
}
shinyApp(ui, server)
It is enough if you just type the text in column container before ..output(..)
I have improved bit your code by replacing splitLayout() to column containers which divide/split the ui into two, same size containers (column(6)=50%) and addition of box(), so your outputs are held there and title will be attached to it (to the box)

display different number of tabPanels in a navbarPage in Shiny, without renderUI or uiOutput

I am trying to display from 1 to 5 tabPanels in a navbarPage in Shiny.
I have 5 plots my code generates, but I'd like a user to be able to select how many they want to have access to -- to be displayed one plot in each tabPanel, naturally.
I've got an external configuration file (config.txt) that via source('config.txt'), I have access to a number_of_pages variable.
For example, number_of_tabPages <- 3
How would I set this up in UI.R?
The number of tabPanels can't be hardcoded at all in the UI file, since it depends on a value that is specified by a user, not using a control.
I've searched around and found that most of the approaches to this kind of thing
involve using uiOutput and renderUI functions, such as this similar problem, but I don't want any special control in the UI to do any selecting.
This is where things get tricky, when we are building the UI depending on values that may change. My brain is trying to wrap itself around the best method for doing this type of thing -- I feel like it isn't exactly in line with how Shiny wants to communicate with itself using a UI <--> server environment.
Any advice is greatly appreciated.
My UI.R is easy to create when it isn't dynamic:
fluidRow(
column(12,
"",
navbarPage("",tabPanel("First Tab",
plotOutput("plot1")),
tabPanel("Second Tab",
plotOutput("plot2")),
tabPanel("Third Tab",
plotOutput("plot3")),
tabPanel("Fourth Tab",
plotOutput("plot4")),
tabPanel("Fifth Tab",
plotOutput("plot5"))
)
)
)
)
Thanks!
If you don't need the user to change the number of tabPanel interactively, but just load varying numbers of them when the app is started you can use the do.call function in the navBarPage:
library(dplyr)
library(shiny)
library(ggvis)
#number of tabs needed
number_of_tabPages <- 10
#make a list of all the arguments you want to pass to the navbarPage function
tabs<-list()
#first element will be the title, empty in your example
tabs[[1]]=""
#add all the tabPanels to the list
for (i in 2:(number_of_tabPages+1)){
tabs[[i]]=tabPanel(paste0("Tab",i-1),plotOutput(paste0("plot",i-1)))
}
#do.call will call the navbarPage function with the arguments in the tabs list
shinyUI(fluidRow(
column(12,
"",
do.call(navbarPage,tabs)
)
)
)

Resources