Shiny: передача ввода $ var в aes () в ggplot2

Я пытаюсь написать блестящее приложение, которое отображает плотность переменной VAL по возрасту (ВОЗРАСТ) или полу (SEX). Пользователь выбирает «SEX» или «ВОЗРАСТ» в выпадающем меню, и я пытался использоватьfill = input$Group_select в ggplot и ggvis.

В ggplot:

output$plot <- renderPlot(ggplot(gdat[gdat$GFR==input$GFR_select,]) + 
                      aes(x= VAL, fill=input$Group_select) +
                      geom_density(adjust=input$slider1))

РЕДАКТИРОВАТЬ: как указывало docendo, это можно исправить для ggplot, используя aes_string:

output$plot <- renderPlot(ggplot(gdat[gdat$GFR==input$GFR_select,]) + 
                              aes(x= VAL) +
                              geom_density(adjust=input$slider1, aes_string(fill=input$Group_select))): 

В ggvis:

gvis <- reactive(subset(gdat, GFR==input$GFR_select) %>% 
                 ggvis(x = ~VAL) %>% group_by_(input$Group_select) %>%
                 layer_densities(adjust = input$slider1) %>%
                 add_axis("y", title = "Density", ticks="none") %>%
                 scale_numeric("x", domain = c(0, 230)) ) 
gvis %>% bind_shiny("ggvis", "ggvis_ui")

РЕШЕНИЕ: использование as.name (input $ Group_select) будет правильно отображать график!

Это (было) то, что предоставлено:Imgur ссылка на блестящий вывод, Интересно, что кажется, что group_by_ правильно интерпретирует ввод $ Group_select, тогда как ввод $ Group_select обрабатывается как константа вfill=input$Group_select

Любые идеи о том, как я могу получить графики для правильного рендеринга?

Вот полный блестящий код:

ui.R

library(shiny)
library(ggplot2)
library(dplyr)
library(ggvis)
shinyUI(fluidPage(
  titlePanel("eGFR in the ARIC study"),
  sidebarPanel(
    selectInput("Group_select",
                label=h3("By-Variable"),
                choices=c("AGE","SEX","ALL"),
                selected="SEX"),
    selectInput("GFR_select",
                label=h3("Creatinine Measure"),
                choices=c("BOTH", "CREATININE", "CYSTATIN", "MDRD"),
                selected="MDRD"),
    sliderInput("slider1", label = h3("Bandwith Adjustment"),
                min = 0.5, max = 4, value = 1)
  ),
  mainPanel(
    uiOutput("ggvis_ui"),
    ggvisOutput("ggvis"),
    plotOutput("plot")
  )
))

server.R

library(shiny)
source("helpers.R")
shinyServer(function(input, output) {
  gvis <- reactive(subset(gdat, GFR==input$GFR_select) %>% 
                     ggvis(x = ~VAL, fill = ~input$Group_select) %>% group_by_(input$Group_select) %>%
                     layer_densities(adjust = input$slider1) %>%
                     add_axis("y", title = "Density", ticks="none") %>%
                     scale_numeric("x", domain = c(0, 230)) ) 
  gvis %>% bind_shiny("ggvis", "ggvis_ui")
  output$plot <- renderPlot(ggplot(gdat[gdat$GFR==input$GFR_select,]) + 
                          aes(x= VAL, fill=input$Group_select) +


      geom_density(adjust=input$slider1))
})

Ответы на вопрос(1)

Ваш ответ на вопрос