如何将数据从一个`reactive()`传递到R闪亮的另一个?

时间:2016-01-06 06:56:44

标签: r shiny

一个闪亮的应用,它接收用户输入并将列名发送到selectizeInput()框。然后,用户选择几个列名称,并在同一个应用程序的另一个部分/选项卡中创建数据的子集。

该应用的用户界面包含以下部分:

  1. 导航栏
  2. 导航栏的一个选项卡,用于选择数据和列
  3. 用于创建数据子集和绘图的其他选项卡。
  4. ui.R

     library(markdown)
    require(XLConnect)
    
    
    shinyUI(fluidPage(
      # Application title
      img(class = "img-polaroid",
          src = paste0(
          "http://www.aithent.com/wp-content/uploads/2015/05/aithent-website-200x80.png"
        )
      ),
      navbarPage(
        "Project Vanguard",
        tabPanel(
          "Table",
          titlePanel("Select Data"),
          sidebarLayout(
            sidebarPanel(
              selectInput(
                "fileType",
                "Select File Type:",
                c("MS Excel Worksheet (xls,xlsx)" = "xls",
                  "Text/CSV (.csv)" = "csv")
              ),
              fileInput(
                'file1',
                'Choose file to upload',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  'text/tab-separated-values',
                  'text/plain',
                  '.csv',
                  '.tsv',
                  'application/vnd.ms-excel',
                  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                  '.xlsx',
                  '.xls'
                )
              ),
              tags$hr(),
              checkboxInput('header', 'Header', TRUE),
              radioButtons('sep', 'Separator',
                           c(Comma = ',',
                             Semicolon = ';',
                             Tab = '\t'
                           ),','),
              radioButtons(
                'quote',
                'Quote',
                c(None = '',
                  'Double Quote' = '"',
                  'Single Quote' = "'"
                  ),'"'
              ),
              tags$hr(),
              uiOutput("selector1"),
              uiOutput("selector2")
              ),
            mainPanel(dataTableOutput('table'))
          )),
        tabPanel("Plot",
                 sidebarLayout(
                   sidebarPanel(radioButtons(
                     "plotType", "Plot type",
                     c("Scatter" = "p", "Line" = "l"))),
                   mainPanel(dataTableOutput("ResultTab"),
                             plotOutput("plot")))),
        navbarMenu("More",
                   tabPanel(
                     "Summary",
                     verbatimTextOutput("summary"))))
    ))
    

    server.R

    library(sqldf)
    options(scipen = 999)
    shinyServer(function(input, output, session){
      output$plot <- renderPlot({
        plot(data$Count, data$Sales, type = input$plotType)
      })
      output$summary <- renderPrint({
        summary(data)
      })
      data <- reactive({
        inFile <- input$file1
        if (is.null(inFile))
          return(NULL)
        if (input$fileType == 'csv') {
          table1 <- read.csv(
            inFile$datapath,
            header = input$header,
            sep = input$sep,
            quote = input$quote
          )
          return(table1)
        } else if (input$fileType == 'xls') {
          table1 <- readWorksheetFromFile(inFile$datapath, sheet = 1)
        }
      })
    
      output$table <- renderDataTable({
        data()
      })
    
      output$selector1 <- renderUI({
        selectizeInput("GroupVar",
                       "Choose Group Variables:",
                       as.list(colnames(data())),
                       multiple = TRUE)
      })
      output$selector2 <- renderUI({
        selectizeInput("AnalysisVar",
                       "Choose Analysis Variables:",
                       as.list(colnames(data())),
                       multiple = TRUE)
      })
    
      ResultSet <- reactive({
        Src <- data()
        GVars <- as.character(input$selector1)
        Avars <- as.character(input$selector2)
        gvs <- paste(unlist(strsplit(GVars, ' ')), collapse = ",")
        avs <-
          paste0('sum(',
                 strsplit(Avars, ' '),
                 ') as SumOf',
                 strsplit(Avars, ' '),
                 collapse = ",")
        sqlStat <-
          paste0("SELECT ", gvs, ',', avs, " FROM Src GROUP BY ", gvs)
        Result <- sqldf(sqlStat)
        return(Result)
      })
      output$ResultTab <- renderDataTable({
        ResultSet()
      })
    })
    

    在代码中,我收到以下错误:

    Error in sqliteSendQuery(con, statement, bind.data) : error in statement: near ",": syntax error

    我希望知道两件事是我在SQL中的错误,因为: 1.错误的数据传递,即ResultSet的被动Src<-data()中的错误使用情况? 要么 2. selectizeInput()语句GVars <- as.character(input$selector1)AdView adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId(""); parentView.addView(adView); AdRequest adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest); 的引用和使用不当。

1 个答案:

答案 0 :(得分:2)

要获取用户在selectize中选择的内容,您需要参考id的{​​{1}}。您应该使用selectizeInput代替input$GroupVar

您可以在input$selector1中添加print(sqlStat),以确保您的SQL语句正常。

同样,反应式表达式是闭包,以获得调用它们所需的值。对于你的ResultSet应该是:

renderPlot