【问题标题】:cycle through plots generated with function using slider使用滑块循环通过函数生成的图
【发布时间】:2022-01-24 06:08:02
【问题描述】:

我有五个 R 脚本来源于我在 renderPlot 中运行的闪亮应用程序。每个脚本中包含一系列独特的突变以及 ggplot 代码,以生成具有特定分位数值的箱线图。每个都是接受我的 input$variable 的独特函数。 我想在 Shiny 中获取脚本,并根据用户滑块选择的分位数值循环遍历绘图。 我没有任何问题单独生成每个情节,问题是一起运行它们。 “名称”的每个变量在每个函数中都有唯一的值。所以理想情况下,我想选择一个变量并使用滑块查看它的每个图。 是否有捷径可寻?我试图将滑块输入与每个特定的情节联系起来,但没有任何运气。


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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    有几种方法可以做到这一点。我认为最简洁的方式 我能想到的是使用get() 命令。这将让您在作为字符串 (input$variable) 调用时在您的环境中找到一个对象。唯一的新项目是顶部的代表和服务器主体。如果我误解了提示,请告诉我,我会尝试更新我的答案。

    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)
    

    【讨论】:

    • 完美运行,非常感谢!
    猜你喜欢
    • 2023-03-25
    • 2021-02-09
    • 2016-04-26
    • 2012-07-17
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多