闪亮的多个条件选择标准

时间:2018-03-11 02:15:48

标签: r shiny

可能很简单:

我有一个data.frame,例如:

set.seed(1)

df <- data.frame(name=c("A","A","B","C","B","A"),id=1:6,rep1=rnorm(6),rep2=rnorm(6),rep3=rnorm(6))

UI R shiny的{​​{1}}部分,我想要一个列出server的下拉菜单,以及然后给出该选择,在第二个下拉菜单中,我想列出与unique(df$name)选择对应的所有df$id(即,如果所选名称为df$name,这将是:selected.name)。然后给出这两个选择(dplyr::filter(df,name == selected.name)$id中的唯一行)我想执行df,执行此函数来绘制给定的选择:

server

此处plotData <- function(selected.df) { plot.df <- reshape2::melt(dplyr::select(selected.df,-name,-id)) ggplot2::ggplot(plot.df,ggplot2::aes(x=variable,y=value))+ggplot2::geom_point()+ggplot2::theme_minimal() } shiny我正在尝试:

code

当我跑:server <- function(input, output) { output$id <- renderUI({ selectInput("id", "ID", choices = unique(dplyr::filter(df,name == input$name)$id)) }) output$plot <- renderPlot({ plotData(selected.df=dplyr::filter(df,name == input$name,id == output$id)) }) } ui <- fluidPage( # App title ---- titlePanel("Results Explorer"), # Sidebar layout with a input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( # select name selectInput("name", "Name", choices = unique(df$name)), uiOutput("id") ), # Main panel for displaying outputs ---- mainPanel( plotOutput("plot") ) ) ) 时,我收到错误:

shinyApp(ui = ui, server = server)

缺少什么?

2 个答案:

答案 0 :(得分:1)

此处的选项包含“服务器”中的renderUI和“ui”中的uiOuput

-ui

library(shiny)
library(dplyr)
library(ggplot2)
ui <- fluidPage(

  # App title ----
  titlePanel("Results Explorer"),

  # Sidebar layout with a input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # select name


      selectInput("name", "Name", choices = unique(df$name)),

      uiOutput("idselection")



      # select id - this is where I need help


    ),

    # Main panel for displaying outputs ----
    mainPanel(
      # ShinyServer part
      plotOutput("plot")

    )
  )
)

-server

server = function(input, output) {

  output$idselection <- renderUI({
      selectInput("id", "ID", choices = unique(df$id[df$name ==input$name]))
  }) 

 output$plot <- renderPlot({

   df %>%
      count(name) %>%
      ggplot(., aes(x = name, y = n, fill = name)) +
           geom_bar(stat = 'identity') +
           theme_bw()


 })



}

shinyApp(ui = ui, server = server)

-output

enter image description here

答案 1 :(得分:0)

好的,微小修复:

创建数据:     set.seed(1)

df <- data.frame(name=c("A","A","B","C","B","A"),id=1:6,rep1=rnorm(6),rep2=rnorm(6),rep3=rnorm(6))

服务器将执行的功能:

plotData <- function(selected.df)
{
  plot.df <- reshape2::melt(dplyr::select(selected.df,-name,-id))
  ggplot2::ggplot(plot.df,ggplot2::aes(x=variable,y=value))+ggplot2::geom_point()+ggplot2::theme_minimal()
}

shiny code

server <- function(input, output)
{
  output$id <- renderUI({
    selectInput("id", "ID", choices = unique(dplyr::filter(df,name == input$name)$id))
  })
  output$plot <- renderPlot({
    plotData(selected.df=dplyr::filter(df,name == input$name,id == input$id))
  })
}


ui <- fluidPage(

  # App title ----
  titlePanel("Results Explorer"),

  # Sidebar layout with a input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # select name
      selectInput("name", "Name", choices = unique(df$name)),
      uiOutput("id")
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      plotOutput("plot")
    )
  )
)