3

What is the most simple way to use R shiny in markdown to obtain an input from the user, say a number, and use it in the rest of the analysis? All the example I have seen so far show how to use such input in generating a specific figure, but not in retaining this value as a variable to use in other code chunks.

2 Answers 2

2
+25

You can use shiny inputs across chunks. Just think of the first chunk as your ui.R where you specify the input (e.g. numericInput("INPUT_ID", ...) and the second chunk as the server.R where you use the input as input$INPUT_ID.

Reproducible example:

---
output: html_document
runtime: shiny
---

# Chunk 1:
```{r, echo = FALSE}
numericInput("n", "How many cars?", 5)
```

# Chunk 2:
```{r, echo = FALSE}
renderTable({
 head(cars, input$n)
})
```

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but it's still not what I meant. I want to use the parameter outside of this render table to instruct the execution of my analysis and not only the figure generation. I get the following error when trying to use the input: 'Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)'
Can u share a minimal reproducible version of the code producing the error. And could you give an example of your "analysis"
1

Without an example of how you want the parameter outside of the renderTable to instruct the execution of your analysis it is hard to answer your question. But basically there are only two major options which come with a couple of sub-options.

Below is an update of my former answer:

1. You want to change the structure or layout of the flexdashboard depending on either:

1.1 User input
At the moment I do not see how this could be done, which does not mean that there is no solution - I might just be not aware of it.

1.2 User information (for example contained in the session info)
For some special cases a workaround using the isolate function is possible, see my answer to a related question here for example.

1.3 Your data
There are ways of nesting several Rmarkdown scripts to produce child templates as subpages based on the structure of the data that you read in (and might not know in advance). Here I found a very intriguing example.

2. You want to use a user input value within your analysis.

Although not clearly visible in the Rmarkdown structure, the analysis is taking place on the server part in shiny where you can use reactive and reactiveValues as they are. In this case BigDataScientist already answered your question. Of course you can use the input$n not only in renderTable but also in any other reactive expression or a reactiveValue which you can then use in some sort of render statement to show the users of your app the results of your analysis.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.