有光泽的conditionalPanel更新问题

时间:2015-06-30 17:11:20

标签: r shiny

我有一个Shiny应用程序没有给出任何错误,但显然我的conditionalPanel无法正常工作。当我选择输入时,一些图表会更新,而有些图表则不会更新。例如,如果我选择星期并将条件更改为0或1,则图表会更新,但如果我选择rel,则图表会更新1但不会更新4(如果我在Shiny应用程序之外执行此操作,则适用于所有情况)。这是代码的样子: UI.R

shinyUI(pageWithSidebar(
headerPanel(' '),
sidebarPanel(
selectInput('zcol', 'Variable to be fixed',  names(taxi[,-c(1,4,5,7,8,9,10,11)])),
conditionalPanel(condition = "input.zcol == 'week'",
                 selectInput("levels", "Levels",c(0,1)
                 )),
conditionalPanel(condition = "input.zcol == 'tollfree'",
                 selectInput("levels", "Levels",c(0,1)
                 )),
conditionalPanel(condition = "input.zcol == 'rel'",
                 selectInput("levels", "Levels",c(1,4)
                 )),
conditionalPanel(condition = "input.zcol == 'source'",
                 selectInput("levels", "Levels",c(1,2)
                 )),
conditionalPanel(condition = "input.zcol == 'hour'",
                 selectInput("levels", "Levels",c(seq(0,23))
                 ))
),
mainPanel(
plotOutput('plot1'),
plotOutput('plot2')
)
))

Server.R

shinyServer(function(input, output, session) {

simiData <- reactive({ 
 eval(substitute(taxi %>%  group_by(simi.mean,col) %>% summarise(mean = mean(prop.conv)) %>% 
                                  filter(col==input$levels) %>% select(simi.mean,mean), 
                                list(col=as.symbol(input$zcol))))
  })

 distData <- reactive({ 
eval(substitute(taxi %>%  group_by(dist.mean,col) %>% summarise(mean = mean(prop.conv)) %>% 
                  filter(col==input$levels) %>% select(dist.mean,mean), 
                list(col=as.symbol(input$zcol))))
})

output$plot1 <- renderPlot({
 plot(simiData(),xlim=c(0,max(simiData()$simi.mean)),ylim=c(0,max(simiData()$mean)))
})

output$plot2 <- renderPlot({
plot(distData())
})

 })

有什么建议吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

您不能拥有多个具有相同inputId(“级别”)的selectInput。每个人都需要一个唯一的inputId。

答案 1 :(得分:1)

您可以使用updateSelectInput(session,"levels",choices = newValue))来拥有一个并使用input$levels更新选项,而不是多个选择输入。每次input$zcol更改时,您都可以使用观察者更改taxi <- data.frame(week=sample(1:10),hour=sample(1:10)) runApp(list( ui = shinyUI( fluidPage( sidebarLayout( sidebarPanel( selectInput("zcol", 'Variable to be fixed', names(taxi)), selectInput("levels", "Levels",1:5) ), mainPanel( plotOutput('plot1') ) ) ) ), server = function(input, output, session) { output$plot1 <- renderPlot({ plot(taxi[1:input$levels,]) }) observe({ if ( input$zcol == 'week') { updateSelectInput(session, "levels", choices = 1:5) } else if(input$zcol == 'hour') { updateSelectInput(session, "levels", choices = 6:10) } }) } )) 的选项。 / p>

以下是如何使用观察者

更新选择输入的基本示例
// Input assuming you've sanitised it
string myInputString = "720000.00";

// Remove the decimals
myInputString = myInputString.Substring(0, myInputString.IndexOf("."));

// The count
int count = 0;

// Loop through and count occurrences
foreach (char c in myInputString) 
{
    if (c == "0")
    {
        count++;
    }
}
相关问题