R闪亮的应用程序与rCharts

时间:2014-06-08 02:04:00

标签: r shiny rcharts

我可以使用rCharts创建此图表:

library(rCharts)
X <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                       8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
                                       3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1", "2", "3", "4", 
                                                                                    "5", "6", "7", "8", "9", "10"), class = "factor"), Var2 = structure(c(1L, 
                                                                                                                                                          1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                                          2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control", 
                                                                                                                                                                                                                          "treatment1", "treatment2"), class = "factor"), Freq = c(0L, 
                                                                                                                                                                                                                                                                                   0L, 3L, 2L, 6L, 9L, 13L, 36L, 50L, 497L, 0L, 2L, 1L, 3L, 6L, 
                                                                                                                                                                                                                                                                                   4L, 11L, 29L, 50L, 499L, 1L, 2L, 0L, 2L, 5L, 6L, 12L, 22L, 63L, 
                                                                                                                                                                                                                                                                                   490L)), .Names = c("Var1", "Var2", "Freq"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                             -30L), class = "data.frame")
n1<-nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
print(n1)

现在我试图嵌入一个闪亮的应用程序。我可以使用ggplot2做一个闪亮的应用程序,但我不确定如何打印rCharts图。

这是我现在拥有的闪亮代码:

#server.R
library(rCharts)
X <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                       8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
                                       3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1", "2", "3", "4", 
                                                                                    "5", "6", "7", "8", "9", "10"), class = "factor"), Var2 = structure(c(1L, 
                                                                                                                                                          1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                                          2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control", 
                                                                                                                                                                                                                          "treatment1", "treatment2"), class = "factor"), Freq = c(0L, 
                                                                                                                                                                                                                                                                                   0L, 3L, 2L, 6L, 9L, 13L, 36L, 50L, 497L, 0L, 2L, 1L, 3L, 6L, 
                                                                                                                                                                                                                                                                                   4L, 11L, 29L, 50L, 499L, 1L, 2L, 0L, 2L, 5L, 6L, 12L, 22L, 63L, 
                                                                                                                                                                                                                                                                                   490L)), .Names = c("Var1", "Var2", "Freq"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                             -30L), class = "data.frame")

shinyServer(
  function(input, output) {
    output$histogram <- renderPlot({
      # You can access the value of the widget with input$select, e.g.
      output$value <- renderPrint({ input$select })
      n2 <- nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
      n2$set(dom = "histogram")
      return(n2)
      })
  }
)

#ui.R
shinyUI(fluidPage(
  titlePanel("Quiz 3 grades distribution"),

  sidebarLayout(
    sidebarPanel(
      helpText("Quiz 3 grade distribution by treatment group"),    
      selectInput("select", label = h3("Select box"), 
                  choices = list("All" = 0, "Not Perfect" = 1, "Perfect" = 2), 
                  selected = 0)
    ),

    mainPanel(plotOutput("histogram"))
  )
))

我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:3)

使用renderChart2showOutputshiny中显示nvd3图。使用renderChart2并不需要使用$set(dom = ....

library(rCharts)
library(shiny)
X <- data.frame(Var1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L,3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L),
                Var2 = structure(c(1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control","treatment1", "treatment2"), class = "factor"),
                Freq = c(0L,0L, 3L, 2L, 6L, 9L, 13L, 36L, 50L, 497L, 0L, 2L, 1L, 3L, 6L, 4L, 11L, 29L, 50L, 499L, 1L, 2L, 0L, 2L, 5L, 6L, 12L, 22L, 63L,490L)
)
runApp(
  list(ui = fluidPage(
    titlePanel("Quiz 3 grades distribution"),

    sidebarLayout(
      sidebarPanel(
        helpText("Quiz 3 grade distribution by treatment group"),    
        selectInput("select", label = h3("Select box"), 
                    choices = list("All" = 0, "Not Perfect" = 1, "Perfect" = 2), 
                    selected = 0)
      ),

      mainPanel(
        showOutput("histogram","Nvd3")
      )
      )
  ),
  server = shinyServer(
    function(input, output, session) {
      output$histogram <- renderChart2({
        n2 <- nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
        n2
      })
    }
  )

  )
)

nPlot in shiny