如何为闪亮的两个相关输入值(滑块和数字)设置初始值? [英] How to set an initial value for two dependent input values (Slider and Numeric) in shiny?

查看:49
本文介绍了如何为闪亮的两个相关输入值(滑块和数字)设置初始值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我闪亮的应用程序中定义了两个相互关联或相互依赖的输入.现在,我的问题是为这些滑块和数字输入设置一个特定的初始值.似乎它们总是从最小值开始,即使我现在也不知道为什么.如何为这些输入参数指定唯一的起点或初始值?

我附上了我的应用程序的简化部分,以便在此处为您提供我的问题的可重现版本:

"ui.R"

图书馆(闪亮)闪亮的UI(流体页面(uiOutput("Param_s"),uiOutput("参数_n")))

server.R"

图书馆(闪亮)闪亮的服务器(功能(输入,输出){# 相互依赖的滑块和数字输入输出$Param_s = renderUI({滑块输入(inputId =param_slide",label="我的输入参数",值=输入$param_numeric,最小值=1,最大值 = 200)})输出$Param_n = renderUI({numericInput(inputId = "param_numeric",label="我的输入参数",值=输入$param_slide,最小值=1,最大值 = 200)})})

我尝试了各种方法来修复初始值,但最终没有任何效果.任何帮助,将不胜感激!!

解决方案

哇!我明白了伙计们!您应该只同时更新两个输入对象并更新到相同的值.这意味着添加这两行解决了我将初始值设置为 60 的问题,例如:

updateSliderInput(session,"param_slide", value = 60)updateNumericInput(session,"param_numeric", value = 60)

因此整个server.R"应该是这样的:

<预><代码>#图书馆(闪亮)闪亮的服务器(功能(输入,输出,会话){# 相互依赖的滑块和数字输入输出$Param_s = renderUI({滑块输入(inputId =param_slide",label="我的输入参数",值=输入$param_numeric,最小值=1,最大值 = 200)})输出$Param_n = renderUI({numericInput(inputId = "param_numeric",label="我的输入参数",值=输入$param_slide,最小值=1,最大值 = 200)})更新滑块输入(会话,param_slide",值= 60)updateNumericInput(session,"param_numeric", value = 60)})

您应该只知道使用

添加这些更新<块引用>

observeEvent()

当您在其他选项卡上有这些输入对象时.在我使用sidebarMenu"的情况下,我使用了一行简单的代码:

observeEvent(input$sidebar_id =="tab1",{更新滑块输入(会话,param_slide",值= 60)updateNumericInput(session,"param_numeric", value = 60)})

I have achieved to define two interconnected or mutually dependent input in my shiny app. Right now, my problem is to set a specific initial value for these slider and numeric inputs. It seems that they always start with the minimum value, even I don't now exactly why. How can I indicate a unique starting point or an initial value for these input parameters?

I have attached a simplified part of my app in order to provide you a reproducible version of my problem here:

"ui.R"

library(shiny)

shinyUI(fluidPage(

  uiOutput("Param_s"),
  uiOutput("Param_n")

))

and the "server.R"

library(shiny)

shinyServer(
function(input,output) {

# Mutually dependent  slider and numeric inputs 
output$Param_s = renderUI({
  sliderInput(inputId = "param_slide",
            label= "My input parameter",
            value= input$param_numeric,
            min=1,
            max=200)
 })

output$Param_n = renderUI({
  numericInput(inputId = "param_numeric",
             label= "My input parameter",
             value= input$param_slide,
             min=1,
             max=200)
})


})

I tried various things to fix the initial value but eventually nothing worked. Any help would be appreciated!!

解决方案

wow! I got it guys! You should only update the two input objects at the same time and up to the same value. It means adding these two lines solved my problem to set the initial value to 60 for example:

updateSliderInput(session,"param_slide", value = 60)
updateNumericInput(session,"param_numeric", value = 60 )

Therefore the whole "server.R" would be like this:

#
library(shiny)

shinyServer(
function(input,output,session) {

# Mutually dependent  slider and numeric inputs 
output$Param_s = renderUI({
sliderInput(inputId = "param_slide",
            label= "My input parameter",
            value= input$param_numeric,
            min=1,
            max=200)
})

output$Param_n = renderUI({
numericInput(inputId = "param_numeric",
             label= "My input parameter",
             value= input$param_slide,
             min=1,
             max=200)
})

updateSliderInput(session,"param_slide", value = 60)
updateNumericInput(session,"param_numeric", value = 60 )

})

You should only be aware of adding these updates with an

observeEvent()

when you have these input objects on the other tabs. In my case which I am using "sidebarMenu" I used a simple line of code as this:

observeEvent(input$sidebar_id =="tab1",{
  updateSliderInput(session,"param_slide", value = 60)
  updateNumericInput(session,"param_numeric", value = 60 )
})

这篇关于如何为闪亮的两个相关输入值(滑块和数字)设置初始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆