Why does R Shiny not recognize my line break command? - html

I have a simple app where I want to have a text pop up, but because the text is long, I want to add line breaks. For some reason, R isn't recognizing my line breaks, even though I've added , like I read in this example.
Any help would be greatly appreciated!
library(shiny)
long_text <- paste0(htmltools::HTML("I have a lot of text. <br><br>And I want it on different lines.<br><br> This should work, but R is being....<br><br>difficult."))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
br(),
actionButton(inputId = "text_info",
label = "My R Sob Story", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B")
),
mainPanel(
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
observeEvent(input$text_info, {
showModal(modalDialog(long_text, title = strong("Why R you doing this to me?")))
})
}
# Run the application
shinyApp(ui = ui, server = server)
Here's what it looks like now:

If you paste after changing the text to HTML, it will be character again.
library(shiny)
long_text <- htmltools::HTML("I have a lot of text. <br><br>And I want it on different lines.<br><br> This should work, but R is being....<br><br>difficult.")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
br(),
actionButton(inputId = "text_info",
label = "My R Sob Story", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B")
),
mainPanel(
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
observeEvent(input$text_info, {
showModal(modalDialog(long_text, title = strong("Why R you doing this to me?")))
})
}
# Run the application
shinyApp(ui = ui, server = server)

Related

How to place text in same line with an html tag in R shiny?

As shown in the below image, I'm trying to place a line of text in front of an info icon rendered using the popify() function in the shinyBS package. The code at the bottom works for the situation where there is no text in front of info icon and commented-out is one of my attempts to insert the text. Uncomment, run the code, and you'll see garbled output.
So how would one insert text in front of the icon? One option is to split the text and icon into 2 separate columns, but I don't want to fiddle with the column widths to make it look right. I want the text to "flow into" the icon.
I thought this Stack Overflow question might provide an answer but it is a dead end: How to place both text and image output in one html div (rshiny)
Code:
library(shiny)
library(shinyBS)
app = shinyApp(
ui =
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins","Number of bins:",min = 1,max = 50,value = 30)
),
mainPanel(
plotOutput("distPlot"),
uiOutput("uiExample")
)
)
),
server =
function(input, output, session) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$uiExample <- renderUI({
# paste( #uncomment
# "Look at the little circle >>", #uncomment
tags$span(
popify(icon("info-circle", verify_fa = FALSE),
"Placeholder",
"This icon is <b>placeholder</b>. It will be fixed</em>!")
)
# ) #uncomment
})
}
)
runApp(app)
This could be achieved via a tagList and another span:
library(shiny)
library(shinyBS)
app = shinyApp(
ui =
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins","Number of bins:",min = 1,max = 50,value = 30)
),
mainPanel(
plotOutput("distPlot"),
uiOutput("uiExample")
)
)
),
server =
function(input, output, session) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$uiExample <- renderUI({
tagList(
tags$span(
"Look at the little circle >>"
),
tags$span(
popify(icon("info-circle", verify_fa = FALSE),
"Placeholder",
"This icon is <b>placeholder</b>. It will be fixed</em>!")
)
)
})
}
)
runApp(app)

How to add tabulation to a line using htmlOutput()?

I want to add one tabulation to a line in Shiny but I don't find the way to do it.
I know that there are HTML tags in Shiny such as strong to put words in bold, small to make them smaller... Even blockquote to add blocks of quotes.
But I didn't find one to add one tabulation.
Does anyone know how to do it?
Reproducible code:
library(shiny)
ui = pageWithSidebar(
headerPanel("My app"),
sidebarPanel(
),
mainPanel(
htmlOutput("text")
)
)
server = function(input, output) {
output$text <- renderUI({
str1 <- strong("This is the first line in bold:")
str2 <- em("This is the second line in italics and with one tabulation")
HTML(paste(str1, str2, sep = '<br/>'))
})
}
shinyApp(ui,server)
You can add a style attribute to each shiny-tag:
library(shiny)
ui = pageWithSidebar(
headerPanel("My app"),
sidebarPanel(),
mainPanel(
htmlOutput("text")
)
)
server = function(input, output) {
output$text <- renderUI({
tag1 <- p(strong("This is the first line in bold:"))
tag2 <- p(em("This is the second line in italics and with one tabulation"), style = "text-indent: 1em;")
HTML(paste(tag1, tag2, sep = '<br/>'))
})
}
shinyApp(ui,server)
You could do it just using html code instead of the r tags from shiny:
library(shiny)
ui = pageWithSidebar(
headerPanel("My app"),
sidebarPanel(
),
mainPanel(
htmlOutput("text")
)
)
server = function(input, output) {
output$text <- renderUI({
str1 <- "<p><strong>This is the first line in bold:</strong></p>"
str2 <- "<p style='text-indent: 45px'><em>This is the second line in italics and with one tabulation</em></p>"
HTML(paste(str1, str2, sep = ''))
})
}
shinyApp(ui,server)
unless I have misunderstood what you're trying to do.

Printing all factor levels as a list in a shiny dashboard

I would like to print all levels of a factor in a shiny dashboard. I know I could easily do that by:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Testing shiny App"),
# Show a plot of the generated distribution
mainPanel(
htmlOutput("my_levels_list")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$my_levels_list <- renderUI({
HTML(levels(iris$Species))
})
}
# Run the application
shinyApp(ui = ui, server = server)
But all the results are outputed within the same row:
<div id="my_levels_list" class="shiny-html-output shiny-bound-output">setosa versicolor virginica</div>
What I would like to have is something like this instead:
<div id="my_levels_list" class="shiny-html-output shiny-bound-output">
<ul><li>setosa</li> <li>versicolor</li> <li>virginica</li></ul></div>
I tried with more extravagant code like:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Testing shiny App"),
# Show a plot of the generated distribution
mainPanel(
htmlOutput("my_levels_list")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$my_levels_list <- renderUI({
for (i in iris$Species) {
HTML(i)
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
But nothing is outputted.

Is there a way to eliminate y scrollers on shiny output in rmarkdown?

I have an interactive doc in rmarkdown using shiny apps.
My YAML:
---
title: "Shiny HTML Doc"
author: "theforestecologist"
date: "Apr 13, 2017"
output: html_document
runtime: shiny
---
I generate a number of shiny apps throughout this document, but they all are too long (i.e., they all require y scrollers to view their entirety.
I know I can add options = list(height = ###,width = ###) within the shinyApp function in my code chunk to control individual rendered apps, but I want my readers to see my shiny code (sans messy option controls).
So my desired approach is to control all of the shiny app outputs at once.
Specifically, is there a way to make it so they each can have variable heights, but each one be fully pictured (i.e., not needing a vertical (y) scroller)?
Example Code:
---
title: "Shiny HTML Doc"
author: "theforestecologist"
date: "Apr 13, 2017"
output: html_document
runtime: shiny
---
I have an interactive shiny doc with app outputs of varying heights.
By default, they all have the same height, which is too small of a value
(and thus creates the need for vertical scrolling bars).
##### **Example 1**
```{r, eval=TRUE,echo=FALSE}
library(shiny)
ui <- fluidPage(
titlePanel("Ex1"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput(inputId = "type", label = "Plant Type", choices = levels(CO2$Type),
selected = levels(CO2$Type))
),
mainPanel(
plotOutput(outputId = "scatter.plot")
)
)
)
server <- function(input, output) {
output$scatter.plot <- renderPlot({
plot(uptake ~ conc, data = CO2, type = "n")
points(uptake ~ conc, data = CO2[CO2$Type %in% c(input$type),])
title(main = "Plant Trends")
})
}
shinyApp(ui = ui, server = server)
```
The output of the next example is longer and therefore needs a larger height assignment to get rid of the scroll bar.
##### **Example 2**
```{r, eval=TRUE,echo=FALSE}
ui <- fluidPage(
titlePanel("Ex1"),
sidebarLayout(
sidebarPanel(
div(style = "padding:0px 0px 450px 0px;",
checkboxGroupInput(inputId = "type", label = "Plant Type", choices = levels(CO2$Type),
selected = levels(CO2$Type))
)
),
mainPanel(
plotOutput(outputId = "scatter.plot")
)
)
)
server <- function(input, output) {
output$scatter.plot <- renderPlot({
plot(uptake ~ conc, data = CO2, type = "n")
points(uptake ~ conc, data = CO2[CO2$Type %in% c(input$type),])
title(main = "Plant Trends")
})
}
shinyApp(ui = ui, server = server)
```

Render Greek letters in table ouput of shiny apps

My fellow Shiny users! I'm having trouble rendering a Greek letter in my Shiny table output. Notice that I do know how to print a Greek letter in Shiny using the HTML function. See the code snippets below.
This is ui.R:
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("A sample"),
sidebarPanel(
numericInput(
inputId = "alpha",
label = HTML("α:"),
value = 0.05,
step = 0.01
)
),
mainPanel(
plotOutput("samplePlot")
)
))
And this is server.R:
shinyServer(function(input, output) {
output$samplePlot <- renderPlot({
hist(c(1:100), main = "A Sample Plot", xlab = "Whatever")
})
})
This works fine and gives me a Greek alpha in the slider. However, I wish to have Greek letters in my table output as well, but a similar approach doesn't seem to work... See the code below.
This is ui.R:
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("A sample"),
sidebarPanel(
numericInput(
inputId = "alpha",
label = HTML("α:"),
value = 0.05,
step = 0.01
)
),
mainPanel(
plotOutput("samplePlot"),
tableOutput("myTable")
)
))
And this is server.R:
shinyServer(function(input, output) {
output$samplePlot <- renderPlot({
hist(c(1:100), main = "A Sample Plot", xlab = "Whatever")
})
output$myTable <- renderTable({
data.frame(alpha = HTML("α:"), value = 3)
})
})
Any ideas on how to get the Greek alpha printed out in the alpha column of the table output. Appreciate any help!
Try
sanitize.text.function = function(x) x
as an option to renderTable,
the idea which I got from this post:
r shiny table not rendering html