How come I cannot access my global variable in my shiny_prerendered document?

Hello everyone,

I am writing an RMarkdown document with runtime = shiny_prerendered, but having trouble accessing what should be a global variable.

Basically, I want the user to upload a file to the document. Once that happens, the variable FILE_VALID will switch from FALSE to TRUE. If FILE_VALID == TRUE, a text message should render elsewhere in the document. However, the message does not render even when the value of FILE_VALID changes.

I did my best to create a reprex of the document using online tutorials for guidance, but I think I was unsuccessful. Therefore, I hope you will accept my RMarkdown code as a stand-in reprex.

If anyone can spot the bug(s) in my code, would you please share your insight?

---
title: "Reprex"
output: html_document
---
```{r setup, include=FALSE}
library(shiny)
library(rmarkdown)
knitr::opts_chunk$set(echo = TRUE)
```
```{r load_file}
fluidPage(
  fileInput('file', 'Load any file and then click "Verify file":'),
  actionButton('verify', 'Verify file')
)

FILE_VALID <<- FALSE
```
```{r, context = 'server'}
observeEvent(input$file, {
  if (isTruthy(input$load_file_upload) == FALSE) {
    FILE_VALID <<- TRUE
  }
})
```
```{r show_text}

fluidPage(
  textOutput('text')
)
```
```{r, context = 'server'}
output$text <- renderText({
  if (FILE_VALID == TRUE) {
    print("hi")
  }
})
```
```

I think you are just missing some context about reactivity

FILE_VALID is not a reactive value in your example so renderText() won't be rerun once you upload the file. You're missing some reactivity I believe.

Quick example without using the upload widget

---
title: "Reprex"
output: html_document
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r load_file}
fluidPage(
  actionButton('clickme', 'Click me')
)
```

```{r context = "server"}
FILE_VALID <- reactiveVal(FALSE)
observeEvent(input$clickme, {
    FILE_VALID(TRUE)
})
```

```{r show_text}
fluidPage(
  textOutput('text')
)
```

```{r context = "server"}
output$text <- renderText({
  if (isTruthy(FILE_VALID())) {
    print("hi")
  }
})
```

I would suggest looking at documentation about reactive values.

Disclaimer: I am not expert in shiny, just wanted to check shiny prerendered was working ok.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.