因此,在您的代码中查看更多逻辑会很有帮助。我想我会说一般来说,理解反应式表达式在程序逻辑的上下文中是如何工作的非常重要。我会尝试在闪亮的主页上阅读尽可能多的代码。这是我写的一个快速脚本,我认为它可以满足您的要求。干杯。
全球.r
library(plyr)
library(dplyr)
exp <- data.frame(Ind=rep(c("a","b"),each=50),val1=rgamma(100,10,5),val2=rnorm(100,2,3.5))
服务器.r
library(shiny)
library(ggvis)
shinyServer(function(input, output, session) {
output$selectO <- renderUI({ selectInput(inputId="selectI", label = h4("Level to Plot"),
choices = list("a","b","c"),selected="a")
})
observe({
if(!is.null(input$selectI)){
expfilter <- reactive({
vals <- exp %>% filter(Ind == input$selectI)
return(vals)
})
if(nrow(expfilter())==0){
fail <- reactive({ return("filter failed") })
output$trouble <- renderText({fail()}) # notice the use of () since fail is a function. when you want to grab the values of reactives use the ()
} else {
success <- reactive({ return("good") })
output$trouble <- renderText({success()})
P1 <- reactive({
expfilter %>% ggvis(~val1, ~val2) %>%
add_axis("x",title="Var1",title_offset=45,properties=axis_props(labels=list(fontSize = 13, fontWeight = "bold"),title=list(fontSize = 15))) %>%
add_axis("y",properties=axis_props(labels=list(fontSize = 13, fontWeight = "bold")))
})
P1 %>% bind_shiny("plot1")
}
}
})
})
用户界面
library(shiny)
shinyUI(fluidPage(
column(3,
wellPanel(
uiOutput("selectO")
)
),
column(9,
wellPanel(
ggvisOutput("plot1")),
wellPanel(h6("How Did the Filter Do"),
textOutput("trouble")
)
)
)
)