我正在尝试使用 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')
)
)
))
server.R
library('shiny')
shinyServer(function(input, output) {
output$DISP_MESSAGE <- reactive({input$switch %% 2 == 0})
output$msg <- renderPrint({print("Hey Ho! Let's Go!")})
})
这里的想法是按下按钮应该切换消息嘿嘿!我们走吧!开启和关闭。使用发布的代码,这是行不通的。在 Chrome 中加载页面时不会显示该消息,并且按下按钮不会执行任何操作。我有来自 CRAN 的最新版本的 Shiny。任何帮助将不胜感激!
这是实现相同效果的一种方法,通过checkboxInput
与操作按钮相反。您可以使用它作为启动代码来让它执行您想要的操作。
UI.R
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')
)
)
))
Server.R
library('shiny')
shinyServer(function(input, output) {
output$msg <- renderText({
if(input$opt_switch == TRUE) {
("Hey Ho! Let's Go!")
}
})
})
希望有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)