简体   繁体   English

R Shiny如何访问服务器代码中的滑块最小值和最大值

[英]R Shiny How to access slider min & max values in server code

Is there a way to reference the minimum & maximum values of a slider within the server.R portion of a Shiny app? 有没有办法在Shiny应用程序的server.R部分中引用滑块的最小值和最大值?

For instance, given the following definition within ui.R : 例如,在ui.R给出以下定义:

sliderInput("slider1", "", min = 0, max = 100, value = 50)

how do I determine that the preset minimum is 0, and the maximum is 100? 如何确定预设的最小值为0,最大值为100?

Thank you. 谢谢。

It depends on what you want to do with the min and max values when you access them. 这取决于您在访问它们时对最小值和最大值的要求。 I assume that you don't want to just read them, since you would already know their fixed values. 我假设您不想只读它们,因为您已经知道它们的固定值。 If you want to be able to manipulate them, you could try defining the slider in server.R using renderUI . 如果您希望能够操作它们,可以尝试使用renderUIserver.R定义滑块。 Then you could set the min and max parameters to variables, which could be manipulated elsewhere. 然后,您可以将minmax参数设置为变量,这些变量可以在其他地方进行操作。 Below is an example of that. 以下是一个例子。

ui.R ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Access the min and max of a slider"),
  sidebarLayout(
    sidebarPanel(uiOutput("SliderWidget")),
    mainPanel()
  )
))

server.R server.R

library(shiny)
shinyServer(function(input, output) {
  SlideMax = 100
  SlideMin = 0
  output$SliderWidget <- renderUI({
    sliderInput("Slider1","",min = SlideMin,max = SlideMax,value = 50)
  })
})

Of course, your question actually asks how to determine the preset values. 当然,您的问题实际上是询问如何确定预设值。 I'm not sure I understand that. 我不确定我明白这一点。 I think that the preset values would be static, so there would be no need to determine them. 我认为预设值是静态的,因此不需要确定它们。

I've now found that one can have a third file as part of a shiny app, called global.R . 我现在发现可以将第三个文件作为闪亮应用程序的一部分,名为global.R

So, for the use case above I could define the min and max parameters as constants in the global.R file or dynamically create the slider as per Paul's suggestion. 因此,对于上面的用例,我可以将global.R和max参数定义为global.R文件中的常量,或者根据Paul的建议动态创建滑块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM