【问题标题】:Rendering Shiny user inputs based on conditional logic in server基于服务器中的条件逻辑呈现闪亮的用户输入
【发布时间】: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 &lt; renderUI应该是output$fuelO &lt;- renderUI

标签: r shiny


【解决方案1】:

你有一个错字。除此之外,您还可以设置条件,例如 input$fuelI:

library(shiny)
library(ggvis)
library(plyr)
library(dplyr)
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))
all_values <- function(x) {
  if(is.null(x)) return(NULL)
  unique(x[,1])
}

运行APP:

runApp(list(ui = 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"))
                            )
)
, server = function(input, output, session) {
  observe({
    if(input$comp=="Regions") { 
      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")
      })
      if(!is.null(input$fuelI)){
        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")  
      }
    } 
  }) 
}
))

【讨论】:

  • @jd ... 感谢您抽出宝贵时间查看此内容并发布一些代码。我在发布到 stackOverFlow 时引入了错字。以 Ui 元素的存在为条件是一个很好的建议和实践。我想它导致观察者在执行应用程序时更新。我仍然必须处理测试 Ui 的存在并且还能够在不让 Ui 自动恢复到它的预定义选定值的情况下更改它的值,但这是另一个问题,我现在正在研究一个......干杯
  • 我基本上需要以井面板中是否存在任何控件为条件并且它可以工作。我想知道我是否可以仅以井板的存在为条件......无论如何,这里是我将放在井板底部的控件以进行条件。 ui控件的功能也有一些直观的感觉,这很酷。 output$tmpO
猜你喜欢
  • 2016-08-16
  • 2015-11-15
  • 2016-10-26
  • 2015-05-06
  • 2021-11-25
  • 2020-07-26
  • 2014-02-26
  • 2020-03-19
  • 2023-03-03
相关资源
最近更新 更多