将sliderValues应用于输入的文件

时间:2017-04-20 14:39:03

标签: r shiny

我正在尝试将我的应用中的滑块值连接到表格,从上传的文件中呈现,但我无法理解如何正确地执行此操作。以下是代码现在的样子:

server.r

library(shiny)

fluidPage(

  titlePanel("Cars plot"),

  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                'text/comma-separated-values,text/plain',
                '.csv')),
      helpText("Note: svc file should be separated by comas, and contain ",
               "single quotes only."),
      checkboxInput('header', 'Header', TRUE),
      tags$hr(),
        sliderInput("cost", "Max cost:",
                    min=0, max=1000000, value=500000, pre = "$"),
        sliderInput("range", "Range of MaxSpeed:",
                    min = 50, max = 500, value = c(100,450))
        ),

    mainPanel(
      tableOutput('contents'),
      tableOutput('values')
    )
  )
)

ui.r

Car, Horsepower, MaxSpeed, Cost
AlfaRomeo, 200, 300, 200000
AstonMartin, 400, 310, 300000

最后,我用来测试它的文件如下所示:

import java.util.*;

interface PaymentValidatorInterface {
    public boolean validate(PaymentValidationContext context);
}

class PaymentValidationContext {
    String result = "";
    String user;
    int cardData;
    String merchant;

    public PaymentValidationContext(String user, int cardData) {
        this.user = user;
        this.cardData = cardData;
    }
}

class PaymentValidator {
    public static boolean validateUser(PaymentValidationContext context) {
        if (context.user == null) {
            context.result += "User is wrong\n";
            return false;
        }
        return true;
    }

    public static boolean validateMerchant(PaymentValidationContext context) {
        context.merchant = context.user + "#" + context.cardData;
        if (context.merchant.length() <= 3) {
            context.result += "Marchant is wrong\n";
            return false;
        }
        return true;
    }

    public static boolean finishValidation(PaymentValidationContext context) {
        context.result += "Everything is fine.\n";
        return true;
    }
}

public class Processor {
    private final static Queue<PaymentValidatorInterface> validators = new LinkedList<>();
    static {
        validators.add(PaymentValidator::validateUser);
        validators.add(PaymentValidator::validateMerchant);
        validators.add(PaymentValidator::finishValidation);
    }

    public String processPayment(String user, int cardData) {
        PaymentValidationContext context = new PaymentValidationContext(user, cardData);
        validators.stream().anyMatch(validator -> !validator.validate(context));
        return context.result;
    }

    // For testing -------
    public static void main(String[] args) {
        Processor p = new Processor();
        System.out.print(p.processPayment("Foobar", 1337));    // ok
        System.out.print(p.processPayment(null, 1337));        // fails
        System.out.print(p.processPayment("", 1));             // fails
    }
}

1 个答案:

答案 0 :(得分:1)

几乎完成了。只需为dataframesubset分配input值:

mytable<-read.csv(inputFile$datapath, header=input$header, sep=',',
         quote="'")

subset(mytable,mytable$Cost<input$cost & mytable$MaxSpeed>=input$range[1] & mytable$MaxSpeed<=input$range[2] )