使用带闪亮的rCharts

时间:2014-09-12 18:06:39

标签: r shiny rcharts

我正在开发一个Shiny应用程序,但在与rCharts集成时遇到了麻烦。我用一个数据库“Cars93”制作了一个简单的应用程序,每个人都可以访问这个数据库来举例说明我的问题

我在应用程序中放了一个表和一个图表,该表仅用于测试数据是否正确。

我的问题是图表没有出现。

提前致谢!

路易斯

ui.R

library(shiny)
library(MASS)
require(rCharts)
options(RCHART_LIB = 'polycharts')
mydata<-Cars93

shinyUI(pageWithSidebar(
  headerPanel('RCharts - Test'),
  sidebarPanel(

    selectInput("Manufacturer2", "Select a Manufacturer", levels(droplevels(mydata$Manufacturer)),
                          selected = "Acura"
    ),
    selectInput("Type2", "Select a Type", levels(droplevels(mydata$Type))[3:4],selected = levels(droplevels(mydata$Type))[3]
    ),




    width = 3

  ),
  mainPanel(
    tabsetPanel(

      tabPanel("Table",dataTableOutput('mytable')),
      tabPanel("Chart",showOutput('myplot', 'polycharts'))




    )
  ))
)

server.R

library(shiny)
library(rCharts)
library(MASS)
options(RCHART_WIDTH = 800)
mydata<-Cars93
data2<-Cars93

shinyServer(function(input, output, session) {

  observe({
    updateSelectInput(session, "Type2", choices = levels(droplevels(mydata$Type[mydata$Manufacturer %in% input$Manufacturer2])),selected = levels(droplevels(mydata$Type[mydata$Manufacturer %in% input$Manufacturer2]))[1]
    )
  })

  output$mytable = renderDataTable({
    MANUFACTURER = input$Manufacturer2
    TYPE = input$Type2
    mydata<- mydata[mydata$Manufacturer %in% MANUFACTURER  & mydata$Type %in% TYPE,]
    mydata<-mydata[,c("Manufacturer","Type","Model","Weight")]
    mydata
  })

  output$myplot<- renderChart({
    MANUFACTURER = input$Manufacturer2
    TYPE = input$Type2
    mydata<- mydata[mydata$Manufacturer %in% MANUFACTURER  & mydata$Type %in% TYPE,]
    p1<-rPlot(Weight ~ Model, color = 'Model', data = mydata, type = 'bar')

    p1$guides(
      color = list(
        numticks = length(factor(mydata$Model))
      ),
      y = list(
        min = 0,
        max = 10
      )
    )
    return(p1)
  })

})

1 个答案:

答案 0 :(得分:2)

如果您在服务器功能中使用renderChart,则需要设置dom

p1$set(dom = "myplot")

其中dom应采用与output

分配renderChart相同的值

或者,可以使用方便的renderChart2代替renderChart,然后不需要设置dom