Sté*_*ent 6 r ggplot2 shiny jsonlite
考虑这个闪亮的应用程序:
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)
Run Code Online (Sandbox Code Playgroud)
当我选择单选按钮“boxplot”时,来自jsonlite包的这条消息出现在 R 控制台中:
asJSON(keep_vec_names=TRUE) 的输入是一个命名向量。在未来的 jsonlite 版本中,将不支持此选项,命名向量将被转换为数组而不是对象。如果您想要 JSON 对象输出,请改用命名列表。见?toJSON。
我想了解发生了什么。我应该怎么做才不会收到这条消息?我担心我的应用程序会被未来版本的jsonlite.
小智 5
打印绘图而不是返回绘图对象应该可以:
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)
Run Code Online (Sandbox Code Playgroud)
很棒的代表,谢谢。我最近遇到了这个问题,这个解决方案适合我的目的。希望这可以帮助其他遇到同样问题的人。
还有一些与shiny此相关的未解决问题:https://github.com/rstudio/shiny/issues/2673