以下代码是我的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"))
)
))
Run Code Online (Sandbox Code Playgroud)
以下是服务器代码:
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")
})
})
Run Code Online (Sandbox Code Playgroud)
每次运行它我都会收到以下错误:
"eval中的错误(expr,envir,enclos):找不到对象'input'"
我试图渲染一个简单的条形图,它将在每个国家/地区的合同支出的净值和绝对值之间切换,但它不会从selectInput框中识别出名为"拆分"的输入.
这是我的数据框的示例:
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))
Run Code Online (Sandbox Code Playgroud)
The problem is with ggplot evaluating the variable in the context of the data frame provided, spend in your case. What you want is:
ggplot(spend, aes_string(x = "Country", y = input$split))
Run Code Online (Sandbox Code Playgroud)
So your working server.R code is:
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")
})
})
Run Code Online (Sandbox Code Playgroud)
Obviously, you can replace the spend df with your CSV import.