基于服务器中的条件逻辑渲染闪亮的用户输入

时间:2014-08-28 19:31:13

标签: r shiny

我试图设置一个闪亮的导航栏面板页面,用户控制我根据一组单选按钮中的初始选择显示更改。我直接在ui中渲染单选按钮,然后在"观察"内部构建条件控件。 Server.r中的逻辑控制结构。弹出错误是因为我的初始if语句的计算结果为false,因此过滤我的数据所需的控件不会被渲染,因此dplyr :: filter()没有得到预期的结果。我想弄清楚的是,如果我运行它,单选按钮会渲染所有其他孔板。然后,如果我点击几个单选按钮,最终会出现其他ui控件,然后再点击" Regions"按钮图表被调用。我怎样才能得到第一个if语句来查看" Regions"被选中了?或者,一旦观察者开始,有没有办法让单选按钮更新?无论如何,任何见解都会受到高度赞赏。

错误:

 Listening on http://...............
  Error in eval(substitute(expr), envir, enclos) : 
  incorrect length (0), expecting: 192 

Global.R:

##### Play data
    options(stringsAsFactors = FALSE)

    exp <- data.frame(scenario=rep(c(1,2,3,4),each=48),
                region=rep(c("Western","Central","Southern","Northern","Capital","Hudson"),
                times=4,each=8),
                period=rep(c(1,2,3,4),times=48),
                price=rep(c("Hi","Lo"),times=24,each=4),
                val=rnorm(192,5,1.5))

##### Functions

    all_values <- function(x) {
    if(is.null(x)) return(NULL)
    unique(x[,1])
    }

Server.R



library(shiny)
library(ggvis)
library(plyr)
library(dplyr)

shinyServer(function(input, output, session) {

observe({

if(input$comp=="Regions") { # For comparing regions #######################################

###### Define Controls ####### 

output$regionO <- renderUI({ checkboxGroupInput(inputId="regionI", label = h4("Regions"), 
        choices = list("Western", "Central", "Southern","Northern", "Capital", "Hudson"),
        selected = "Capital")
}) 

output$scenarioO <- renderUI({ selectInput(inputId="scenarioI", label = h4("Scenario"), 
        choices = list("L1" = 1, "L2" = 2, "L3" = 3,"L4" = 4), 
        selected = 1)
})

output$fuelO < renderUI({ selectInput(inputId="fuelI", label = h4("Fuel Price"), 
        choices = list("High Price" = "Hi", "Low Price" = "Lo"), 
        selected = "Lo")
})

###### Define Data ########### 


choose <- reactive({ 

        chooseD <- exp %>% filter(scenario == input$scenarioI, region %in% input$regionI, price == input$fuelI)
        names(chooseD)<-c("scenario","NYregion","period","price","val")
        return(chooseD)
})        


sPlot1 <- reactive({

        choose %>% ggvis(~factor(period),~val,stroke=~NYregion) %>% 
        layer_lines(strokeWidth:=2.5,strokeWidth.hover:=5) %>% 
        add_axis("x",title="Analysis Period") %>% add_tooltip(all_values,"hover") 
}) 

sPlot1 %>% bind_shiny("plot1") 
sPlot1 %>% bind_shiny("plot2")  


} 

}) 

# The code below is commented out so I can test the first condition of input$comp

#else if(data==2) { # For comparing scenarios ######

###### Define Controls ####### 

###### Define Data ########### 

#} else { # For comparing fuel prices ##############

###### Define Controls ####### 

###### Define Data ########### 

#}

}) # End Shiny Server

Ui.R:


library(shiny)
library(ggvis)
library(plyr)
library(dplyr)

shinyUI(navbarPage("TBR Economic Results Viewer", # theme="bootstrapCR.css",

  tabPanel("Summary Results",
   fluidRow(
      column(2,
       wellPanel(
        radioButtons(inputId="comp", label = h4("Comparison"),
        choices = list("Regions", "Scenarios", "Prices"), 
        selected ="Regions")
        ),

       wellPanel(
        uiOutput("regionO"),
        br(),
        uiOutput("scenarioO"),
        br(),
        uiOutput("fuelO")
        )
      ),

     column(5, 
      wellPanel(
        ggvisOutput("plot1"),
        textOutput("test")
        )
      ),
     column(5,
      wellPanel(
        ggvisOutput("plot2")
        ) 
      )
    )
 ),

 tabPanel("Detailed Results",
      mainPanel(
        plotOutput("plot2")
      )
    )
 )
)

1 个答案:

答案 0 :(得分:1)

你有一个错字。除此之外,您可以选择input$fuelI

library(shiny)
library(ggvis)
library(plyr)
library(dplyr)
options(stringsAsFactors = FALSE)

exp <- data.frame(scenario=rep(c(1,2,3,4),each=48),
                  region=rep(c("Western","Central","Southern","Northern","Capital","Hudson"),
                             times=4,each=8),
                  period=rep(c(1,2,3,4),times=48),
                  price=rep(c("Hi","Lo"),times=24,each=4),
                  val=rnorm(192,5,1.5))
all_values <- function(x) {
  if(is.null(x)) return(NULL)
  unique(x[,1])
}

runAPP:

runApp(list(ui = navbarPage("TBR Economic Results Viewer", # theme="bootstrapCR.css",
                            tabPanel("Summary Results",
                                     fluidRow(
                                       column(2, wellPanel(
                                         radioButtons(inputId="comp", label = h4("Comparison"),
                                                      choices = list("Regions", "Scenarios", "Prices"), 
                                                      selected ="Regions")
                                       ),
                                       wellPanel(uiOutput("regionO"), br(),
                                                 uiOutput("scenarioO"), br(),
                                                 uiOutput("fuelO")
                                       )
                                       ),

                                       column(5, wellPanel(ggvisOutput("plot1"),textOutput("test"))
                                       ),
                                       column(5, wellPanel(ggvisOutput("plot2")) 
                                       )
                                     )
                            ),
                            tabPanel("Detailed Results", mainPanel(plotOutput("plot2"))
                            )
)
, server = function(input, output, session) {
  observe({
    if(input$comp=="Regions") { 
      output$regionO <- renderUI({ checkboxGroupInput(inputId="regionI" , label = h4("Regions"), 
                                                      choices = list("Western", "Central", "Southern","Northern", "Capital", "Hudson"),
                                                      selected = "Capital")
      })
      output$scenarioO <- renderUI({ selectInput(inputId="scenarioI", label = h4("Scenario"), 
                                                 choices = list("L1" = 1, "L2" = 2, "L3" = 3,"L4" = 4), 
                                                 selected = 1)
      })
      output$fuelO <- renderUI({ selectInput(inputId="fuelI", label = h4("Fuel Price"), 
                                            choices = list("High Price" = "Hi", "Low Price" = "Lo"), 
                                            selected = "Lo")
      })
      if(!is.null(input$fuelI)){
        choose <- reactive({ 
          chooseD <- exp %>% filter(scenario == input$scenarioI, region %in% input$regionI, price == input$fuelI)
          names(chooseD)<-c("scenario","NYregion","period","price","val")
          return(chooseD)
        })        
        sPlot1 <- reactive({
          choose %>% ggvis(~factor(period),~val,stroke=~NYregion) %>% 
            layer_lines(strokeWidth:=2.5,strokeWidth.hover:=5) %>% 
            add_axis("x",title="Analysis Period") %>% add_tooltip(all_values,"hover") 
        }) 

        sPlot1 %>% bind_shiny("plot1")
        sPlot1 %>% bind_shiny("plot2")  
      }
    } 
  }) 
}
))

enter image description here