[英]Issue changing Shiny slider to represent categorical/string features with custom javascript
I'm trying to create a Shiny slider that represents months of the year. 我正在尝试创建一个代表一年中几个月的“闪亮”滑块。 Ideally, I'd like the months to be displayed as strings/characters rather than integers (where 1 = January, 2 = February, etc.).
理想情况下,我希望月份显示为字符串/字符,而不是整数(其中1 =一月,2 =二月,等等)。
I found this question , which led me to this answer that allows characters to be displayed on a Shiny slider. 我发现了这个问题 ,这使我想到了这个答案 , 该答案允许在“发光”滑块上显示字符。 It inserts JS code into R.
它将JS代码插入R。
When I try to change the above answer to fit my example, I can get the month names to display correctly, but I think there's an issue with the loop in the JS code. 当我尝试更改上述答案以适合我的示例时,我可以使月份名称正确显示,但是我认为JS代码中的循环存在问题。 Instead of January corresponding to a value of 1, it corresponds to a value of 0. I believe that this is a result of JS indexing from 0, whereas R indexes from 1. But I'm only very novice at JS, so I'm not sure how to correct this issue so that my slider correctly displays 1s as January, etc. As a note, again, I'm a JS novice, so the code I've provided here was altered to something that simply worked and probably has a lot of room for improvement!
而不是对应于值1的一月,它对应于值0。我认为这是JS从0开始索引的结果,而R从1开始索引的结果。但是我只是JS的新手,所以我我不确定如何解决此问题,以便我的滑块将1正确显示为1月,等等。再次说明一下,我还是JS新手,因此我在此处提供的代码已更改为可以正常工作的代码,并且可能有很大的改进空间! I was just working off of the example given.
我只是根据给出的示例进行工作。
library(shiny)
JScode <-
"$(function() {
setTimeout(function(){
var vals = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
for (i = 1; i >= vals.length; i++) {
var val = (1,12);
vals.push(val);
}
$('#month').data('ionRangeSlider').update({'values':vals})
}, 12)})"
runApp(shinyApp(
ui = fluidPage(
tags$head(tags$script(HTML(JScode))),
sliderInput("month",
"Month:",
min = 1,
max = 12,
value = 1,
step = 1,
width = "100%")
),
server = function(input, output, session) {
observeEvent(input$month, {
print(input$month)
})
}
))
The code above results in this: 上面的代码导致:
I have my slider set at a value of 1, so the automatic display should be on January. 我将滑块设置为1,因此自动显示应该在1月。 Instead, it's on February.
相反,它是在二月。
Any remedies? 有什么补救办法吗? I believe the issue lies in the JS loop, but I've tinkered with it all day and can't find a solution.
我认为问题出在JS循环中,但是我整天都在修补它,找不到解决方案。
This can be easily done using sliderTextInput function in shiny. 使用闪亮的sliderTextInput函数可以轻松完成此操作。 No need to add all this complex js function.
无需添加所有这些复杂的js函数。 Just a few lines of code will do the trick.Install the shinywidgets package which contains the sliderTextInput function.
只需执行几行代码即可 。安装包含sliderTextInput函数的shinywidgets软件包。 Do the following :
请执行下列操作 :
sliderTextInput("Month","Select Month" ,
choices = c("January", "February", "March", "April" #Add more months),
selected = c("January", "February", "March", "April"), #if you want any default values
animate = FALSE, grid = FALSE,
hide_min_max = FALSE, from_fixed = FALSE,
to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
post = NULL, dragRange = TRUE)
I think this depends on what you want to do next with your code but to just get the visual to show January as the first month: 我认为这取决于您接下来要对代码执行的操作,而只是获得将月份显示为第一个月的视觉效果:
Set the min to 0, max to 11, and value to 0 in the R code 在R代码中将min设置为0,max设置为11,并将value设置为0
sliderInput("month",
"Month:",
min = 0,
max = 11,
value = 0,
step = 1,
width = "100%")
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.