以下是我想在runtime: shiny
的1}}的Rmarkdown文档中做的事情。
问题似乎是读取查询字符串必须在被动上下文中完成,但构建接口发生在被动上下文之外。这是我的第一次尝试,作为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页面加载时,如何在查询字符串中使用变量来禁用窗口小部件?
答案 0 :(得分:1)
所有UI都在runtime: shiny
文档中动态呈现,因此观察者确实会在文本输入或shinyjs
加载之前运行。因此,即使textInput
看起来是静态的,它实际上也是通过renderUI
/ uiOutput
在文档中呈现的。
您必须在动态UI渲染后让观察者运行,或者使用预渲染的Shiny文档(runtime: shiny_prerendered
)来获取静态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")
```
只是另一个选项 - 您还可以根据查询字符串动态呈现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)))
})
```