I'm trying to include multiple tabsetPanel in the mainPanel of a Shiny app. When I launch the app, it's not showing anything as if the app is trying to figure out the UI element without getting into the server elements at all.
Anyone knows why this is the case?
Below is a minimal example based on the Shiny app template in RStudio.
ui.R
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Multiple tabset panels
mainPanel(
h2("tabsetpanel1"),
tabsetPanel(id = "pan1",
type = "tabs",
tabPanel("tab1", plotOutput("distPlot"))),
h2("tabsetpanel2"),
tabsetPanel(id = "pan2",
type = "tabs",
tabPanel("tab2", plotOutput("distPlot")))
)
)
))
server.R
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
Your outputs have to be unique you cant have multiple "distPlot", Change to something like this:
data <- reactive({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot1 <- renderPlot({
data()
})
output$distPlot2 <- renderPlot({
data()
})
Then in your ui
plotOutput("distPlot1")
plotOutput("distPlot2")
Related
I am trying to write Shiny App that would update slider input maximum value dynamically once I am executing arbitrary code when pressing the button:
library(shiny)
ui <- fluidPage(
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton(inputId ="refresh", label = 'boom')
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
counter <- reactiveValues(c = nrow(faithful))
observeEvent(input$refresh, {
faithful <- faithful[-1:-10, ]
counter$c <- nrow(faithful)
updateSliderInput(session, "bins", min = 1, max = counter$c, value = counter/2)
})
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
Unfortunately, slider input update works only once! Documentation example suggests to use observe() construct watching some reactive values however I would expect that such functionality should also be working when using observeEvent() What could be wrong here?
Appreciate your help!
The problem here is not the updateSliderInput, it is that counter is not a reactive so it is not persitent inside the app. Just convert it to a reactive like in this example and it works fine.
library(shiny)
ui <- fluidPage(
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton(inputId ="refresh", label = 'boom')
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
counter <- reactiveValues(c = 10)
observeEvent(input$refresh, {
counter$c <- counter$c + 10
updateSliderInput(session, "bins", min = 1, max = counter$c, value = counter$c/2)
})
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
In the following code,
is it possible to put bins in movable panel like the color change panel.
The follwoing image shows one panel is movable but the other is not movable.
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
shiny::tags$head(
shinythemes::themeSelector()
),#taghead
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
How to make movable panel?
Please let me know.
**Answer: The following code makes a draggable panel**
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
shiny::tags$head(
shinythemes::themeSelector()
),#taghead
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
# Show a plot of the generated distribution
mainPanel(
absolutePanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
draggable = T
),
plotOutput("distPlot")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
Movable panels can be achieved with absolutePanel.
I have a shiny app with a longer computation depending on the input. I am wondering if it is possible to display a text in the main panel at the same time when the computation is done (and not before).
Let's make an easy example. I simulated the longer computation with Sys.sleep():
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
h3('This is an example'),
plotOutput("distPlot")
)
)
))
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
Sys.sleep(5)
})
})
# Run the application
shinyApp(ui = ui, server = server)
The goal would be, to show the text 'This is an example' at the same time the computation is done and not before.
I think I have to make the text somehow reactive, but so far I haven't found a solution for this. Maybe a conditionalPanel could do it, but how can I bring the computation time in the condition? Any ideas?
Would this be what you search for? Your text variable as a reactive after observing the event of distPlot
library(shiny)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
textOutput('text1'),
plotOutput("distPlot")
)
)
))
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
Sys.sleep(2)
})
observeEvent(plotOutput("distPlot"), {
output$text1 <- renderText({ paste("Number of bins:", input$bins)})
})
})
# Run the application
shinyApp(ui = ui, server = server)
Is there a way to get italic words in my shiny titlePanel?
I tried
library(shiny)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Old <em>Faithful Geyser</em> Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
# Run the application
shinyApp(ui = ui, server = server)
and this print the title as Old <em>Faithful Geyser</em> Data. The em does not get interpreted. Am I doing something wrong or isn't there a way to get italic font in the title?
hello try this it should work
titlePanel( div(HTML("Old <em>Faithful Geyser</em> Data")))
This is my ui.R. This is an example provided in Shiny tutorial. I just edited it.
library(shiny)
library(markdown)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
absolutePanel(
bottom = 0, left=420, width = 800,
draggable = TRUE,
wellPanel(
em("This panel can be moved")
)
)
))
))
and my server. R
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
# Expression that generates a histogram. The expression is
# wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should be automatically
# re-executed when inputs change
# 2) Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})**
In this case, sliderInput is not working. If i remove absolute panel, sliderInput is ok. What may be the problem? Many thanks
The absolutePanel uses the jqueryui javascript library. It has its own slider. This results in a conflict with sliderInput which uses jslider library. You can see this as follows:
library(shiny)
runApp(
list(ui = fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
, tags$head(tags$script(src = "shared/jqueryui/1.10.3/jquery-ui.min.js"))
)
)
),
server = function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
)
)
EDIT: This has been fixed in the latest dev version of shiny. The slider component has been removed from the jqueryui inc. https://github.com/rstudio/shiny/commit/7e12a281f51e047336ba2c501fcac43af5253225