I am building my first app in shiny. The problem to solve is like this.
I've ui
shinyUI(fluidPage(
# Application title
titlePanel("My"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel("Variables",
sliderInput("bins",
"Number of circles:",
min = 1,
max = 50,
value = 30),
sliderInput("bins",
"Number of triangels:",
min = 1,
max = 50,
value = 1)
),
# Show a plot of the generated distribution
mainPanel(tabsetPanel(
tabPanel("Data", plotOutput("distPlot")),
tabPanel("Data1", textOutput("text1")),
tabPanel("Data2", plotOutput("distPlot1")))
)
)
))
and 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')
})
output$distPlot1 <- renderPlot({
x <- faithful[, 2]
plot(density(x))
})
output$text1 <- renderText({
"Hello mother"
})
})
The question concern ui part. I'd like to fave 2 panels in the sidebarPanel - triangles and circles, similar to that what is in the mainPanel i.e. Data, Data1, Data2.
Thanks for the comments!
Related
given the default example of an Shiny app, I would like adapt the size of the three plots such that they fill the height of the window. My current output looks like this.
library(shiny)
# Define UI for application that draws a histogram
ui <- fillPage(
# 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"),
plotOutput("distPlot2"),
plotOutput("distPlot3")
)
)
)
# 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')
})
output$distPlot2 <- 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')
})
output$distPlot3 <- 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)
As you can see from the code, there should be a third plot visible.
Thanks in advance
Add a height argument to your plotOutputs with 30vh (vertical height, essentially % of container). You can mess with the value to get it exactly where you want. More info on vh here.
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot", height = '30vh'),
plotOutput("distPlot2", height = '30vh'),
plotOutput("distPlot3", height = '30vh')
)
I am using bsModal() window from the ShinyBS package in one of my Shiny Apps. Whenever the user opens the modal window for the second time, the contents of the window from the first opening remain on the screen until the new content loads. Which is not ideal. Is there a way to delete the contents of the bsModal() window once it is closed? I do not want the older content to appear in the window once it is reopened. Subsequently, maybe there is a way to clean up the bsModal() window before opening?
I wasn't able to find any solution for this
Here is some example (not my) code of an app that can be also accessed through:
library(shinyBS)
bsExample("Modals")
ui.R
library(shiny)
library(shinyBS)
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton("tabBut", "View Table")
),
mainPanel(
plotOutput("distPlot"),
bsModal("modalExample", "Data Table", "tabBut", size = "large",
dataTableOutput("distTable"))
)
)
)
server.R
library(shiny)
library(shinyBS)
shinyServer(
function(input, output, session) {
output$distPlot <- renderPlot({
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$distTable <- renderDataTable({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
tab <- hist(x, breaks = bins, plot = FALSE)
tab$breaks <- sapply(seq(length(tab$breaks) - 1), function(i) {
paste0(signif(tab$breaks[i], 3), "-", signif(tab$breaks[i+1], 3))
})
tab <- as.data.frame(do.call(cbind, tab))
colnames(tab) <- c("Bins", "Counts", "Density")
return(tab[, 1:3])
}, options = list(pageLength=10))
}
)
I am developing a shiny app and want to improve the accuracy of the slider (the slider ranges from 0 to 1000 and it's very difficult to accurately adjust the slider with a step of 1). I can't find an answer to this anywhere.
This is the code for one of my sliders:
sliderInput("mean2", "", min=0, max=1000, value=500, step=1)
Try arranging your code like such:
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
actionButton("minus", "Minus"),
actionButton("plus", "Plus"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("mean2",
"Number of bins:",
min = 0,
max = 1000,
value = 500,
step= 1)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output, session) {
v <- reactiveValues(data = 500)
observeEvent(input$minus, {
v$data <- input$mean2 - 1
updateSliderInput(session,"mean2", value = input$mean2 - 1)
})
observeEvent(input$plus, {
v$data <- input$mean2 + 1
updateSliderInput(session,"mean2", value = input$mean2 + 1)
})
observeEvent(input$mean2, {
v$data <- input$mean2
})
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = v$data + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
shinyApp(ui, server)
I'm trying to create a timed counter within a Shiny app. When it increments each second, it should refresh a plot with some new characteristic that depends on the counter. Here's an example based on the 'Old Faithful' app. It doesn't work, but it gets at the idea. I tried to have reactiveTimer() refresh the plot, and a counter recorded with reactiveValues().
server.R
library(shiny)
shinyServer(function(input, output) {
refreshPlot <- reactiveTimer(intervalMs = 1000)
vals <- reactiveValues(counter = 0)
output$distPlot <- renderPlot({
refreshPlot()
vals$counter <- isolate(vals$counter) + 1
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
n_bins <- input$bins + vals$counter
bins <- seq(min(x), max(x), length.out = n_bins)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
ui.R
library(shiny)
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(
plotOutput("distPlot")
)
)
invalidateLater is what I'm looking for. Thanks, Dieter. Below is the server.R that works.
library(shiny)
shinyServer(function(input, output, session) {
vals <- reactiveValues(counter = 0)
output$distPlot <- renderPlot({
invalidateLater(millis = 1000, session)
vals$counter <- isolate(vals$counter) + 1
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
n_bins <- input$bins + vals$counter
bins <- seq(min(x), max(x), length.out = n_bins)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
My Shiny script gets input from a drop down list. Based on this, I set (on the server side) a specific string (in a reactive) that should be displayed in the chart (for example as x axis title). This works only if the string contains no spaces, but no string is shown in the chart if contains spaces.
How can I get it to accept any string?
Here's my code (I modified one of the example from the Shiny tutorial to keep it as simple as possible):
# server.r
# Here the string is set depending on what was chosen by the user
shinyServer(function(input, output, session) {
label1_new <- reactive({
if (input$variable1=="pp_pmw") {label1_new <- "PP pmw"}
if (input$variable1=="perc_pp*100") {label1_new <- "PP percent"}
if (input$variable1=="formality") {label1_new <- "Formality"}
})
label1_new2 <- renderText({label1_new()})
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
# xlabel1_new2() contains the string from above
hist(x, breaks = bins, col = 'darkgray', border = 'white', xlab=label1_new2())
})
})
# ui.r
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
selectInput("variable1", "Circle size:",
list("PP pmw" = "pp_pmw",
"PP percent" = "perc_pp*100",
"Formality" = "formality")),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
renderText is for use with ui.r, not for creating strings to be used in server.r
# server.r
# Here the string is set depending on what was chosen by the user
shinyServer(function(input, output, session) {
label1_new <- reactive({
if (input$variable1=="pp_pmw") return("PP pmw")
if (input$variable1=="perc_pp*100") return("PP percent")
if (input$variable1=="formality") return("Formality")
})
output$distPlot <- renderPlot({
x <- as.numeric(unlist(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', xlab=label1_new())
})
})
(same ui.r)