闪亮的情节不会随着输入而改变

时间:2018-08-14 19:11:13

标签: r shiny

我绝对是Shiny的初学者,因此,感谢您的耐心配合以及对我的问题的任何建议。这是我用来输出ggplot的服务器函数,该函数可以单独运行,但是当我更改输入时根本不改变:

server <- function(input, output) {
  output$plooot<-renderPlot({
    df = df %>%
    group_by(input$Category,Type) %>%
    summarise(Distribution=sum(Distribution))
    ggplot(df,aes(input$Category,Distribution,fill=Type))+geom_bar(stat="identity",position="dodge")})
}

shinyApp(ui=ui,server=server)

这也是我的ui函数,仅供参考:

ui <- fluidPage(    

  titlePanel("chart"),

  # Generate a row with a sidebar
  sidebarLayout(      

    # Define the sidebar with one input
    sidebarPanel(
      selectInput("Category","Category:",choices=c("a","b","c","d","e","f")),
      selectInput("a","a:", choices=unique(Table$a), selected="All"),
      selectInput("b","b:", choices=unique(Table$b), selected="All"),
      selectInput("c","c:", choices=unique(Table$c), selected="All"),
      selectInput("d","d:", choices=unique(Table$d), selected="All"),
      selectInput("e","e:", choices=unique(Table$e), selected="All"),
      selectInput("f","f:", choices=unique(Table$f), selected="All")
    ),

    # Create a spot for the barplot
    mainPanel(
      plotOutput("plooot")  
    )

  )
)

不幸的是,出于法律原因,我无法发布数据,但这是我想要的和我拥有的两个图: This is want I want. This separates each category into its options and then plots two bars (the fill) for each option

This is what I get from shiny. It separates the entire dataset into the two fill options, and nothing changes when I change the inputs.

这可能是一个非常基本的错误,但是我很难理解自己在做什么。

2 个答案:

答案 0 :(得分:3)

我同意@AndS。,不太可能想要/需要重新分配给df = ...,但是几乎可以肯定地减少了您的数据。此外,input$Categorycharacter所期望的symbol而不是group_by。试试这个:

library(shiny)
library(dplyr)
library(ggplot2)

ui <- fluidPage(    
  titlePanel("chart"),
  # Generate a row with a sidebar
  sidebarLayout(      
    # Define the sidebar with one input
    sidebarPanel(
      selectInput("Category","Category:",choices=colnames(mtcars))
    ),
    # Create a spot for the barplot
    mainPanel(
      plotOutput("plooot")  
    )
  )
)

server <- function(input, output) {
  output$plooot<-renderPlot({
    req(input$Category)
    icq <- sym(input$Category)
    mtcars %>%
      group_by(!!!icq, vs) %>%
      summarise(disp=sum(disp)) %>%
      ggplot(aes_string(input$Category, "disp", fill="vs")) +
      geom_bar(stat="identity", position="dodge")
  })
}

shinyApp(ui=ui,server=server)

答案 1 :(得分:0)

不知道您的数据是什么样子,请参见下文。对于将受到用户输入影响的任何数据集,最好的做法是将其放入反应性表达式中。然后在输出图中使用该反应式。我还为您的选择添加了“ ALL”,并为您添加了if函数,以防您想像图片一样看到它们。

ui <- fluidPage(

  titlePanel("Chart"),
  sidebarLayout(
    sidebarPanel(
      selectInput("Category","Category:",choices=c("All","a","b","c","d","e","f"))
    ),
    mainPanel(
      plotOutput("Plot")
    )
  )
)

server <- function(input, output) {

  Distribution <- c(1,2,3,4,1,2,3,5,2,4)
  Category <- c("a","b","c","e","f","a","b","c","e","f")
  Type <- c("Blue","Blue","Blue","Blue","Blue","Red","Red","Red","Red","Red")

  df <- data.frame(Distribution ,Category,Type)

  df_subset <- reactive({

    if (input$Category == "All") {df}
    else{df[df$Category == input$Category,]}
  })

  output$Plot <- renderPlot({

    dat <- df_subset()

    dat <- dat %>%
      group_by(Category,Type) %>%
      summarise(Distribution=sum(Distribution))

    plot <- ggplot(dat,aes(Category,Distribution,fill=Type))+geom_bar(stat="identity",position="dodge")
    return(plot)
  })
}

shinyApp(ui=ui,server=server)

enter image description here enter image description here