使用查询字符串变量来禁用页面加载时闪亮的小部件?

时间:2017-12-02 10:19:26

标签: shiny r-markdown

以下是我想在runtime: shiny

  • 阅读查询字符串
  • 从查询字符串
  • 中的变量获取textInput的默认参数
  • 如果查询字符串中的变量指示
  • ,则禁用textInput

问题似乎是读取查询字符串必须在被动上下文中完成,但构建接口发生在被动上下文之外。这是我的第一次尝试,作为Rmarkdown文件:

---
title: "Test session variables"
output: 
  flexdashboard::flex_dashboard:
    orientation: row
runtime: shiny
---

```{r}
require(flexdashboard)
require(shinyjs)
useShinyjs(rmd=TRUE)
```

```{r}

observe({
  query = getQueryString()
  ID = query[['ID']]
  active = query[['IDactive']]

  if (!is.null(ID)) {
    updateTextInput(session, 'ID', value = ID)
  }
  if(!is.null(active)){
    if(active == '0'){
      disable('ID')
    }
  }
})

```

Column {.sidebar}
-----------------------------------------------------------------------

```{r}
textInput("ID", label = "ID:", width = "150px", placeholder = "ID", value = "hi")
```

Row  {data-height=60}
------------------------------------

### Other 

Row 
-------------------------------------

### Query string

```{r}
renderUI({
    query <- getQueryString()
    return(HTML(jsonlite::toJSON(query)))
})
```

当我运行此命令并在查询字符串中添加适当的变量时,textInput会更新,但不会被禁用。 Dean Attali(shinyjs的作者)有told me这是因为当disable运行时textInput还不存在。

我的问题是:当Rmarkdown页面加载时,如何在查询字符串中使用变量来禁用窗口小部件?

1 个答案:

答案 0 :(得分:1)

所有UI都在runtime: shiny文档中动态呈现,因此观察者确实会在文本输入或shinyjs加载之前运行。因此,即使textInput看起来是静态的,它实际上也是通过renderUI / uiOutput在文档中呈现的。

您必须在动态UI渲染后让观察者运行,或者使用预渲染的Shiny文档(runtime: shiny_prerendered)来获取静态UI。以下是每个例子:

延迟观察者执行,直到动态UI呈现

---
title: "Test session variables"
output: 
  flexdashboard::flex_dashboard:
    orientation: row
runtime: shiny
---

```{r}
require(flexdashboard)
require(shinyjs)
useShinyjs(rmd=TRUE)
```

```{r}
observeEvent(input$ID, {
  observe({
    query = getQueryString()
    ID = query[['ID']]
    active = query[['IDactive']]

    if (!is.null(ID)) {
      updateTextInput(session, 'ID', value = ID)
    }

    if (!is.null(active)) {
      if (active == '0') {
        disable('ID')
      }
    }
  })
}, once = TRUE)
```

Column {.sidebar}
-----------------------------------------------------------------------

```{r}
textInput("ID", label = "ID:", width = "150px", placeholder = "ID", value = "hi")
```

Row  {data-height=60}
------------------------------------

### Other 

Row 
-------------------------------------

### Query string

```{r}
renderUI({
  query <- getQueryString()
  return(HTML(jsonlite::toJSON(query)))
})
```

预呈现的闪亮文件

---
title: "Test session variables"
output: 
  flexdashboard::flex_dashboard:
    orientation: row
runtime: shiny_prerendered
---

```{r}
require(flexdashboard)
require(shinyjs)
useShinyjs(rmd=TRUE)
```

```{r, context="server"}
observe({
  query = getQueryString()
  ID = query[['ID']]
  active = query[['IDactive']]

  if (!is.null(ID)) {
    updateTextInput(session, 'ID', value = ID)
  }

  if (!is.null(active)) {
    if (active == '0') {
      disable('ID')
    }
  }
})

output$query <- renderUI({
  query <- getQueryString()
  return(HTML(jsonlite::toJSON(query)))
})
```

Column {.sidebar}
-----------------------------------------------------------------------

```{r}
textInput("ID", label = "ID:", width = "150px", placeholder = "ID", value = "hi")
```

Row  {data-height=60}
------------------------------------

### Other 

Row 
-------------------------------------

### Query string

```{r}
uiOutput("query")
```

动态呈现UI

只是另一个选项 - 您还可以根据查询字符串动态呈现textInput。我在这里使用shinyjs::disabled来禁用文本输入。

---
title: "Test session variables"
output: 
  flexdashboard::flex_dashboard:
    orientation: row
runtime: shiny
---

```{r}
require(flexdashboard)
require(shinyjs)
useShinyjs(rmd=TRUE)
```

Column {.sidebar}
-----------------------------------------------------------------------

```{r}
renderUI({
  query <- getQueryString()
  value <- if (!is.null(query[['ID']])) query[['ID']] else "hi"
  disabled <- identical(query[['IDactive']], "0")

  idInput <- textInput("ID", label = "ID:", width = "150px", placeholder = "ID", value = value)

  if (disabled) {
    shinyjs::disabled(idInput)
  } else {
    idInput
  }
})
```

Row  {data-height=60}
------------------------------------

### Other 

Row 
-------------------------------------

### Query string

```{r}
renderUI({
  query <- getQueryString()
  return(HTML(jsonlite::toJSON(query)))
})
```