以下代码是我的Shiny ui:
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")
})
})
每次运行时都会出现以下错误:
“eval中的错误(expr,envir,enclos):找不到对象'输入'”
我正在尝试渲染一个简单的条形图,该条形图将在每个国家/地区的合同支出的净值和绝对值之间切换,但它不会从selectInput
框中识别出名为“split”的输入。 / p>
以下是我的数据框的示例:
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))
答案 0 :(得分:4)
问题在于ggplot在您提供的数据框的上下文中评估变量,花费在您的案例中。你想要的是:
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")
})
})
显然,您可以将支出 df替换为CSV导入。
答案 1 :(得分:2)
我已通过“ <<-”解决了此问题。我不确定,但这与全球环境有关。
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")
})