2015-09-07 109 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") 
    } 
    }) 

}) 

反應性似乎有效,現在您必須修復您的數據。

乾杯