简体   繁体   中英

Multiple conditional selection criteria in shiny

Probably a simple one:

I have a data.frame such as this:

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))

In the UI part of the R shiny server I'd like to have a drop-down menu that lists unique(df$name) , and then given that selection, in a second drop-down menu I'd like to list all df$id that correspond to that df$name selection (ie, if the selected name is selected.name , this will be: dplyr::filter(df,name == selected.name)$id ). Then given these two selections (which are a unique row in df ) I'd like to execute server , which executes this function to plot the given selection:

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()
}

Here's the shiny code I'm trying:

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")
    )
  )
)

When I run: shinyApp(ui = ui, server = server) , I get the error:

Evaluation error: Reading objects from shinyoutput object not allowed..

What's missing?

Here the option would be have renderUI in the 'server' and uiOuput in 'ui'

-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

在此处输入图片说明

Ok, tiny fix:

Create data: 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))

Function that server will execute:

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")
    )
  )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM