如何与闪亮的R中的复选框进行交互?

时间:2015-09-07 13:42:49

标签: r shiny

我很难与R shiny中的复选框选项进行交互。我想要的是如果用户选择复选框选项,则图表应该与非检查图表不同。然而,即使我已经多次尝试,我也无法让它工作

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 个答案:

答案 0 :(得分:1)

您的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")
    } 
  })

})

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

干杯