R Shiny error: object input not found

R Shiny error: object input not found

下面的代码是我的 Shinyui:

library(shiny)
shinyUI(fluidPage(

titlePanel("All Country Spend"),

  sidebarLayout(
    sidebarPanel(  selectInput("split", 
        label = "Choose Fill For the Chart",
        choices = c("Action.Obligation","Action_Absolute_Value"),
      selected = "Action.Obligation"
        )
            ),
    mainPanel(plotOutput("SpendChart"))
)
))

服务器代码如下:

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {

spend <- read.csv("data/Ctrdata.csv")
output$SpendChart <- renderPlot({

    Country <- spend$Principal.Place.of.Performance.Country.Name
    ggplot(spend, aes(x = Country, y = input$split)) + geom_bar(stat = "identity")

})

})

每次我 运行 我都会收到以下错误:

"Error in eval(expr, envir, enclos) : object 'input' not found"

我正在尝试呈现一个简单的条形图,它将在每个国家/地区的合同支出的净值和绝对值之间切换,但它无法识别我从 [=13= 中命名为 "split" 的输入]框。

这是我的数据框示例:

data.frame(Country = c("Turk", "Turk", "Saudi", "Saudi", "Ger", "Ger"),
Action.Obligation = c(120,-345,565,-454, 343,-565),
Action_Absolute_Value = c(120,345,565,454,343,565))

问题在于 ggplot 在提供的数据框的上下文中评估变量,在您的情况下 spend。你想要的是:

ggplot(spend, aes_string(x = "Country", y = input$split))

所以你的工作 server.R 代码是:

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {

spend <- data.frame(Country = c("Turk", "Turk", "Saudi", "Saudi", "Ger", "Ger"),
                    Action.Obligation = c(120,-345,565,-454, 343,-565),
                    Action_Absolute_Value = c(120,345,565,454,343,565))

    output$SpendChart <- renderPlot({


        ggplot(spend, aes_string(x = "Country", y = input$split)) +
            geom_bar(stat = "identity")

    })

})

显然,您可以用 CSV 导入替换 spend df。

我已经用“<<-”解决了这个问题。不太清楚,不过跟全球环境有关:

output$SpendChart <- renderPlot({

choice <<- input$split

    Country <- spend$Principal.Place.of.Performance.Country.Name
    ggplot(spend, aes(x = Country, y = choice)) + geom_bar(stat = "identity")

})