【发布时间】:2014-10-22 18:38:49
【问题描述】:
我正在尝试设置一个闪亮的导航栏面板页面,我根据一组单选按钮中的初始选择显示用户控件的更改。我直接在 ui 中渲染单选按钮,然后在 Server.r 中的“观察到的”逻辑控制结构内构建条件控件。弹出错误是因为我的初始 if 语句评估为 false,因此过滤我的数据所需的控件不会被渲染,因此 dplyr::filter() 没有得到预期的结果。我想弄清楚的是,如果我运行它,单选按钮会在所有其他井面板空白的情况下呈现。然后,如果我单击几个单选按钮,其他 ui 控件最终会出现,然后通过单击“区域”按钮返回图表被调用。如何获得第一个 if 语句以查看选择了“区域”?或者有没有办法让观察者开始后更新单选按钮?无论如何,任何见解都将不胜感激。
错误:
Listening on http://...............
Error in eval(substitute(expr), envir, enclos) :
incorrect length (0), expecting: 192
全球.R:
##### Play data
options(stringsAsFactors = FALSE)
exp <- data.frame(scenario=rep(c(1,2,3,4),each=48),
region=rep(c("Western","Central","Southern","Northern","Capital","Hudson"),
times=4,each=8),
period=rep(c(1,2,3,4),times=48),
price=rep(c("Hi","Lo"),times=24,each=4),
val=rnorm(192,5,1.5))
##### Functions
all_values <- function(x) {
if(is.null(x)) return(NULL)
unique(x[,1])
}
服务器.R
library(shiny)
library(ggvis)
library(plyr)
library(dplyr)
shinyServer(function(input, output, session) {
observe({
if(input$comp=="Regions") { # For comparing regions #######################################
###### Define Controls #######
output$regionO <- renderUI({ checkboxGroupInput(inputId="regionI", label = h4("Regions"),
choices = list("Western", "Central", "Southern","Northern", "Capital", "Hudson"),
selected = "Capital")
})
output$scenarioO <- renderUI({ selectInput(inputId="scenarioI", label = h4("Scenario"),
choices = list("L1" = 1, "L2" = 2, "L3" = 3,"L4" = 4),
selected = 1)
})
output$fuelO < renderUI({ selectInput(inputId="fuelI", label = h4("Fuel Price"),
choices = list("High Price" = "Hi", "Low Price" = "Lo"),
selected = "Lo")
})
###### Define Data ###########
choose <- reactive({
chooseD <- exp %>% filter(scenario == input$scenarioI, region %in% input$regionI, price == input$fuelI)
names(chooseD)<-c("scenario","NYregion","period","price","val")
return(chooseD)
})
sPlot1 <- reactive({
choose %>% ggvis(~factor(period),~val,stroke=~NYregion) %>%
layer_lines(strokeWidth:=2.5,strokeWidth.hover:=5) %>%
add_axis("x",title="Analysis Period") %>% add_tooltip(all_values,"hover")
})
sPlot1 %>% bind_shiny("plot1")
sPlot1 %>% bind_shiny("plot2")
}
})
# The code below is commented out so I can test the first condition of input$comp
#else if(data==2) { # For comparing scenarios ######
###### Define Controls #######
###### Define Data ###########
#} else { # For comparing fuel prices ##############
###### Define Controls #######
###### Define Data ###########
#}
}) # End Shiny Server
用户界面.R:
library(shiny)
library(ggvis)
library(plyr)
library(dplyr)
shinyUI(navbarPage("TBR Economic Results Viewer", # theme="bootstrapCR.css",
tabPanel("Summary Results",
fluidRow(
column(2,
wellPanel(
radioButtons(inputId="comp", label = h4("Comparison"),
choices = list("Regions", "Scenarios", "Prices"),
selected ="Regions")
),
wellPanel(
uiOutput("regionO"),
br(),
uiOutput("scenarioO"),
br(),
uiOutput("fuelO")
)
),
column(5,
wellPanel(
ggvisOutput("plot1"),
textOutput("test")
)
),
column(5,
wellPanel(
ggvisOutput("plot2")
)
)
)
),
tabPanel("Detailed Results",
mainPanel(
plotOutput("plot2")
)
)
)
)
【问题讨论】:
-
你打错了
output$fuelO < renderUI应该是output$fuelO <- renderUI