I have an application that displays data on indicators. Each indicator (30+) has a definition. We have this definition of all indicators in a single html file (easier to maintain than 30 different ones).
In my ShinyApp, I want to show only the section relevant to the indicator selected, not the full document.
I was wondering how this could be done....
Here is an example that is only showing the full 'document' instead of the part which is selected in the sidebar:
documentation <- structure("<div> <div id=\"part1\"> <h1>Part 1</h1> <p>This is part 1 out of 2</p> <p> </p> </div> <div id=\"part2\"> <p> </p> <h1>Part 2</h1> <p>This is part 2 out of 2.</p> </div> </div>", html = TRUE, class = c("html", "character"))
documentation
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = "part",label = "Select the part you want to see", choices = c("part1", "part2"))
),
dashboardBody(
uiOutput("section")
)
)
server <- function(input, output) {
output$section <- renderUI({
HTML(documentation) # This needs subsetting based on input$part
})
}
shinyApp(ui, server)
I came up with a quick and dirty solution, by manipulating the HTML as a string.
I wonder if there are more elegant methods though...
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = "part",label = "Select the part you want to see", choices = c("part1", "part2"))
),
dashboardBody(
uiOutput("section")
)
)
server <- function(input, output) {
output$section <- renderUI({
# HTML(documentation)
t1 <- strsplit(documentation, "<div")[[1]][grepl(input$part, strsplit(documentation, "<div")[[1]])]
t2 <- paste0("<div", t1)
t2 <- gsub("</div> </div>", "</div>", t2)
HTML(t2)
})
}
shinyApp(ui, server)
Related
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.
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)
I'm dealing with a quite complicated shiny app in which I would like to create an UI output inside server function. UI is not that easy and depends on many items created on a server side so I'm creating it concatenating HTML parts of UI. Everything works until I meet plotly chart.
I've created a simpler version of my app to make it easier to understand my problem.
Normally I'd do sth like that:
library("shiny")
library("plotly")
library("dplyr")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
plotlyOutput("distPlot1"),
plotOutput("distPlot2")
)
)
)
server <- function(input, output) {
output$distPlot1 <- renderPlotly({
x <- faithful[, 2]
plot_ly(x = x, type = "histogram")
})
output$distPlot2 <- renderPlot({
x <- faithful[, 2]
hist(x)
})
}
shinyApp(ui = ui, server = server)
to obtain this:
But when I start to create ui on server side like here (edited part with more divs inside ui):
library("shiny")
library("plotly")
library("dplyr")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
htmlOutput("ui1"),
uiOutput("ui2")
)
)
)
server <- function(input, output) {
output$distPlot1 <- renderPlotly({
x <- faithful[, 2]
plot_ly(x = x, type = "histogram")
})
output$distPlot2 <- renderPlot({
x <- faithful[, 2]
hist(x)
})
output$ui1 <- renderUI({
show <- h1("lfkhg")
show <- paste0(show, plotlyOutput("distPlot1") %>% as.character())
HTML(show)
})
output$ui2 <- renderUI({
show <- h1("lfkhg")
show <- paste0(show, plotOutput("distPlot2") %>% as.character())
HTML(show)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Plotly plot does not appear...
Do you know why and how to deal with this problem?
I dont know why you need %>% HTML() in there as it works for me without it. Also if you want to add more things into the renderUI then simply use tagList and combine them together, here I will add h1 as per your comment
library("shiny")
library("plotly")
library("dplyr")
ui <- fluidPage(
sidebarLayout(sidebarPanel(),
mainPanel(
uiOutput("ui1"),
uiOutput("ui2")
)
)
)
server <- function(input, output) {
output$distPlot1 <- renderPlotly({
x <- faithful[, 2]
plot_ly(x = x, type = "histogram")
})
output$distPlot2 <- renderPlot({
x <- faithful[, 2]
hist(x)
})
output$ui1 <- renderUI({
tagList(h1("lfkhg"),plotlyOutput("distPlot1"))
})
output$ui2 <- renderUI({
plotOutput("distPlot2")
})
}
# Run the application
shinyApp(ui = ui, server = server)
Though there is the posibility to add a title in a shiny dashboard app, which appears correctly in the app page,
## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "my title"),
dashboardSidebar(),
dashboardBody()
)
server <- function(input, output) { }
shinyApp(ui, server)
It does not appear as the name of tab of the browser. As name of tab in browser only appears the URL (like 127...).
You can set the browser page title like this
ui <- dashboardPage(title="Browser title",
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
...
I am developing a Shiny application in R. For certain pieces of renderPrint output I would like the shiny user interface to display nothing. Kind of like the hidden option for pre or div tags in HTML5 example shown below:
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_global_hidden
Below is my shiny example code. A brief explanation: you can use a drop down menu to select one of two variables (factor variable or continuous variable). If you select the factor variable I want to show the caption and the table output. If you select the continuous variable I don't want to see anything. Right now, the caption disappears if you insert a blank string "" as the return to renderText. However, I don't know how to get renderPrint to show nothing. I've tried:
"". Doesn't work as it returns the actual blank string
NULL. Doesn't work as it returns the string NULL
invisible(). Best so far, but still doesn't work as it returns the grey formatted box.
Goal is to just display nothing. Shiny ui.r and server.r code given below:
library(shiny)
##
## Start shiny UI here
##
shinyUI(pageWithSidebar(
headerPanel("Shiny Example"),
sidebarPanel(
wellPanel(
selectInput( inputId = "variable1",label = "Select First Variable:",
choices = c("Binary Variable 1" = "binary1",
"Continuous Variable 1" = "cont1"),
selected = "Binary Variable 1"
)
)
),
mainPanel(
h5(textOutput("caption2")),
verbatimTextOutput("out2")
)
))
##
## Start shiny server file and simulated data here
##
binary1 <- rbinom(100,1,0.5)
cont1 <- rnorm(100)
dat <- as.data.frame(cbind(binary1, cont1))
dat$binary1 <- as.factor(dat$binary1)
dat$cont1 <- as.numeric(dat$cont1)
library(shiny)
shinyServer(function(input, output) {
inputVar1 <- reactive({
parse(text=sub(" ","",paste("dat$", input$variable1)))
})
output$caption2 <- renderText({
if ( (is.factor(eval(inputVar1()))==TRUE) ) {
caption2 <- "Univariate Table"
} else {
if ( (is.numeric(eval(inputVar1()))==TRUE) ) {
caption2 <- ""
}
}
})
output$out2 <- renderPrint({
if ( (is.factor(eval(inputVar1()))==TRUE) ) {
table(eval(inputVar1()))
} else {
if ( (is.numeric(eval(inputVar1()))==TRUE) ) {
invisible()
}
}
})
})
A few questions...
Why does renderText handle hidden/invisible presentation different than renderPrint? Is it because the former outputs text as pre tag; whereas, the latter displays formatted output in div tag?
To those HTML experts (upfront, I am not one)...what option would be best to get my output to display nothing? Is the hidden option embedded in a pre or div tag best (I know it doesn't work in IE browsers). Should I try something else? CSS options, etc?
Assuming hidden is the best way to go (or that I get an answer to 2. above), how do I pass this option/argument through the renderPrint function in shiny? Or would I need to use a different shiny function to achieve this functionality?
Btw...My R version is: version.string R version 3.0.1 (2013-05-16) and I am using shiny version {R package version 0.6.0}. Thanks in advance for your help.
I am not sure that I have understood your question, but try the following:
here is the Ui first:
library(shiny)
ui <- fluidPage(pageWithSidebar(
headerPanel("Shiny Example"),
sidebarPanel(
wellPanel(
selectInput(inputId = "variable1",label = "Select First Variable:",
choices = c("Binary Variable 1" = "binary1",
"Continuous Variable 1" = "cont1"),
selected = "Binary Variable 1"
)
)
),
mainPanel(
h5(textOutput("caption2")),
verbatimTextOutput("out2", placeholder=TRUE)
)
))
Start shiny Server file and simulated data here:
binary1 <- rbinom(100,1,0.5)
cont1 <- rnorm(100)
dat <- as.data.frame(cbind(binary1, cont1))
dat$binary1 <- as.factor(dat$binary1)
dat$cont1 <- as.numeric(dat$cont1)
server <- (function(input, output) {
inputVar1 <- reactive({
parse(text=sub(" ","",paste("dat$", input$variable1)))
})
output$caption2 <- renderText({
if ((is.factor(eval(inputVar1()))==TRUE) ) {
caption2 <- "Univariate Table"
} else {
if ((is.numeric(eval(inputVar1()))==TRUE) ) {
caption2 <- "Continous"
}
}
})
output$out2 <- renderPrint({
if ((is.factor(eval(inputVar1()))==TRUE) ) {table(eval(inputVar1()))}
})
})
And finally:
shinyApp(ui = ui, server = server)