Json save of shiny multiple input

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 :slight_smile:

Best Regards,

Jb

use a list rather than a dataframe

library(jsonlite)

(temp <- data.frame(
  D1 = c("A","B"),#input$filter1,
  D2 = 1 #input$filter2
))
# D1 D2
# 1  A  1
# 2  B  1
(save_data <- toJSON(
  temp,
  pretty = TRUE
))
# [
#   {
#     "D1": "A",
#     "D2": 1
#   },
#   {
#     "D1": "B",
#     "D2": 1
#   }
# ] 

(temp <- list(
  D1 = c("A","B"),#input$filter1,
  D2 = 1 #input$filter2
))
# $D1
# [1] "A" "B"
# 
# $D2
# [1] 1

(save_data <- toJSON(
  temp,
  pretty = TRUE
))

# {
#   "D1": ["A", "B"],
#   "D2": [1]
# }

Hi Nirgrahamuk,

Thank you for the input and the fast answer !

However question remains open for integration with shiny widgets. How to extract multiple inputs from "input$filter1", e.g "A", "B" so that we have "input$filter1" = c("A", "B") ?

At the moment multiple input on shiny only results in two Json objects, not in a single Json object with a list with two elements.

Thank you :slight_smile:
Jb

library(shiny)
library(shinyWidgets)
library(jsonlite)
ui <- fluidPage(
  pickerInput(
    inputId = "filter1", 
    label = "Choose:", 
    choices = c("A", "B", "C", "D"),
    selected  = c("A","B"),
    multiple = TRUE
  ),
  numericInput(
    inputId = "filter2", 
    h3("Second input"), 
    value = 1),
  verbatimTextOutput("jsonview")
)

server <- function(input, output, session) {
  json_data  <- reactive({
    toJSON(
      list(
        D1 = input$filter1,
        D2 = input$filter2
      ),
    pretty = TRUE
    )})
  
output$jsonview <- renderPrint({
  req(json_data())
})

}

shinyApp(ui, server)
1 Like

Hi Nirgrahamuk,

Thanks a lot for the help, it works perfectly as expected :partying_face:

Best Regards

This topic was automatically closed 7 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.