Dear R community,
I'm trying to save inputs from Shiny widgets to a Json file. Everything is working fine except that I do not manage to save multiple inputs the way I want since I obtain:
[
{
"D1": "A",
"D2": 1
},
{
"D1": "B",
"D2": 1
}
]
instead of the expected
[
{
"D1": [ ["A"], ["B"] ],
"D2": ["1"]
}
]
Below a simplified version of the code:
#####################################################################################
.packages = c("shinydashboard",
"shinythemes",
"shinyWidgets",
"shiny",
"dplyr",
"shinycssloaders",
"jsonlite"
)
#####################################################################################
#####################################################################################
.inst <- .packages %in% installed.packages()
if(length(.packages[!.inst]) > 0) install.packages(.packages[!.inst])
lapply(.packages, require, character.only=TRUE)
#####################################################################################
#####################################################################################
ui = pageWithSidebar(
headerPanel('Title'),
fluidRow(
box(
title = "First input",
solidHeader = TRUE,
status = "primary",
height = 750,
width = 3,
pickerInput(
inputId = "filter1",
label = "Choose:",
choices = c("A", "B", "C", "D"),
multiple = TRUE
),
verbatimTextOutput("default"),
verbatimTextOutput("placeholder", placeholder = TRUE),
numericInput(
inputId = "filter2",
h3("Second input"),
value = 1)
),
p("Include actionButton to prevent write occuring before user finalises selection"),
actionButton("generateButton","Write Data")),
mainPanel(tableOutput("test1"))
)
#####################################################################################
#####################################################################################
server = function(input, output) {
chemicalInput <- reactive({
if(input$generateButton == 0){return()}
isolate({
input$generateButton
temp <- data.frame(
D1 = input$filter1,
D2 = input$filter2
)
save_data <- toJSON(
temp,
pretty = TRUE
)
})
write(save_data, "./save.json")
})
output$test1 <- renderUI({chemicalInput()})
#
output$default <- renderText({ input$filter1 })
output$placeholder <- renderText({ length(input$filter1) })
}
#####################################################################################
#####################################################################################
runApp(list(ui = ui, server = server))
Any idea how to:
- Obtain the same formatting, with the , working normally when playing with a simple R script ?
- Have the A and B stored in the same Json object ?
Thank you in advance
Best Regards,
Jb