2015-09-07 104 views
1

我很难与R闪亮中的复选框选项进行交互。我想要的是,如果用户选择了复选框选项,那么图表应该是与非检查图表相比不同的图表。但是我不能让即使我已经试了好几次如何与闪亮的R中的复选框进行交互?

ui.R

它的工作
library(shiny) 
library(ggplot2) 

shinyUI(fluidPage(

    # Application title 
    titlePanel("Demonstration of 2 dropdown menus"), 

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
    sidebarPanel(
     selectInput("element_id1", "select variable for x-axis", c("mpg", "cyl", "disp", "hp", "wt"), selected = "wt"), 
     selectInput("element_id2", "select variable for x-axis", c("mpg", "cyl", "disp", "hp", "wt"), selected = "mpg"), 
     checkboxGroupInput("variable", "Variable:", c("stat_smooth()")) 
    ), 

    # Show a plot of the generated distribution 
    mainPanel(h3("Outputs"), 
     textOutput("id1"), 
     textOutput("id2"), 
     plotOutput("plt") 
    ) 
) 
)) 

server.R

library(shiny) 
library(ggplot2) 

shinyServer(function(input, output) { 
    output$id1 <- renderText({ 
    sprintf("You have selected %s on the x-axis", input$element_id1) 
}) 
    output$id2 <- renderText({ 
    sprintf("You have selected %s on the y-axis", input$element_id2) 
    }) 
    output$plt <- renderPlot(
    ggplot(mtcars, aes_string(x = input$element_id1, y = input$element_id2)) + geom_point() + ggtitle("ggplot"), 

    if(input$variable) { 
     ggplot(mtcars, aes_string(x = input$element_id1, y = input$element_id2)) + geom_point() + geom_smooth() + ggtitle("ggplot") 
    } 
) 
}) 

回答

1

你server.R具有不匹配opeing({字符和错放。逗号

我固定它:

library(shiny) 
library(ggplot2) 

shinyServer(function(input, output) { 
    output$id1 <- renderText({ 
    sprintf("You have selected %s on the x-axis", input$element_id1) 
    }) 

    output$id2 <- renderText({ 
    sprintf("You have selected %s on the y-axis", input$element_id2) 
    }) 

    output$plt <- renderPlot({ 
    ggplot(mtcars, aes_string(x = input$element_id1, y = input$element_id2)) + geom_point() + ggtitle("ggplot") 
    if(input$variable) { 
     ggplot(mtcars, aes_string(x = input$element_id1, y = input$element_id2)) + geom_point() + geom_smooth() + ggtitle("ggplot") 
    } 
    }) 

}) 

反应性似乎有效,现在您必须修复您的数据。

干杯