R Shiny - 使用对数刻度滑块获取0.01到100之间的值

时间:2016-06-16 07:59:50

标签: javascript r slider shiny

我正在尝试设置一个闪亮的滑块,其基数为10,从0.01到100。我一直using this Q/A试图解决我的问题。但是我似乎无法让绘图上的水平线根据滑块阈值做出反应。

我错过了什么吗?

图像显示我的滑块和绘图上绘制的线条;底部或顶部阈值都不匹配水平线位置。如果我恢复为线性比例,则线条与滑块位置匹配。

我可重现的代码

# Set libraries
library(shiny)
library(ggplot2)

# Global variables
from <- seq(1, 100, 1)
data <- (rexp(100, 0.05))
df1 <- as.data.frame(cbind(from, data))

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

ui <-
  shinyUI(fluidPage(tabsetPanel(tabPanel(
    "", " ",
    fluidRow(
      column(
        width = 6,
        tags$head(tags$script(HTML(JScode))),
        sliderInput(
          "slider1",
          "bounds",
          min = 0,
          max = 100,
          value = c(0, 100)
        )
      ),
      column(width = 6, " ",
             plotOutput("plot7"))
    )
  ))))

server <- function(input, output) {
  filedata <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
      return(df1) # this returns the default .Rds - see global variables
    }
    temp <- read.csv(infile$datapath)
    #return
    temp[order(temp[, 1]),]
  })
  output$plot7 <- renderPlot({
    ggplot(df1, aes(sample = data)) +
      stat_qq() +
      scale_y_log10() +
      geom_hline(aes(yintercept = input$slider1[1])) + # low bound
      geom_hline(aes(yintercept = input$slider1[2])) + # high bound
      labs(title = "Q-Q Plot", x = "Normal Theoretical Quantiles", y = "Normal Data Quantiles")
  })
}

shinyApp(ui = ui, server = server)

图片 enter image description here

1 个答案:

答案 0 :(得分:0)

我发布了一个答案,因为评论部分太有限了。 为了提取错误,我检查了JScode将输出哪些值。 值为seq(-2, 2, 1)。因此,您需要使用10^x计算“实际”值(例如1,10,100):

这里是完整的渲染图函数:

output$plot7 <- renderPlot({
  ggplot(df1, aes(sample = data)) +
   stat_qq() +
   scale_y_log10(limits = c(0.01,100)) + # set limits to plot reproducible images
   geom_hline(aes(yintercept = c(0,10^seq(-2,2))[input$slider1[1]+1])) + # low bound
   geom_hline(aes(yintercept = c(0,10^seq(-2,2))[input$slider1[2]+1])) + # high bound
   labs(title = "Q-Q Plot", x = "Normal Theoretical Quantiles", y = "Normal Data Quantiles")
})