如何以反应的方式将多个变量从ui.r传递到有光泽的app中的server.r.

时间:2017-05-31 06:44:54

标签: r ggplot2 shiny

ui.r
这主要用于商店每天销售的数据分析

   shinyUI(fluidPage(
          titlePanel("Sales Analysis"),
          sidebarLayout(
            sidebarPanel(
              helpText("Create line graph for  
                       Sale."),
              selectInput(multiple='TRUE' ,"var", 
                          label = "Choose a year to display",
                          choices = c(Result),
                          selected = "Result[1]",
           selectInput( "var2", 
            label1 = "Choose a month to display",
            choices1 = c(Result2),
            selected1 = "Result2[1]")),
            mainPanel(
              plotOutput("lineplot"),
              print(Result),
              print(Result1)
            )
          )
        ))

server.r

这主要是针对商店每天销售的数据分析 使用闪亮的网络应用程序,所以我想尝试在ui.r中有多个下拉框,想要选择一个下拉,基于另一个下拉选择然后这些选定的下拉变量传递给server.r其中它应该在查询中传递为被动方式。

library(shiny)library for shiny web browser
library(DBI) database package
library(rJava)for connection basic package
library(RJDBC) 
library(scales)
library(ggplot2)
library(PKI)
library(RCurl)
library(rsconnect)

shinyServer(function(input, output) {

  output$lineplot <- renderPlot({ 

    jcc = JDBC("","")
    conn = dbConnect(jcc,"//",user="",
                     password="")
    Result<-dbGetQuery(conn,statement = paste("select sale_dt,count(trd) as Bill from retail_str_sales_master where year(sale_date)=",input$var," group by sale_date"))
    Result$SALE_DATE<-as.Date(Result$SALE_Dt)
    #Result1<-dbGetQuery(conn,statement = paste("select sale_dt,count(trd) as Bill from retail_str_sales_master where to_char(to_date(month(sale_date),'MM'),'Month')=",input$var,"  group by sale_date"))

  draw the line Graph with the specified number of bins using Sale date and Total no Of Bills
    ggplot(Result,aes(x=SALE_DATE,y=BILL))+geom_line()+geom_point()+scale_x_date(date_breaks ="2 day", date_labels=("%d-%m-%y"))+
      scale_y_continuous(breaks =seq (0,max(Result$BILL),200))+ylab("Total No Of Bills")+xlab("sale date")+theme_bw()+
      theme(axis.text.x=element_text(angle=90))+theme(axis.text.x = element_text(vjust = 0.5,hjust = 0.5))


  })

})

2 个答案:

答案 0 :(得分:0)

您可以使用renderUI()。以下是一个示例:http://shiny.rstudio.com/gallery/dynamic-ui.html

答案 1 :(得分:0)

修复缩进错误并检查问题是否仍然存在。

 shinyUI(fluidPage(
  titlePanel("Sales Analysis"),
  sidebarLayout(
    sidebarPanel(
      helpText("Create line graph for  
                       Sale."),
      selectInput(multiple='TRUE' ,"var", 
                  label = "Choose a year to display",
                  choices = c(Result),
                  selected = Result[1]),
      selectInput( "var2", 
                   label1 = "Choose a month to display",
                   choices1 = c(Result2),
                   selected1 = Result2[1])
    ),
    mainPanel(
      plotOutput("lineplot"),
      print(Result),
      print(Result1)
    )
  )
))