Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonlite warning with shiny 1.4.0 #2673

Open
stla opened this issue Oct 18, 2019 · 6 comments
Open

jsonlite warning with shiny 1.4.0 #2673

stla opened this issue Oct 18, 2019 · 6 comments

Comments

@stla
Copy link

@stla stla commented Oct 18, 2019

Hello,

Consider this 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 you select the radio button "boxplot", this message 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.

It is caused by x = "" in the aes. If I remove this argument, there's no warning anymore.

This message didn't appear with shiny < 1.4.0.

@harrismcgehee
Copy link

@harrismcgehee harrismcgehee commented Apr 13, 2020

Similar experience with any single x axis item:

library(shiny)
library(ggplot2)

ui <- fluidPage(
            tabsetPanel(
                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({
        ggplot(
            data = data.frame(z = c("a", "b"), n = c(1, 2)),
            aes(x = z, y = n)
        ) + geom_col()
    })
    output$noisyPlot <- renderPlot({
        ggplot(
            data = data.frame(z = c("a"), n = c(1)),
            aes(x = z, y = n)
        ) + geom_col() 
    })
}

shinyApp(ui = ui, server = server)

@jntrcs
Copy link

@jntrcs jntrcs commented Apr 16, 2020

Also receiving this warning whenever x-axis is reduced to single factor.

@kaijagahm
Copy link

@kaijagahm kaijagahm commented Jan 20, 2021

I am getting this warning message when I use onRestore and onBookmark. When I run the same app without those functions, the warning disappears.

@wch
Copy link
Collaborator

@wch wch commented Jan 22, 2021

@kaijagahm Can you file a new issue, with a minimal reproducible example?

For the others that have experienced this issue, when I run the example apps, I don't see the message. It's possible that it has been fixed since the issue was originally filed. If you still see that message, please provide the output of sessionInfo() or sessioninfo::session_info().

@SigurdJanson
Copy link

@SigurdJanson SigurdJanson commented Jan 30, 2021

I am not sure this issue is limited to ggplot. I have the same warning whenver I the value argument is a named vector. The problem is there even in shiny 1.6 (session info below). Once I put unname() around it, it works.

I'd prefer if the update-function would just ignore the names in the vector.

library(shiny)

ui <- fluidPage(
    titlePanel("as JSON Warning Sample"),
    mainPanel(
       sliderInput("inpSlider", "Just another value", 1, 10, 5),
       actionButton("btnReset", "Reset to 1")
    )
)

server <- function(input, output, session) {
    observeEvent(
        input$btnReset, 
        {    # NAMED VECTOR CAUSES THE WARNING
             updateSliderInput(session, "inpSlider", value = c(a = 1)) 
        })
}

shinyApp(ui = ui, server = server)
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shiny_1.6.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        withr_2.4.1       digest_0.6.27     later_1.1.0.1    
 [5] mime_0.9          R6_2.5.0          jsonlite_1.7.2    lifecycle_0.2.0  
 [9] xtable_1.8-4      magrittr_2.0.1    cachem_1.0.1      rlang_0.4.10     
[13] promises_1.1.1    jquerylib_0.1.3   bslib_0.2.4       ellipsis_0.3.1   
[17] tools_4.0.3       tinytex_0.29      rsconnect_0.8.16  httpuv_1.5.5     
[21] xfun_0.20         fastmap_1.1.0     compiler_4.0.3    htmltools_0.5.1.1
[25] sass_0.3.0   

SigurdJanson added a commit to SigurdJanson/Fate-Explorer.R that referenced this issue Jan 30, 2021
@wch
Copy link
Collaborator

@wch wch commented Feb 1, 2021

@SigurdJanson Thanks for that example. This is a difficult (or really, almost impossible) problem to fix in general, because of R's nature as a loosely-typed language.

The root issue is that jsonlite doesn't like named atomic vectors when keep_vec_names=TRUE:

> jsonlite::toJSON(c(a=1), keep_vec_names=TRUE)
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.
{"a":1} 

In contrast, named lists are fine:

> jsonlite::toJSON(list(a=1), keep_vec_names = TRUE)
{"a":[1]} 

In the examples posted previously, it seems that something in ggplot2 generated data with named atomic vectors. In @SigurdJanson's example, it's the call to updateSliderInput() that tries to send a named vector.

In Shiny, we do some input validation, we don't check every possible input for every possible kind of invalid value. (It's similar to the vast majority of R code in this way.) With a strongly-typed language, many checks just happen automatically; with a loosely-typed language like R, deciding which things to check (and whether to "fix" them for the user) is much more subjective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants