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
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:
2 Comments
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.
