Shiny Slider on Logarithmic Scale

Shiny slider on logarithmic scale

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.

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

Creating a categorical sliderInput within a rendered UI in R shiny

Moving the line where you include the JavaScript to the renderUI (and wrapping it in a div together with the input element so both are returned to the UI) seems to do the trick, working example below. I am no JavaScript expert, but I assume the reason for this is is that in your case, the DOM element does not exist yet and thus the JavaScript code is not attached - but someone please correct me if I am wrong in this assumption.

I added two pieces of code below, one with a categorical slider with interactive labels as suggested by @agenis in the comments, and another with your code slightly adjusted to make your example work.

Hope this helps!


1. Interactive categorical slider

Sample Image

library(shiny)

runApp(shinyApp(
ui = fluidPage(
numericInput('nlabs','number of labels:', min=3,max=10,value=3),
checkboxInput('rev','Reverse?',value=FALSE),
textOutput('texty'),
uiOutput('uu')
),
server = function(input, output, session) {

output$texty <- renderText({
input$pvalue
})

output$uu <- renderUI({

# Create labels
my_labs = sort(LETTERS[1:input$nlabs],decreasing = input$rev)
my_labs = paste(sapply(my_labs,function(x){paste0("'",x,"'")}),collapse=",")
# Create the JS code
JScode <-paste0(
"$(function() {
setTimeout(function(){
var names = [",
my_labs,
"];
var vals = [];
for (i = 0; i < names.length; i++) {
var val = names[i];
vals.push(val);
}
$('#pvalue').data('ionRangeSlider').update({'values':vals})
}, 7)})")

# Return the div with the JS Code and the sliderInput.
div(
tags$head(tags$script(HTML(JScode))),
sliderInput("pvalue",
"PValue:",
min = 0,
max = 7,
value = 0
)
)
})
}
))

2. Working version of your code

library(shiny)
JScode <-
"$(function() {
setTimeout(function(){
var names = ['Unrated', 'Emerging', ' ', 'Formative', ' ', ' ', 'Developed', ' '];
var vals = [];
for (i = 0; i < names.length; i++) {
var val = names[i];
vals.push(val);
}
$('#pvalue').data('ionRangeSlider').update({'values':vals})
}, 7)})"

runApp(shinyApp(
ui = fluidPage(
textOutput('texty'),
uiOutput('uu')
),
server = function(input, output, session) {

output$texty <- renderText({
input$pvalue
})
output$uu <- renderUI({
div(
tags$head(tags$script(HTML(JScode))),
sliderInput("pvalue",
"PValue:",
min = 0,
max = 7,
value = 0
)
)
})
}
))

shiny R slider with few values

Yes you can use sliderTextInput from shinyWidgets package.
You provide the different values for choices and as you get the value in input as text you must cast them to numbers before use.

UI

...
sliderTextInput("bins1", "Number of bins:", choices=c("3", "5", "9", "25", "36"))
...

SERVER

...
bins <- seq(min(x), max(x), length.out = as.integer(input$bins1))
... nclass = as.integer(input$bins1) ...
...

How can I display all values on a slider in Shiny? Right now it only shows every other year but I'd like it to show all

FYI this was answered before here: https://stackoverflow.com/a/50222952/4954719

rng <- 1965:1980
sliderTextInput("map.year", "Year",
choices = rng,
selected = rng[1],
grid = T,
width = "100%")

I changed your selected value as it is not in your specified range.



Related Topics



Leave a reply



Submit