我正在尝试使用 R 中精彩的 Shiny 库构建一个应用程序,我希望它为用户生成一些错误和状态消息。为了使其正常工作,我将条件面板与输出对象上的一些布尔标志结合使用,以呈现错误和状态消息的面板。根据文档,这个策略应该对我有用,但事实并非如此。
我将这个想法归结为一个简单的用户界面和服务器脚本,本质上我想做的是:
ui.R
library("shiny")
shinyUI(pageWithSidebar(
headerPanel('Hey There Guys!'),
sidebarPanel(
h4('Switch the message on and off!'),
actionButton('switch', 'Switch')
),
mainPanel(
conditionalPanel(condition = 'output.DISP_MESSAGE',
verbatimTextOutput('msg')
)
)
))
Run Code Online (Sandbox Code Playgroud)
服务器R
library('shiny')
shinyServer(function(input, output) {
output$DISP_MESSAGE <- reactive({input$switch %% 2 == 0})
output$msg <- renderPrint({print("Hey Ho! Let's Go!")})
})
Run Code Online (Sandbox Code Playgroud)
这里的想法是,按下按钮应该切换消息嘿嗬!我们走吧!开启和关闭。使用发布的代码,这是行不通的。在 Chrome 中加载页面时不会显示该消息,并且按下按钮不会执行任何操作。我有来自 CRAN 的最新版本的 Shiny。任何帮助将不胜感激!
这是实现相同效果的一种方法,通过而checkboxInput
不是操作按钮。您可以使用它作为启动代码来让它执行您想要的操作。
library("shiny")
shinyUI(pageWithSidebar(
headerPanel('Hey There Guys!'),
sidebarPanel(
h4('Switch the message on and off!'),
checkboxInput(inputId = "opt_switch", label = "Toggle Message", value = FALSE)
),
mainPanel(
conditionalPanel(condition = 'opt_switch',
verbatimTextOutput('msg')
)
)
))
Run Code Online (Sandbox Code Playgroud)
library('shiny')
shinyServer(function(input, output) {
output$msg <- renderText({
if(input$opt_switch == TRUE) {
("Hey Ho! Let's Go!")
}
})
})
Run Code Online (Sandbox Code Playgroud)
希望有帮助。