根据被动输入修改我的rChart?

时间:2015-10-15 17:10:52

标签: r shiny rcharts

我遇到了一些我写过的代码。

以下是数据集的示例:https://docs.google.com/spreadsheets/d/1C_P5xxzYr7HOkaZFfFiDhanqDSuSIrd2UkiC-6_G2q0/edit?usp=sharing

目的: 我有一个dataset,其中包含Purchase_Monthcandyfreq列在该月份购买糖果类型的次数。

我有一个rPlot,我根据SelectInput中选择的糖果栏进行更改。并根据当月购买糖果的次数输出折线图。

我目前的代码如下,但它告诉我找不到candyCount

        ## ui.R ##
        library(shinydashboard)
        library(rCharts)

        dashboardPage(
          dashboardHeader(title = "Dashboard"),

          dashboardSidebar(
            width = 150,
            sidebarMenu(
              menuItem("Dashboard", tabName = "dashboard", icon = icon("bar-chart"))
            )

          ),

          dashboardBody(

          sidebarPanel(

              htmlOutput("candy")

            ),
            mainPanel(
              showOutput("plot2", "polycharts")
            ))




            )

    ##server.R##   
        library(rCharts)
        library(ggplot2)
        library(ggvis)

    server <- function(input, output, session) { 

      output$candy <- renderUI({

        available2 <- dataset[(dataset$candy == input$candy), "candy"]

        selectInput(
          inputId = "candy",
          label = "Choose a candy:  ",
          choices = sort(as.character(unique(available2))),
          selected = unique(available2[1])
        )
      })
      observeEvent(input$candy, {
    candyChoice<- toString(input$customer_issue)
    print(candyChoice)
    candyCount<- dataset[dataset$candy == candyChoice, ]
  })
      })

      output$plot2 <- renderChart2({
        p2 <- rPlot(freq~purchase_month, data = candyCount, type = 'line')
        p2$guides(y = list(min = 0, title = ""))
        p2$guides(y = list(title = sprintf("%s Claims",input$candy)))
        p2$addParams(height = 300, dom = 'chart2')
        return(p2)
      })


      }

更新数据:为什么这不起作用?

  candyCount<- reactive({
    dataset[dataset$candy == input$candy, ]
    })

  output$plot2 <- renderChart2({
    p2 <- rPlot(freq~purchase, data = candyCount(), type = 'line')
    p2$guides(y = list(min = 0, title = ""))
    p2$guides(y = list(title = ""))
    p2$addParams(height = 300, dom = 'chart2')
    return(p2)
  })

2 个答案:

答案 0 :(得分:1)

available2,您正在使用dataset$candy == input$candy过滤有关所选糖果的数据。但是,您使用相同的available2来确定choices处的selectInputavailable2 <- dataset[, "candy"]。我猜你想要的是:{{1}}。

答案 1 :(得分:1)

output$candy <- renderUI({

    available2 <- dataset[(dataset$candy == input$candy), "candy"]

    selectInput(
      inputId = "candy",
      label = "Choose a candy:  ",
      choices = sort(as.character(unique(available2))),
      selected = unique(available2[1])
    )
  })

在上面,您尝试按输入中的输入进行子集化。 selectInput需要位于UI.R内。

您可能会觉得有用的基本示例。

library(shiny)
df <- read.csv("/path/to/my.csv")

ui <- shinyUI(pageWithSidebar(

       headerPanel('Candy Data'),
       sidebarPanel(
             selectInput('candy', 'Candy', unique(as.character(df[,2])), selected = "Twix")

         ),
       mainPanel(
             plotOutput('plot1')
         )
 ))

server <- shinyServer(function(input, output, session) {
  selectedData <- reactive({
    df[which(df[,2] == input$candy),3]
  })

  output$plot1 <- renderPlot({
    barplot(selectedData())
  })

})

shinyApp(ui, server)

在上面的示例中,ui呈现了selectInput,其ID为candy。值,即所选的糖果现在分配给input$candy范围。在server中,我们有一个reactive函数可以监视任何input更改。当用户选择新功能此功能时,df[which(df[,2] == input$candy),3]会说“通过新input$candy”对我的数据框df进行子集化。现在已将其分配给selectedData()。最后我们渲染一下boxplot。

<强> 修改

server.R

require(rCharts)
options(RCHART_WIDTH = 500)
df <- read.csv("path/to/my.csv")
shinyServer(function(input, output, session) {
  selectedData <- reactive({
    df[which(df[,2] == input$candy),]
  })

  output$plot1 <- renderChart({
    p <- rPlot(freq~purchase_month, data = selectedData(), type = "line")
    p$addParams(dom = 'plot1')
    return(p)
  })
})

ui.R

require(rCharts)
options(RCHART_LIB = 'polycharts')
shinyUI(pageWithSidebar(

  headerPanel('Candy Data'),
  sidebarPanel(
    selectInput('candy', 'Candy', unique(as.character(df[,2])), selected = "Twix")

  ),
  mainPanel(
    showOutput('plot1', 'polycharts')
  )
))

将文件保存在目录中,然后保存runApp