6

Consider this shiny app:

library(shiny)
library(ggplot2)

ui <- fluidPage(
  radioButtons("type", "Type of plot", choices = c("density", "boxplot")),
  plotOutput("plot")
)

server <- function(input, output){
  output[["plot"]] <- renderPlot({
    if(input$type == "density"){
      ggplot(iris, aes(Sepal.Length)) + geom_density()
    }else{
      ggplot(iris, aes(x = "", y = Sepal.Length)) + geom_boxplot()
    }
  })
}

shinyApp(ui, server)

When I select the radio button "boxplot", this message from the jsonlite package appears in the R console:

Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.

I would like to understand what's going on. What should I do to not get this message ? I fear that my app will be broken with a future version of jsonlite.

Stéphane Laurent
  • 48,421
  • 14
  • 86
  • 170
  • 1
    The issue seems to be caused by `x = ""`, try to delete the argument and the message doesn't appear (or use `x = 0` instead). – ismirsehregal Oct 18 '19 at 12:15
  • 2
    @ismirsehregal Thanks, you're right. I can remove this argument and add `scale_x_continuous(breaks = NULL)`. But this warning didn't occur with the previous version of shiny. I will report. – Stéphane Laurent Oct 18 '19 at 13:55
  • 2
    Did you report the issue somewhere? I'm running into the same. – Bob Jansen Mar 06 '20 at 14:39

1 Answers1

3

Printing your plot rather than returning the plot object should work:

library(shiny)
library(ggplot2)

ui <- fluidPage(
  radioButtons("type", "Type of plot", choices = c("density", "boxplot")),
  plotOutput("plot")
)

server <- function(input, output){
  output[["plot"]] <- renderPlot({
    if(input$type == "density"){
      ggplot(iris, aes(Sepal.Length)) + geom_density()
    }else{
      print(ggplot(iris, aes(x = "", y = Sepal.Length)) + geom_boxplot())
    }
  })
}

shinyApp(ui, server)

Great reprex, thanks. I recently ran into this issue and this solution worked for my purposes. Hope this helps someone else with the same problem.

There are also some open issues in shiny related to this: https://github.com/rstudio/shiny/issues/2673

v44k3
  • 61
  • 5