2016-06-16 27 views
0

我試圖設置一個閃亮的滑塊,從0.01到100的日誌基準10比例。我一直在using this Q/A嘗試解決我的問題。然而,我似乎無法得到圖上的水平線根據滑塊閾值作出反應。R閃亮 - 從0.01到100的值使用日誌比例滑塊

我錯過了什麼嗎?

該圖顯示了我的滑塊和繪圖上的線條;底部或頂部閾值都不符合水平線位置。如果我恢復到線性比例,則線條與滑塊位置匹配。

我可重複碼

# 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

無法重現你的榜樣。您可以在擦除'scale_y_log10()'命令時嘗試記錄數據。 – Jimbou

+0

@Jimbou:增加了repr。碼。 – val

+0

你可以試試嗎? 'geom_hline(aes(yintercept = c(0,10^seq(-2,2))[input $ slider1 [1] +1]))' – Jimbou

回答

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") 
})