我正在尝试使用这些功能创建一个简单的应用程序:
用户将选择"自定义"或"默认"。
如果"默认"如果选中,将显示默认文本。但如果" Custom"如果被选中,用户将能够提交"提交"将显示文本和提交的文本。
我已经编写了一些代码来执行此操作,但无法在" Custom"中同时处理这两个反应式表达式。情况下。
以下是代码:
library( shiny )
ui = navbarPage(
title = 'Text',
tabPanel(
'Custom App',
fluidRow(
radioButtons( 'radio', 'Select Action', choices = list( 'Default' = 'Default', 'Custom' = 'Custom' ),
selected = 'Default', inline = T ),
column( 6, textInput( 'text_given', label = 'Enter Text', value = '' ) ),
actionButton( 'submit_button', 'Submit' ),
textOutput( text )
)
)
)
server = function( input, output, session ){
main = reactive({
selected_action = input$radio
if( selected_action == 'Default' ){
text_to_show = 'Default Text'
} else{
dataInput = eventReactive( input$submit_button,{
text_to_show = input$text_given
})
}
return( list( 'text_output' = text_to_show ) )
})
output$text = renderText( main()$text_output )
}
shinyApp( ui = ui, server = server )
答案 0 :(得分:3)
无需嵌套您的被动物。您应该在反应之外创建您的reactiveExpression,并从您的被动反应中引用该对象。我相信这会做你想要的,如果这有帮助,请告诉我!
library( shiny )
ui = navbarPage(
title = 'Text',
tabPanel(
'Custom App',
fluidRow(
radioButtons( 'radio', 'Select Action', choices = list( 'Default' = 'Default', 'Custom' = 'Custom' ),
selected = 'Default', inline = T ),
column( 6, textInput( 'text_given', label = 'Enter Text', value = '' ) ),
actionButton( 'submit_button', 'Submit' ),
textOutput( "text")
)
)
)
server = function( input, output, session ){
submitted_text <- eventReactive(input$submit_button,
{
input$text_given
})
main = reactive({
selected_action = input$radio
if(selected_action == 'Default' ){
text_to_show = 'Default Text'
}
else
{
text_to_show = submitted_text()
}
return(text_to_show)
})
output$text = renderText( main() )
}
shinyApp( ui = ui, server = server )
编辑:基于评论
library( shiny )
library(shinyjs)
ui = navbarPage(
title = 'Text',
tabPanel(
'Custom App',
useShinyjs(),
fluidRow(
radioButtons( 'radio', 'Select Action', choices = list( 'Default' = 'Default', 'Custom' = 'Custom' ),
selected = 'Default', inline = T ),
column( 6, shinyjs::hidden(textInput( 'text_given', label = 'Enter Text', value = '' ) )),
shinyjs::hidden(actionButton( 'submit_button', 'Submit' )),
textOutput( "text")
)
)
)
server = function( input, output, session ){
submitted_text <- eventReactive(input$submit_button,
{
input$text_given
})
observeEvent(input$radio,
{
if(input$radio=='Default')
{
hide("text_given")
hide("submit_button")
}
else
{
show("text_given")
show("submit_button")
}
})
main = reactive({
selected_action = input$radio
if(selected_action == 'Default' ){
text_to_show = 'Default Text'
}
else
{
text_to_show = submitted_text()
}
return(text_to_show)
})
output$text = renderText( main() )
}
shinyApp( ui = ui, server = server )