使用滑块循环通过函数生成的图
cycle through plots generated with function using slider
我在 renderPlot 中 运行 我闪亮的应用程序中有五个 R 脚本。每个脚本中都包含一系列独特的突变以及 ggplot 代码,以生成具有特定分位数值的箱线图。每个都是接受我的输入变量的唯一函数。
我想在 Shiny 中获取脚本,并根据分位数值的用户滑块 selection 在绘图中循环。
我在单独生成每个图时没有遇到任何问题,问题是 运行 它们在一起。
“名称”的每个变量在这些函数中的每一个中都有唯一的值。所以理想情况下,我想 select 一个变量并使用滑块查看它的每个图。
是否有捷径可寻?我已经尝试 link 每个特定情节的滑块输入,但没有任何运气。
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)
有几种方法可以做到这一点。我认为最简洁的方式
我能想到的就是使用get()
命令。当以字符串形式调用 (input$variable
) 时,这将使您能够在您的环境中找到一个对象。唯一的新项目是顶部的 reprex 和服务器主体。如果我误解了提示,请告诉我,我会尝试更新我的答案。
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)
我在 renderPlot 中 运行 我闪亮的应用程序中有五个 R 脚本。每个脚本中都包含一系列独特的突变以及 ggplot 代码,以生成具有特定分位数值的箱线图。每个都是接受我的输入变量的唯一函数。 我想在 Shiny 中获取脚本,并根据分位数值的用户滑块 selection 在绘图中循环。 我在单独生成每个图时没有遇到任何问题,问题是 运行 它们在一起。 “名称”的每个变量在这些函数中的每一个中都有唯一的值。所以理想情况下,我想 select 一个变量并使用滑块查看它的每个图。 是否有捷径可寻?我已经尝试 link 每个特定情节的滑块输入,但没有任何运气。
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)
有几种方法可以做到这一点。我认为最简洁的方式
我能想到的就是使用get()
命令。当以字符串形式调用 (input$variable
) 时,这将使您能够在您的环境中找到一个对象。唯一的新项目是顶部的 reprex 和服务器主体。如果我误解了提示,请告诉我,我会尝试更新我的答案。
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)