根据服务器中的条件逻辑呈现 Shiny 的用户输入

标签 r shiny

我正在尝试设置一个 Shiny 的导航栏面板页面,用户可以根据在一组单选按钮中做出的初始选择来控制我显示的更改。我直接在 ui 中呈现单选按钮,然后在 Server.r 中的“观察到的”逻辑控制结构中构建条件控件。弹出错误是因为我的初始 if 语句评估为 false,因此过滤我的数据所需的控件不会呈现,因此 dplyr::filter() 不会得到预期的结果。我想弄清楚的是,如果我运行它,单选按钮将呈现为所有其他井面板为空白。然后,如果我单击几个单选按钮,其他 ui 控件最终会出现,然后通过单击“区域”按钮返回调用图表。如何获得第一个 if 语句以查看已选择“Regions”?或者有没有办法让单选按钮在观察者开始后更新?无论如何,任何见解将不胜感激。

错误:

 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

Ui.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")
      )
    )
 )
)

最佳答案

你打错了。除此之外,您还可以使用 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")  
      }
    } 
  }) 
}
))

enter image description here

关于根据服务器中的条件逻辑呈现 Shiny 的用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25556612/

相关文章:

r - 使用 spplot 重叠绘制两个 SpatialPolygonsDataFrames

r - 在 R 中结合头尾方法

javascript - Shiny 模块和 renderUI 传递 javascript 代码

r - 在条形图中设置可变列大小(R Shiny)

R data.table 列通配符与 sprintf

r - glmnet:相同的 lambda 但 glmnet() 和 cv.glmnet() 的系数不同

r - 在 R 中按组有效地复制矩阵行

R shiny - Shiny 模块函数中最后单击的按钮 id

r - Shiny.io 内存不足

r - 无法将用户输入传递到 R Shiny 模块