Reputation: 423
I have five R scripts sourced in my shiny app that I am running in renderPlot. A series of unique mutations are contained in each script along with ggplot code to generate a boxplot with specific quantile values. Each is a unique function that accepts my input$variable. I would like to source the scripts in Shiny and cycle through the plots based on the user slider selection of quantile values. I haven't had any trouble generating each plot individually, the problem is running them all together. Each variable of "Name" has a unique value in each of these functions. So ideally, I would like to select a variable and view each of its plots with the slider. Is there an easy way to do this? I have tried to link the slider input with each specific plot but haven't had any luck.
source("s_75.R")
source("s_80.R")
source("s_85.R")
source("s_90.R")
source("s_95.R")
# Define UI for pair interaction app ----
ui <- pageWithSidebar(
# App title ----
headerPanel("Set"),
# Sidebar panel for inputs ----
sidebarPanel(# Input: Selector for variable to plot against ----
selectInput("variable", "Name:", qc$S),
sliderInput("quantile", "Quantile Range:",
min = 75, max = 95, value = c(85), step = 5)),
# Main panel for displaying outputs ----
mainPanel(
h1("Header 1"),
plotOutput("plot", width = "100%")
))
# Define server logic to plot various variables against
server <- function(input, output, session) {
output$plot <- renderPlot(s_75(input$variable),
height = 600, width = 800)
output$plot2 <- renderPlot(s_80(input$variable),
height = 600, width = 800)
output$plot3 <- renderPlot(s_85(input$variable),
height = 600, width = 800)
output$plot4 <- renderPlot(s_90(input$variable),
height = 600, width = 800)
output$plot5 <- renderPlot(s_95(input$variable),
height = 600, width = 800)
shinyApp(ui, server)
Upvotes: 0
Views: 48
Reputation: 3236
There are a few ways you could do this. I think the most concise way
I could think of is to use the get()
command. This will let you find an object in your environment when called as a string (input$variable
). The only new items are the reprex at the top and the server body. If I misunderstood the prompt, let me know and I'll try to update my answer.
library(shiny)
library(tidyverse)
# reprex ----
qc <-
mtcars |>
pivot_longer(everything()) |>
print()
s_75 <- function(var) ggplot(mtcars, aes(get(var))) + geom_bar()
s_80 <- function(var) ggplot(mtcars, aes(get(var))) + geom_dotplot()
s_85 <- function(var) ggplot(mtcars, aes(get(var))) + geom_histogram()
s_90 <- function(var) ggplot(mtcars, aes(get(var), 1)) + geom_count()
s_95 <- function(var) ggplot(mtcars, aes(get(var))) + geom_density()
# ui ----
ui <- pageWithSidebar(
# App title ----
headerPanel("Set"),
# Sidebar panel for inputs ----
sidebarPanel( # Input: Selector for variable to plot against ----
selectInput("variable", "Name:", unique(qc$name)),
sliderInput("quantile", "Quantile Range:",
min = 75, max = 95, value = c(85), step = 5
)
),
# Main panel for displaying outputs ----
mainPanel(
h1("Header 1"),
plotOutput("plot", width = "100%")
)
)
# server ----
# Define server logic to plot various variables against
server <- function(input, output, session) {
fn <- reactive(get(paste0("s_", input$quantile)))
output$plot <- renderPlot(fn()(input$variable), height = 600, width = 800)
# ^^^ note that the reactive value goes fn()(var)
}
shinyApp(ui, server)
Upvotes: 1