从下拉列表中选择一个变量,并将其作为参数传递到R闪亮的reactivePlot中 [英] select a variable from drop-down and pass it as an argument in reactivePlot in R Shiny

查看:11
本文介绍了从下拉列表中选择一个变量,并将其作为参数传递到R闪亮的reactivePlot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于R SHINY应用程序的一般问题:我有一个ID列表,终端用户可以在下拉菜单(selectInput)中选择,我希望根据这个ID做一个相应的绘图。换句话说,选择的变量将作为参数传递到Plot函数中,每次我选择不同的ID,绘图也会改变。我能知道这在闪亮是否可行吗?如果有人能就类似的问题提供一个有效的例子,我将不胜感激。谢谢!

推荐答案

这里是一个使用DropDown作为参数的示例工作示例。可以在http://glimmer.rstudio.com/bishwamitrad/ggplot2browser/

上查看此程序的输出

ui.R

library(shiny)
library(ggplot2)

## Define UI for miles per gallon application

dataset <- diamonds

title <- "Diamonds data Analysis"

## Define UI for application that plots random distributions

shinyUI(pageWithSidebar(

  ## Application title
  headerPanel(title),

  ## Sidebar with a slider input for number of observations

  sidebarPanel (

    sliderInput('sampleSize','Sample Size', min=1, max=nrow(dataset),
                value=min(1000,nrow(dataset)),
                step=500,
                round=0),

    selectInput('x','X',names(dataset)),
    selectInput('y','Y',names(dataset),
                names(dataset)[[2]]),

    selectInput('color','Color',c('None',names(dataset))),

    selectInput('shape','Shape',c('None',names(dataset))),

    checkboxInput('jitter','Jitter'),
    checkboxInput('smooth','Smooth'),

    selectInput('facet_col','Facet Column',
                c(None='.',names(dataset))),

    selectInput('facet_row','Facet Row',
                c(None='.',names(dataset)))

  ),

  ## Show a plot of the generated distribution

  mainPanel(plotOutput('plot',height="700px"))


)

)

server.R

library(shiny)
library(ggplot2)

## Define server logic required to generate and plot a random distribution
shinyServer(function(input,output) {

  dataset <- reactive(function(){
    diamonds[sample(nrow(diamonds),input$sampleSize),]
  })

output$plot <- renderPlot(function(){

  p <- ggplot(dataset(),aes_string(x=input$x, y=input$y))+geom_point()

  if(input$color != 'None')
    p <- p + aes_string(color=input$color)

  if (input$shape != 'None')
    p <- p + aes_string(shape=input$shape)

  facets <- paste(input$facet_row, '~', input$facet_col)

  if (facets != '. ~ .')
    p <- p + facet_grid(facets)

  if (input$jitter)
    p <- p + geom_jitter()

  if (input$smooth)
    p <- p + geom_smooth()


  print(p)

})


})

这篇关于从下拉列表中选择一个变量,并将其作为参数传递到R闪亮的reactivePlot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆