闪亮:checkboxGroupInput的条件面板和条件列表

时间:2016-03-22 19:38:27

标签: r ggplot2 shiny facet-wrap

我想创建一个闪亮的应用程序,用于绘制美国总统初选的最新民意调查图表。用户应该能够选择一个党(民主党或众议员),候选人和初选(或Caucusus)所在的州。

我有三个问题:

  1. 根据所选方(Dem或Rep),用户应获得下一个checkboxGroupInput,其中只显示民主党或共和党候选人。我试着用conditionalPanel来解决这个问题。但是,我不能使用"候选人"两次作为Widget的名称(稍后在server.R我需要输入$ Candidate)。我该如何解决?

  2. 根据所选方(同样是民主党或众议员),用户应该获得所有州的列表,其中初选和公告到目前为止。目前,我正在列出我之前定义的所有美国州(因此,如果我想绘制状态的结果,那么我就会得到错误,没有可用的民意调查)。有没有办法从数据集中获取状态列表,这是在server.R部分生成的(它在那里被称为轮询$状态,但是我不能使用它,因为ui.R现在不是"轮询"。)

  3. 我用ggplot和facet_wrap函数(有两列)将结果绘制成条形图。我选择的州越多,情节越小。有没有办法设置图的高度并在主面板中插入垂直滚动条?

  4. 以下是用户界面的代码:

    <?php
    
    //register form v1.0
    
    error_reporting(0);
    
    //declares register form
    
    
    $formFields = array('reg-username' => 'اسم المستخدم', 
        'reg-email' => 'البريد الإلكتروني', 
        'reg-password' => 'كلمة المرور', 
        'reg-confirmPassword' => 'تأكيد كلمة المرور');
    
    
    
    
    function checkBlank(){
    
    global $formFields;
    
    
    //now I want the browser to check each field if its empty
    
    foreach($formFields as $fieldName => $fieldRealName){
        if(empty($_POST[$fieldName])){
            echo '<ul class="ErrorMessage"><li>لم تدخل '. $fieldRealName .' * </li></ul>';
            echo '<style>.'. $fieldName .'{
                border-color: red;
            }
            .'. $fieldName .'::-webkit-input-placeholder {
       color: red;
    }
            .'. $fieldName .'-h{
                color: red;
            }
            #asetrik{
                display: none;
            }
            </style>';
    }
    }
    
    }
    
    
    
    //blank fields have been checked
    function checkPass(){
    
            $regPassword = $_POST['reg-password'];
            $regConfPassword = $_POST['reg-confirmPassword'];
    
    
    
        if($regPassword !== $regConfPassword){
            echo '<ul class="ErrorMessage"><li>كلمات المرور غير متطابقة *</li></ul>';
        } //if the fields are not empty i want it to check if the passwords match
        }   
    
    
        function checkEmail(){
    
    
    
            $regEmail = $_POST['reg-email'];
    
            if (!filter_var($regEmail, FILTER_VALIDATE_EMAIL)) {
    
                    echo '<ul class="ErrorMessage"><li>البريد الإلكتروني المدخل غير صحيح *</li></ul>';
        }
    
        function checkName(){
            $regUsername = $_POST['reg-username'];
    if ( !preg_match('/^[A-Za-z][A-Za-z0-9]{5,31}$/', $regUsername)){
      echo '<ul class="ErrorMessage"><li>اسم المستخدم يجب أن يبدأ بحرف *</li></ul>'; 
    }
        }
    
    
    
    function checkExist(){
    
            $regUsername = $_POST['reg-username'];
            $regEmail = $_POST['reg-email'];
            $connectToDB  = mysql_connect('localhost', 'root', '') or die(mysql_error());
            $selectDB = mysql_select_db('supermazad') or die(mysql_error());
            $checkIfExist = mysql_query("SELECT * FROM users WHERE username LIKE '".$regUsername."' OR email LIKE '".$regEmail."' ");
    
    
            if(mysql_num_rows($checkIfExist) > 0){
            echo '<ul class="ErrorMessage"><li>اسم المستخدم/ البريد الإلكتروني موجود *</li></ul>';
    
    }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    ?>
    

    这里是server.R的代码:

    shinyUI(fluidPage(
        titlePanel("2016 Presidential primaries"),
    
        sidebarLayout(position = "right",
                sidebarPanel(
                        helpText("Choose between Democratic (Dem) and Republican (Rep)
                                 Primaries and Caucuses:"),
    
                        selectInput("party", 
                                    label = "Dem or Rep?",
                                    choices = c("Dem", "Rep",
                                    selected = "Dem")),
    
                        conditionalPanel(
                                condition = "input.party == 'Dem'",
                                checkboxGroupInput("Candidate", label = h4("Democratic Candidates"), 
                                                   choices = list("Clinton" = "Clinton", "Sanders" = "Sanders"),
                                                   selected = NULL)),
    
                        conditionalPanel(
                                condition = "input.party == 'Rep'",
                                checkboxGroupInput("Candidate", label = h4("Republican Candidates"), 
                                                   choices = list("Bush" = "Bush", "Carson" = "Carson", "Christie" = "Christie",
                                                                  "Cruz" = "Cruz", "Kasich" = "Kasich", "Rubio" = "Rubio",
                                                                  "Trump" = "Trump"),
                                                   selected = NULL)),
    
                        checkboxGroupInput("state",
                                label = "Select State",
                                choices = states,
                                inline = TRUE,
                                selected = NULL)
                ),
    
                mainPanel(
                        tabsetPanel(
                                tabPanel("Plot", plotOutput("plot")), 
                                tabPanel("Table", tableOutput("table"))
                        )
                )
    
    
        )
    ))
    

1 个答案:

答案 0 :(得分:0)

以下是问题1和2的解决方案:

ui.R中,将conditionalPanelcheckboxGroupInput替换为

  uiOutput('candidates'),
  uiOutput('states')

server.R中,在df <- reactive({....之前添加以下代码。请注意,您需要将部分input$Candidate代码更改为小写。

  observeEvent(input$party, {
    output$candidates <- renderUI({
      checkboxGroupInput(
        "candidate",
        ifelse(input$party == 'Dem', "Democratic Candidates", "Republican Candidates"),
        as.vector(unique(filter(polls,party==input$party)$choice))
      )
    })
  })

  observeEvent(input$candidate, {
    output$states <- renderUI({
      states_list <- as.vector(unique(filter(polls, party==input$party & choice==input$candidate)$state))
      checkboxGroupInput(
        "state",
        "Select state",
        # Excluding national surveys
        states_list[states_list!="US"]
      )
    })
  })

对于问题3,将df reactive更改为observe,然后根据所选的状态数设置绘图高度。同时更改此行p <- ggplot(df)

  observe({      
    df <- polls %>% filter(party %in% input$party) %>% filter(choice %in% input$candidate) %>% filter(state %in% input$state)
    height <- ceiling(length(input$state) / 2) * 200
    output$plot <- renderPlot({
      #Your plot code
    }, height=height)
  })