Hi, I have a Shiny app that takes in several inputs and plots a graph upon clicking an actionButton. I want to have a graph displayed with default settings upon launching the application, but I can't seem to do this without getting rid of the actionButton entirely. However, I want to keep the actionButton because I want the user to be able to change as many inputs a they want before the application can react to it. What can I do?
Hi @sharitian. Below is one approach that loads a default plot upon launching an app. The example app plots mpg vs. cyl from the mtcars data set. Users can enter max values for cyl and/or mpg and click "Submit" to update the plot. Initially, the default values are 6 and 25, respectively. These values are established using reactiveVal()
, and each is updated upon clicking the button. The data in the plot is filtered based on these reactiveVal()
values, so when either is updated, the plot updates as well.
library(shiny)
library(tidyverse)
ui <- fluidPage(
numericInput('cylID', 'Enter cyl max', value = NA),
numericInput('mpgID', 'Enter mpg max', value = NA),
actionButton('btn', 'Submit'),
br(),
br(),
plotOutput('plot', width = '600px')
)
server <- function(input, output) {
# initial values
my_cyl = reactiveVal(6)
my_mpg = reactiveVal(25)
# update initial values on button click
observeEvent(input$btn, {
req(input$cylID)
my_cyl(input$cylID)
})
observeEvent(input$btn, {
req(input$mpgID)
my_mpg(input$mpgID)
})
# plot
output$plot = renderPlot({
myTitle = paste0('Records with cyl <= ', my_cyl(), ' and mpg <= ', my_mpg())
mtcars |>
filter(cyl <= my_cyl()) |>
filter(mpg <= my_mpg()) |>
ggplot(aes(x = factor(cyl), y = mpg)) +
geom_point(size = 2, color = 'tomato') +
labs(title = myTitle) +
theme_minimal()
})
}
shinyApp(ui, server)
Does this method is usefull for every types of apps do i used for my personal site free fire hack mod apk .
This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.