below is the shiny app, if I need to add some css into it (attached below), where can i add it so that it renders in r shiny app
css
<style>
.bs-example{
margin: 60px;
}
</style>
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Stateful"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
id="inTabset",
tabPanel("Tab 1",actionButton("switch_tab", "Go to the third tab")),
tabPanel("Tab 2", "there!"),
tabPanel("Tab 3", "there!"))
)
)
))
server.R
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
})
You can add your CSS file in the ui section, like below:
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "style.css")
)
Keep in mind your CSS file should be in the www directory in your shiny app.
Related
How would I add a black boarder to the main panel and side bar panel for the MRE? I would like the panels to stand out slightly more. I have adjusted background color on my app but think adding a black boarder will make each panel stand out better.
UI
library(shiny)
library(shinyalert)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
DT::dataTableOutput("cap_table"),
)
)
))
Server
library(shiny)
library(shinyalert)
data <- data.frame(x=rnorm(10),y=rnorm(10))
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
# render data selection
output$cap_table <- DT::renderDataTable(data)
})
You may consider adding custom css to your app. I right click the app when it's running to "inspect" the css class that I would like to change.
Here is an example using in-line code.
Here is a link to consider other approaches such as sourcing a css file. https://shiny.rstudio.com/articles/css.html
library(shiny)
library(DT)
# Define UI for application that draws a histogram
ui <- shinyUI(
fluidPage(
tags$head(
# Note the wrapping of the string in HTML()
tags$style(HTML(".well {
border: 1px solid #000000;
}")),
tags$style(HTML(".col-sm-8 {
border: 1px solid #000000;
}"))
),
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
DT::dataTableOutput("cap_table"),
)
)
))
data <- data.frame(x=rnorm(10),y=rnorm(10))
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {
# render data selection
output$cap_table <- DT::renderDataTable(data)
})
shinyApp(ui = ui, server = server)
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 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)
```
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 have a Shiny app using R code which attempts to use a bootstrap theme bootstrap_united.css:
# ui.R
library(shiny)
#
shinyUI(
fluidPage(
theme = "bootstrap_united.css",
navbarPage("",
tabPanel("Input and display your text",
fluidRow(
column(width = 6,
wellPanel(
h5("Your input ="),
tags$style(type="text/css", "textarea {width:100%}"),
tags$textarea(id = "myText", rows = 22, "")
)
),
column(width = 6,
wellPanel(
h5("Our output ="),
verbatimTextOutput("myText")
)
)
)
),
tabPanel("Help page",
fluidRow(
includeHTML("help.html")
# h5("This works!")
)
)
)
)
)
and
# server.R
library(shiny)
#
shinyServer(
function(input, output) {
output$myText <- renderText({input$myText})
}
)
The second tabPanel attempts to call an html file which is saved in the same folder as the ui.R and server.R files. The call is successful, and the page is displayed, but the presence of this page causes the theme colors from the CSS file to not be shown, and so everything in the R Shiny app is displayed in monochrome shades of gray.
However, if the second tabPanel just displays some text {e.g. shown as # h5("Our output =") in the R code} instead of an html page, then the theme is used/displayed correctly, and the app is displayed in color.
How can I adapt the R code in the shiny app so that the html page will be displayed, and the theme will be displayed correctly too?