Warnings : JsonLite in shiny app

Using ggplot function in a shiny output, i receive this warning only when x-axis is reduced to single factor.

" 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. "

If x-axis is > 1 factor, works without warning.

I tried to convert my dataset(dataframe) in a named list :

df <- data.frame(table(type)) 
colnames(df) <- c("Var1", "Var2")
df$Var1 <- fct_rev(df$Var1)

dataset <- list(df)
names(dataset) <- "Test"

Then use the ggplot function but nothing changes :

graph <- ggplot(dataset$Test, aes(x=Var1, y=Var2))
graph_dyn <- graph + coord_flip()

If not possible, is there a way to not display the Json warning in the console?

options(warn=-1) # not working

I don't know what's going on that it would mention jsonLite anywhere in relation to what you are doing...
What happens when you run this reprex ?

Var1 <- as.factor(c(1,1,1))
Var2 <- c(1,2,3)

dataset <- data.frame(
graph <- ggplot(dataset, aes(x=Var1, y=Var2))
graph_dyn <- graph + coord_flip()


mydflist <- list(dataset)
names(mydflist) <- "Test"

graph <- ggplot(mydflist$Test, aes(x=Var1, y=Var2))
graph_dyn <- graph + coord_flip()


oh, and in general you might use suppressWarnings() function


I found the same problem (better explained) on this topic : https://github.com/rstudio/shiny/issues/2673

Running the following example, as you can see, the second part generates Json message :


ui <- fluidPage(
                tabPanel("no message", plotOutput("quietPlot")),
                tabPanel("generates `Input to asJSON` message", plotOutput("noisyPlot"))

# Define server logic required to draw cols
server <- function(input, output, session) {
    output$quietPlot <- renderPlot({
            data = data.frame(z = c("a", "b"), n = c(1, 2)),
            aes(x = z, y = n)
        ) + geom_col()
    output$noisyPlot <- renderPlot({
            data = data.frame(z = c("a"), n = c(1)),
            aes(x = z, y = n)
        ) + geom_col() 

shinyApp(ui = ui, server = server)

In my case, dataset come from a reactive function, so x-axis can have 1 or multiple factors : the Json message appears in the R console when only 1 factor in the variable.

weird bug ! I did experience it in your reprex.
I found a way to make it go away.
relies on loading plotly library, changing from plotOutput to plotlyOutput, and finally from renderPlot to renderPlotly. Everything else the same the warning disappears.


1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.