【问题标题】:R Shiny reactivity error, how can I fix it?R Shiny 反应性错误,我该如何解决?
【发布时间】:2020-07-02 12:22:53
【问题描述】:

我有以下闪亮的应用程序: 数据集:(https://www.kaggle.com/rush4ratio/video-game-sales-with-ratings)

library(shinythemes)
library(shiny)
library(ggplot2)
ui = navbarPage(theme = shinytheme("united"),"Video Games Dashboard",
                tabPanel("Dashboard",
                         sidebarLayout(
                             sidebarPanel(
                                 selectInput(inputId = "dataset",
                                         label = "Choose a dataset:",
                                         choices = colnames(data)),
                             
                         ),
                         mainPanel(
                             plotOutput(outputId = "ggPlot"),
                             plotOutput(outputId = "ggPlot2"),
                             
                         )
                     )
            ),
            tabPanel("Summary",
            )
)
server <- function(input, output) {


output$ggPlot <- renderPlot({
    ggplot ( data=data,aes(x=Global_Sales, y=input$dataset)) +
        geom_bar(stat="identity" ,fill="steelblue") +
        coord_flip() +
        theme_minimal()
})

output$ggPlot2 <- renderPlot({
    
    ggplot ( data=data,aes(x=Global_Sales, y=Platform)) +
        geom_bar(stat="identity" ,fill="steelblue") +
        
        theme_minimal()
})
}

shinyApp(ui = ui, server = server)

看起来像这样:

正如你所看到的,我想在第一个 plot("ggPlot") 中做同样的事情,就像在第二个 plot("ggPlot2") 中一样,只是第一个 plot 是响应式的,您可以选择要显示的数据表的每一列它在情节中。 但是,我一直收到这条消息:

asJSON(keep_vec_names=TRUE) 的输入是一个命名向量。在 jsonlite 的未来版本中,将不支持此选项,命名向量将被转换为数组而不是对象。如果您想要 JSON 对象输出,请改用命名列表。请参阅 ?toJSON。

有人知道如何解决这个问题吗?这种方法甚至可能吗?

谢谢!

【问题讨论】:

  • 你确定第一个情节是你所期望的,只有一个酒吧?如果要将y 设置为名称为input$dataset 的变量,可以使用aes_string(x="Global_Sales", y=input$dataset)。在您的代码中,y 未设置为数据变量,因为input$dataset 是一个字符串。
  • 嗨,Stéphane,当我写 aes_string 时,你很难过它有效!谢谢!为什么 input$dataset 是一个字符串?

标签: r shiny reactive


【解决方案1】:

当两个轴之一上有单个项目时会出现此警告 - 请参阅this shiny issue

但是您的代码不会产生您期望的图:input$dataset 是数据列的名称,因此它是一个字符串,然后当您执行aes(x = Global_Sales, y = input$dataset) 时, aesy 参数未设置为数据的变量,而是设置为字符串。因此,您在 y 轴上有一个项目,因此会出现警告。

当你有这些变量的名字时,要将aes的参数设置为数据的一些变量,你可以使用aes_string

aes_string(x = "Global_Sales", y = input$dataset)

【讨论】:

    猜你喜欢
    • 2014-12-14
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多