R应用程序条件计算和面板上的条件输出
我是新手。我想在我的应用程序中做的是,仅当满足另一个计算的条件时才运行和显示代码的某些部分 conditionalPanel可以很好地处理输入值上的条件,但我不知道如何处理“输出”值,即,有条件地处理函数的输出值。下面是我的示例代码:R应用程序条件计算和面板上的条件输出,r,shiny,R,Shiny,我是新手。我想在我的应用程序中做的是,仅当满足另一个计算的条件时才运行和显示代码的某些部分 conditionalPanel可以很好地处理输入值上的条件,但我不知道如何处理“输出”值,即,有条件地处理函数的输出值。下面是我的示例代码: library(shiny) msLocation <- "msLoc" searchMWText <- "searchMW" bid <- "2333333" fulltext <
library(shiny)
msLocation <- "msLoc"
searchMWText <- "searchMW"
bid <- "2333333"
fulltext <- "fullDisplay"
ui <- fluidPage(
titlePanel("Run server codes conditionally"),
sidebarLayout(
sidebarPanel(
helpText("Evaluate input and run different parts of the code depending on the output functions"),
br(),
sliderInput("rand", "select seed", min = 1, max = 50, step = 1, value = 1)
),
mainPanel(
fluidRow(conditionalPanel("output.rand == 1"),
tags$h4("Here comes the default part"),
br(),
textOutput("defaultCalc")),
fluidRow(conditionalPanel("output.randomint != 1",
tags$h4("I can evaluate if the chosen number is even or odd."),
br(),
textOutput("evenodd")
),
fluidRow(conditionalPanel("output.evenodd == 'Number is even'",
tags$h4("Number even calculation "),
textOutput("msLoc"),
br(),
textOutput("searchMW"),
br(),
textOutput("defaultID"),
br()
),
fluidRow(conditionalPanel("output.evenodd == 'Number is odd'",
tags$h4("Here is some id:", textOutput("id")),
textOutput("displayFull")
)
)
)
)
)))
#
server <- function(input, output) {
rand1 <- reactive({
if(is.null(input$rand)){return(NULL)}
rn <- input$rand
return(rn)
})
randomint <- reactive({
seedn <- rand1()
set.seed(seedn)
rint <- sample(1:50, 1)
return(rint)
})
calc1 <- reactive({
intn <- randomint()
modn <- intn %% 2
return(modn)
})
evenOdd <- reactive({
modn <- calc1()
if(modn == 0){valueText = "Number is even"}
if(modn != 0){valueText = "Number is odd"}
return(valueText)
})
idtext <- reactive({
idint <- sample(1:10000, 3)
idint <- as.character(idint)
idint <- paste(idint, collapse = "")
return(idint)
})
output$defaultCalc <- renderText({
as.character(randomint())
})
output$evenodd <- renderText({
evenOdd()
})
output$searchMW <- renderText({
searchMWText
})
output$defaultID <- renderText({
bid
})
output$id <- renderText({
idtext()
})
output$displayFull <- renderText({
fulltext
})
}
shinyApp(ui = ui, server = server)
库(闪亮)
msLocationDo:
randomint我收到一个错误,“randomint不在输出对象列表中”。@maop啊,是的,对不起。您必须执行输出$randomint,非常感谢。我消除了错误,但仍然无法获得所需的输出。我仍然有“这是一些id”,无论数字是奇数还是偶数。@maop在你的情况下缺少一个引号。
randomint <- reactive({
seedn <- rand1()
set.seed(seedn)
rint <- sample(1:50, 1)
return(rint)
})
output$randomint <- reactive(randomint())
outputOptions(output, "randomint", suspendWhenHidden = FALSE)