闪亮的模块:在创建模块UI时已经存储了参数(附加参数),而不是将其传递给模块的服务器功能? [英] shiny modules: Store parameters (additional argument) already when creating module-UI instead of passing it to module's server function?

查看:67
本文介绍了闪亮的模块:在创建模块UI时已经存储了参数(附加参数),而不是将其传递给模块的服务器功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个模块sliderCheckbox,该模块将sliderInputcheckBoxInput捆绑在一起以禁用slideInput-基本上可以说出我不知道",这对于类似调查的输入是必需的.当禁用滑块时,我希望它返回默认值-通常是初始值,但不一定.

I have created a module sliderCheckbox which bundles together a sliderInput and a checkBoxInput to disable the sliderInput - basically a possibility to state "I don't know", which is necessary for survey-like inputs. When the slider is disabled, I want it to return a default value - most often the initial value, but not necessarily.

现在我的问题是:是否有可能在初始化UI时通过sliderCheckboxInput()传递此默认值?由于默认值是最小值和最大值之类的属性,因此它在逻辑上所属的位置,并且也更适合我的其余设置.

Now my question is: Is there any possibility to pass this default value when initialising the UI, that is with sliderCheckboxInput()? As the default value is a property like minimum and maximum, that is where it logically belongs to, and it also fits better to the rest of my setup.

示例:

library(shiny)
library(shinyjs)

sliderCheckboxInput <- function(id,description="",
                                min = 0,
                                max = 100,
                                value = 30,
                                default= NULL ##HERE I would want the default value to be set
                                cb_title = "I don't know"){
  ns <- NS(id)

  fluidRow(
    column(width=9,
           sliderInput(ns("sl"),
                       paste0(description, collapse=""),
                       min = min,
                       max = max,
                       value = value)
    ),
    column(width=2,
           checkboxInput(ns("active"),
                         cb_title, value=FALSE )
    )
  )
}

sliderCheckbox<- function(input, output, session,
                          default=NA) { #Problem: set default when initialising module

  oldvalue<- reactiveVal()

  observeEvent(input$active, {
    if (input$active){
      oldvalue(input$sl)
      disable("sl")
      updateSliderInput(session, "sl", value=default)
    }else {
      updateSliderInput(session, "sl", value=oldvalue())
      enable("sl")
    }

    toggleState("sl", !input$active)
  })

  onclick("sl",
          if(input$active) updateCheckboxInput(session, "active", value=FALSE)
  )

  return ( reactive({
    if (input$active){
      default
    }else {
      input$sl
    }
  }))

}


ui <- fluidPage(

  useShinyjs(),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderCheckboxInput("bins", "Number of bins:",
                          min = 1,
                          max = 50,
                          value = 30)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

server <- function(input, output, session) {
  bins_nr <- callModule(sliderCheckbox, "bins", default=44)

  output$distPlot <- renderPlot({

    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = bins_nr() + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')

  })

}

shinyApp(ui, server)

推荐答案

您可以使用隐藏的textInput

library(shiny)
library(shinyjs)

sendValueToServer <- function(id, value) {
  hidden(textInput(
    id, "If you can see this, you forgot useShinyjs()", value
  ))
}

myModuleUI <- function(id, param) {
  ns <- NS(id)
  tagList(
    sendValueToServer(ns("param_id"), param),
    textOutput(ns("text_out"))
  )
}

myModule <- function(input, output, session) {
  param <- isolate(input$param_id)

  output$text_out <- renderText({
    param
  })
}

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    myModuleUI("id", "test")
  ),
  server = function(input, output, session) {
    callModule(myModule, "id")
  }
)

使用Shiny的JavaScript API可能有更直接的方法,但这是一个纯R"解决方案,对于大多数用例来说应该足够了.请注意,您可以在初始化时使用输入值

There are probably more direct ways to do this using the JavaScript API of shiny but this is a "pure R" solution which should be enough for most usecases. Note that you can use the input value at initialization time with

isolate(input$text_in)

因为ui总是在服务器之前构建的.如果将所有内容都包裹在renderUI中,事情会变得更加复杂,但这对您而言似乎并非如此.

because the ui is always built before the server. Things get more complicated if everything is wrapped into renderUI but this does not seem to be the case for you.

这篇关于闪亮的模块:在创建模块UI时已经存储了参数(附加参数),而不是将其传递给模块的服务器功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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