I would like to create a simple shiny app to let the user export a yaml file. Creating the structure of a yaml file can be tricky. When I want to use the selectInput
to give the user multiple options it works when we only have that function. Here is some reproducible example:
library(shiny)
library(yaml)
ui <- fluidPage(
selectInput("Letters", "Select a letter:", LETTERS[1:4]),
downloadButton("export_yaml", "Export yaml")
)
server <- function(input, output) {
data <- reactive({
list(test_yaml = setNames(list(TRUE), input$Letters))
})
output$export_yaml <- downloadHandler(
filename = function() {
paste0('test_yaml', ".yaml")
},
content = function(file) {
yaml_input <- as.yaml(data())
cat(as.character(yaml_input), sep = "n", file = file, append = TRUE)
}
)
}
shinyApp(ui, server)
Output test_yaml.yaml
:
test_yaml:
A: yes
This is right, but when we add another option above the current function it returns an empty string as named value for the option. Here is the code:
library(shiny)
library(yaml)
ui <- fluidPage(
selectInput("Letters", "Select a letter:", LETTERS[1:4]),
downloadButton("export_yaml", "Export yaml")
)
server <- function(input, output) {
data <- reactive({
list(test_yaml = list(test = TRUE), # Changed code
setNames(list(TRUE), input$Letters))
})
output$export_yaml <- downloadHandler(
filename = function() {
paste0('test_yaml', ".yaml")
},
content = function(file) {
yaml_input <- as.yaml(data())
cat(as.character(yaml_input), sep = "n", file = file, append = TRUE)
}
)
}
shinyApp(ui, server)
Output test_yaml.yaml
:
test_yaml:
test: yes
'':
A: yes
This is not my expected output, It should be like this:
test_yaml:
test: yes
A: yes
Since we just give another input option and not a new nested name. So I was wondering how we could create such yaml structure?