【问题标题】:selecting dimensions of plot in Shiny在 Shiny 中选择绘图的尺寸
【发布时间】:2018-02-22 08:22:22
【问题描述】:

我正在开发一个 ShinyApp,我必须打印出很多类似的情节。我认为让用户通过 Input 选择所需的绘图 y 维度将是一个优雅的解决方案,因此可以避免大量类似的绘图输出。 我已经通过以下代码运行它:

library(tidyverse)
library(shiny)

set.seed(1)
name <- c("a", "b", "c")
v1 <- sample(1:100, 3)
v2 <- sample(1:100, 3)

df <- data_frame(name, v1, v2) 


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("selector")
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output){

  output$selector <- renderUI({

    selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
  })

  data <- reactive({

     df %>% 
      select(name, y = input$select)

  })

  output$plot <- renderPlot({

    ggplot(data())+
      geom_col(aes(x = name, y = y))+
      geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)

  })

}

shinyApp(ui = ui, server = server)

代码执行,应用按要求运行,但我仍然收到错误消息:

错误:input$select 必须解析为整数列位置, 不为空

我认为这是由于 select 命令同时针对变量名称和索引(通过 input$select)。

我想知道是否有更清洁或更优雅的方式来执行此操作。

【问题讨论】:

    标签: r shiny tidyverse


    【解决方案1】:

    您可以为此使用req(),请参阅该函数here 的文档。下面显示了一个工作示例。

    library(ggplot2)
    library(dplyr)
    library(shiny)
    
    set.seed(1)
    name <- c("a", "b", "c")
    v1 <- sample(1:100, 3)
    v2 <- sample(1:100, 3)
    
    df <- data_frame(name, v1, v2) 
    
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          uiOutput("selector")
        ),
        mainPanel(
          plotOutput("plot")
        )
      )
    )
    
    server <- function(input, output){
    
      output$selector <- renderUI({
    
        selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
      })
    
      data <- reactive({
    
        req(input$select)
    
        df %>% 
          select(name, y = input$select)
    
      })
    
      output$plot <- renderPlot({
        ggplot(data())+
          geom_col(aes(x = name, y = y))+
          geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)
    
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    或者,您可以简单地在 UI 中创建输入,但这可能无法实现,具体取决于您的应用程序:

    library(ggplot2)
    library(dplyr)
    library(shiny)
    
    set.seed(1)
    name <- c("a", "b", "c")
    v1 <- sample(1:100, 3)
    v2 <- sample(1:100, 3)
    
    df <- data_frame(name, v1, v2) 
    
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
        ),
        mainPanel(
          plotOutput("plot")
        )
      )
    )
    
    server <- function(input, output){
    
      data <- reactive({
    
        df %>% 
          select(name, y = input$select)
    
      })
    
      output$plot <- renderPlot({
        ggplot(data())+
          geom_col(aes(x = name, y = y))+
          geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)
    
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2013-03-23
      • 2018-07-02
      • 2021-04-26
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多