RShiny的动态输入选择

时间:2018-07-15 14:10:31

标签: r shiny

我有以下代码:

library(shiny)

vec <- seq(1,10)
ui <- fluidPage(

  titlePanel(""),

  sidebarLayout(
    sidebarPanel(
      fluidRow(
      selectInput("city", "Select number", vec, multiple = TRUE),
      actionButton("First_fives", "First Fives" ),
      actionButton("Last_fives", "Last Fives"),
      actionButton("ok", "OK"))
    ),

    mainPanel(
      fluidRow(
        h5("Selected numbers:"))
    )
  )
)

server <- function(input, output) {


}

shinyApp(ui = ui, server = server)

我希望能够:

  • 当我单击“前五个”时,清空选择输入框并将数字1到5添加到我的选择输入框
  • 当我单击“最后五个”时,清空选择输入框,然后将数字6到10添加到我的选择输入框
  • 当我单击“确定”时,我希望所选项目出现在“所选编号:”下

1 个答案:

答案 0 :(得分:1)

 public class RestAdapter {


    Context context;
    public static final String BASE_URL = "https://45.55.43.15:9090/api/";


    private OkHttpClient okHttpClient;
    private Authenticator authenticator = new Authenticator() {
        @Override
        public Request authenticate(Route route, Response response) {
            return null;
        }
    };


    private RestAdapter() {
    }

    public void setAuthenticator(Authenticator authenticator) {
        this.authenticator = authenticator;
    }


    public static class Builder {
        String email, password;
        RestAdapter apiManager = new RestAdapter();

        public Builder setAuthenticator(Authenticator authenticator) {
            apiManager.setAuthenticator(authenticator);
            return this;
        }

        public RestAdapter build(String param_email, String param_password) {
            this.email = param_email;
            this.password = param_password;
            return apiManager.newInstance(email, password);
        }

    }

    public class RequestTokenInterceptor implements Interceptor {
        String email, password;
        String credentials, basic;
        public RequestTokenInterceptor(String email, String password) {
            this.email = email;
            this.password = password;
            credentials = email + ":" + password;
            basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
        }



        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder builder = original.newBuilder()
                    .addHeader("Authorization", basic)
                    .method(original.method(), original.body());
            return chain.proceed(builder.build());
        }
    }

    private RestAdapter newInstance(String email, String password) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.e("https", message);
            }
        });
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);


        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .addInterceptor(new RequestTokenInterceptor(email, password))
                .authenticator(authenticator)
                .build();


        return this;
    }


    public <T> T createRest(Class<T> t) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        return retrofit.create(t);
    }
 }