【问题标题】:Define column values as input for reactive shiny plot将列值定义为反应性闪亮图的输入
【发布时间】:2020-08-31 14:08:58
【问题描述】:

我想启动一个闪亮的应用程序进行练习,用户可以从下拉列表中选择钻石数据集(来自 ggplot2)的“切割”列中的值。

我的用户界面如下:

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Reactive Boxplot"),

  # Show a boxplot of the selected cut
  mainPanel(
    selectInput("column", label = h3("Column to plot"),
                choices = c("", diamonds$cut),
                selected = 1,
                width='55%',
                multiple = FALSE),
    plotOutput("diamondshist")
  )
)
)

我不知道如何将输入变量定义为钻石数据集“切割”列中的五个不同值。对此有何意见?

我的服务器文件如下所示。我想我还需要调整绘图的输入数据。

library(shiny)
library(ggplot2)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  compute_plot <- reactive({
    if (input$column != ""){
      ggplot(diamonds[, input$column])+
        labs(title = "From diamonds dataset")+
        geom_boxplot(aes(x = cut, y = price))+
        scale_y_reverse()
    }
  })


  output$diamondshist <- renderPlot({
    compute_plot();
  })

})

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    我想这就是你所追求的:

    • 通过diamonds$cut 的级别作为输入选择
    • diamonds 数据集子集到所选剪辑
    library(shiny)
    library(ggplot2)
    
    # Define UI for application that draws a histogram
    ui=shinyUI(fluidPage(
    
        # Application title
        titlePanel("Reactive Boxplot"),
    
        # Show a boxplot of the selected cut
        mainPanel(
            selectInput("column", label = h3("Column to plot"),
                        choices = c("", levels(diamonds$cut)),
                        selected = NULL,
                        width='55%',
                        multiple = FALSE),
            plotOutput("diamondshist")
        )
    )
    )
    
    
    
    
    # Define server logic required to draw a histogram
    server=shinyServer(function(input, output) {
    
        compute_plot <- reactive({
            if (input$column != ""){
                ggplot(subset(diamonds, cut==input$column))+
                    labs(title = "From diamonds dataset")+
                    geom_boxplot(aes(x = cut, y = price))+
                    scale_y_reverse()
            }
        })
    
    
        output$diamondshist <- renderPlot({
            compute_plot();
        })
    
    })
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2020-12-28
      • 2017-11-24
      • 2017-01-24
      • 2020-08-18
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 2019-07-19
      相关资源
      最近更新 更多