使用R/S创建动态UI,其中用户输入将参数从常量切换到范围

使用R/S创建动态UI,其中用户输入将参数从常量切换到范围,r,dynamic,shiny,R,Dynamic,Shiny,我对Shiny是个新手,我制作了一个应用程序,在这个应用程序中,输出最终将是一个点的绘图,使用一个函数在用户施加的x轴参数变化范围内进行计算。有20个可能的参数,用户可以从中选择1作为x,但这些参数的其余部分也被函数用作单个值。我希望使应用程序能够: 所有20个变量在应用程序中都可以作为单值滑块显示 用户可以通过从下拉列表中选择与滑块相关的参数名称,将这些单值滑块中的1个一次更改为范围滑块 一次只能有一个滑块可以是范围滑块,这意味着当选择不同的滑块来生成范围时,先前选择用于生成值范围的滑块应恢复

我对Shiny是个新手,我制作了一个应用程序,在这个应用程序中,输出最终将是一个点的绘图,使用一个函数在用户施加的x轴参数变化范围内进行计算。有20个可能的参数,用户可以从中选择1作为x,但这些参数的其余部分也被函数用作单个值。我希望使应用程序能够:

  • 所有20个变量在应用程序中都可以作为单值滑块显示
  • 用户可以通过从下拉列表中选择与滑块相关的参数名称,将这些单值滑块中的1个一次更改为范围滑块
  • 一次只能有一个滑块可以是范围滑块,这意味着当选择不同的滑块来生成范围时,先前选择用于生成值范围的滑块应恢复为某个单一默认值
  • 到目前为止,我只成功地生成了一个可以使用下拉列表操纵的滑块。在ui.R中,我有
    uiOutput(“ui”)
    ,而在server.R中:

    server <- shinyServer(function(input, output) {
    var <- reactive(switch(input$var_param,
                     "K_As1" = (value <- c(10,20)),
                     "K_Ap1" = (value <- c(10,15))
    ))
    
    output$ui <- renderUI({
        # Depending on input$var_param, we'll generate a different
        # slider component and reload it with a range.
        sliderInput("ui", label = input$var_param, 0, 50, var())
      })
    })
    

    server有不同的方法来创建具有光泽的动态UI。这里有一个可能解决你问题的方法。其主要思想是使用反应变量来存储所选变量的名称。只有
    selectInput
    中选定的变量将具有范围滑块,其余变量将具有具有默认值的单值滑块。与
    selectInput
    关联的观察者将更改所选变量的名称,并且将再次呈现
    rangeUI

    我希望它能帮助您构建动态用户界面

    library(shiny)
    
    ui <- fluidPage(
      fluidRow(
        column(6, uiOutput('controls')),
        column(6, plotOutput('plot1', height = 500))
      )
    )
    
    server <- shinyServer(function(input, output, session) {
      # define the data frame to use
      dat <- mtcars
      # name of available data frame
      varNames <-  names(dat)
      # define defaul values as the first value in each column
      defaultValues <- as.numeric(dat[1,])
      # store the selected variable in a reactive variable 
      rv <- reactiveValues(selected = varNames[1])
    
      # dynamically creates a set of sliders
      output$controls <- renderUI({
        div(
          fluidRow(
            column(3, 
              selectInput("select", "Variables", varNames, selected=varNames[1])
            ),
            column(9, uiOutput("rangeUI"))
          )
        )
      })
    
      output$rangeUI <- renderUI({
        lapply(1:length(varNames), function(k) {
          # get min and max value
          minmax <- c(round(min(dat[[varNames[k]]])), round(max(dat[[varNames[k]]])) )
          fluidRow(
            column(12,
              if (rv$selected == varNames[k]) {
                # a slider range will created only is the variable is selected
                sliderInput(paste0("slider_", varNames[k]), label = varNames[k], 
                  min = minmax[1], max = minmax[2], value = minmax)
              } else {
                # otherwise uses single value with a default value
                sliderInput(paste0("slider_", varNames[k]), label = varNames[k], 
                  min = minmax[1], max = minmax[2], value = defaultValues[k])
              }
            )
          )
        })
      })
    
      observeEvent(input$select, {
        # changes in this variable will make the UI "controls" to be rendered again
        rv$selected <- input$select
      })
    
      # plot dat
      output$plot1 = renderPlot({
        # get the correct id name for the current slider
        id <- paste0("slider_", rv$selected)
        cat("id", id, "\n")
        # get the value from the input
        val = input[[id]]
        # plot all points of the selected variable
        plot(dat[,rv$selected])
        # fill out the points that are greater or equal to the value
        points(dat[ dat[,rv$selected] >= val, rv$selected, drop = FALSE], pch = 19, cex = 2)
      })
    
    })
    
    shinyApp(ui, server)
    
    库(闪亮)
    
    谢谢你,Geovany;你的帖子给了我一些关于如何创建应用程序的想法。然而,我发现我仍然在努力使我的滑块对使用
    SelectInput
    创建的下拉列表中的输入作出反应。这在逻辑上应该与您在这里编写的操作按钮类似;你认为将我选择的输入等同于一个反应值,然后传递给
    observeEvent
    会有用吗?Jon,我更新了解决方案以满足你的要求。我希望它现在能为你工作。这个更新非常棒,真的帮助我了解了反应的逻辑。再次感谢!不客气。这是一篇关于闪亮上反应性的好文章: