首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于data.table的闪亮更新图

基于data.table的闪亮更新图
EN

Stack Overflow用户
提问于 2017-06-11 06:23:49
回答 0查看 2.1K关注 0票数 0

在我的示例应用程序中,我让用户在第一个选项卡中输入一些内容并生成一个data.table。在第二个选项卡中,我想根据data.table显示绘图。我有一段相当困难的时间来正确地处理反应性。不幸的是,在这一点上我得到了error: Operation not allowed without an active reactive context.

请帮助我或给我提示我做错了什么。

数据:

代码语言:javascript
复制
tdata <- data.table(fruit = c("Apple", "Apple", "Apple", "Apple", "Apple", "Apple", "Apple", "Apple","Banana", "Banana","Banana","Banana","Banana", "Banana","Banana","Banana"), 
                    Fertilizer = c(1,2,4,3,2,2,2,2,1,4,3,2,4,4,3,1), 
                    amount = c(2,3,4,7,1,34,33,21,12,32,22,17,14,9,22,6),
                    red = rep(c("+","+","-","-"),4),
                    green = rep(c("+","-"),8))
tdata[, grp := do.call(paste, c(list(sep="\n"),.SD)),.SDcols = 4:5]

用户界面:

代码语言:javascript
复制
library(shiny)
library(data.table)
library(DT)

ui <- (fluidPage(tagList(
  sidebarLayout(
    sidebarPanel(uiOutput("file_input")),
    mainPanel(
      tabsetPanel(
        tabPanel("Data",dataTableOutput('fruit_table') ),
        tabPanel("Plot", plotOutput('barPlot'))

  ))))))

服务器:

代码语言:javascript
复制
server <- function(input, output) {

  fileData <- reactive(
    return(tdata)
  )

  output$file_input <- renderUI ({
    if(is.null(fileData())){
      return()
    }else{
      tagList(
        checkboxGroupInput(inputId = "fruit",
                           label = "fruit",
                           choices = c(unique(fileData()[,get("fruit")])),
                           selected = fileData()[1, 1, with = FALSE]),
        checkboxGroupInput(inputId = "tube",
                           label = "Fertilizer",
                           choices = unique(fileData()[,get("Fertilizer")]),
                           selected = fileData()[1, 3, with = F]),
        ###build checkboxes from Loop:
        lapply(1:(length(fileData())-4), function(i) {
          checkboxGroupInput(inputId = paste0("color",i),
                             label = colnames(fileData()[,i+3, with = FALSE]),
                             choices = c(unique(fileData()[,get(colnames(fileData()[,i+3, with = FALSE]))])),
                             inline = TRUE,
                             selected = fileData()[1, i+3, with = FALSE])
        }))}})

  output$fruit_table <- renderDataTable({
    if(is.null(fileData())){
      return(NULL)
    }else{

      validate(
        need(input$fruit, 'Check at least one fruit'),
        need(input$tube, 'Check at least one Fertilizer'),
        ####loop not working in here
        need(input$color1, "Check at least one !"), 
        need(input$color2, "Check at least one !")
      )

      filter_expr <- TRUE

      if (!(is.null(input$fruit))) {
        filter_expr <- filter_expr & fileData()[,fruit] %in% input$fruit
        #print((input$fruit))
      }
      if (!(is.null(input$tube))) {
        filter_expr <- filter_expr & fileData()[,Fertilizer] %in% input$tube
      }

      ##non-loop-verison
      if (!(is.null(input$color1))) {
        filter_expr <- filter_expr & fileData()[,red] %in% input$color1
      }

      if (!(is.null(input$color2))) {
        filter_expr <- filter_expr & fileData()[,green] %in% input$color2
       }

      datatable(fileData()[filter_expr,],options = list(pageLength = 25))
    }})

  plot.dat <- reactiveValues(main = NULL)
  plot.dat$main <- ggplot(data = fileData(), mapping = aes( x = fileData()[,grp], y =fileData()[,amount]))+
    geom_boxplot( stat = 'boxplot',
                  position = position_dodge(width=0.8),
                  width = 0.55) 
  observe({

    output$barPlot <- renderPlot({
      if(is.null(fileData())){
        return(NULL)
      }else{

        validate(
          need(input$fruit, 'Check at least one fruit'),
          need(input$tube, 'Check at least one Fertilizer'),
          need(input$color1, "Check at least one !"), 
          need(input$color2, "Check at least one !")
        )

        plot.dat$main

  }})
})
}
shinyApp(ui = ui, server = server

)

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44478631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档