对数刻度上的闪亮滑块 [英] Shiny slider on logarithmic scale

查看:22
本文介绍了对数刻度上的闪亮滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Shiny 构建一个简单的 Web 应用程序,其中包含一个 滑块,它可以控制 p 值应该是什么显示在输出中.

I'm using Shiny to build a simple web application with a slider that controls what p-values should be displayed in the output.

如何使滑块作用于对数而不是线性比例?

How can I make the slider act on a logarithmic, rather than linear, scale?

目前我有:

sliderInput("pvalue",
            "PValue:",
            min = 0,
            max = 1e-2,
            value = c(0, 1e-2)
),

谢谢!

推荐答案

我不确定你想要什么作为输出,但我所做的是让可能的 p 值为 [0, 0.00001, 0.0001, 0.001, 0.01].如果您想要一些不同的东西,希望这个答案是一个足够好的起点.

I wasn't sure exactly what you wanted as the output, but what I did was have the possible p-values be [0, 0.00001, 0.0001, 0.001, 0.01]. If you want something a little different, hopefully this answer is a good enough starting point.

基本上,我首先创建了一个数组来保存数字的值(0、0.0000.1、...),然后我只使用滑块 API 中的特殊更新函数来使这些值粘在滑块上.一旦您弄清楚如何使用滑块的 API,这将非常简单.此外,由于某种原因,在 RStudio 中运行它看起来很奇怪,但在浏览器中它很好.另外,请注意我有 5 毫秒的延迟,因为我想确保它在滑块初始化后运行.不是最干净的解决方案,可能有更好的方法来做到这一点,但它确实有效.

Basically, first I created an array that holds the values of the numbers (0, 0.0000.1, ...), and then I just use the special update function from the slider's API to make these values stick to the slider. It's pretty simple once you figure out how to use the slider's API. Also, for some reason running this inside RStudio looks weird, but in a browser it's fine. Also, note that I have a 5ms delay because I want to make sure this runs after the slider gets initialized. Not the cleanest solution, there's probably a better way to do that, but it works.

library(shiny)

JScode <-
  "$(function() {
    setTimeout(function(){
      var vals = [0];
      var powStart = 5;
      var powStop = 2;
      for (i = powStart; i >= powStop; i--) {
        var val = Math.pow(10, -i);
        val = parseFloat(val.toFixed(8));
        vals.push(val);
      }
      $('#pvalue').data('ionRangeSlider').update({'values':vals})
    }, 5)})"

runApp(shinyApp(
  ui = fluidPage(
    tags$head(tags$script(HTML(JScode))),

    sliderInput("pvalue",
                "PValue:",
                min = 0,
                max = 1e-2,
                value = c(0, 1e-2)
    )
  ),
  server = function(input, output, session) {
  }
))

这篇关于对数刻度上的闪亮滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆