gpt4 book ai didi

r 基于选择的 Shiny 用户输入

转载 作者:行者123 更新时间:2023-12-05 06:44:26 24 4
gpt4 key购买 nike

我知道如何在 UI 上添加文本输入或单选按钮或日期输入,我想做的是询问用户是想输入文本还是日期范围?根据用户选择显示文本输入或日期输入。不确定如何执行此操作。

       Pseudo Code

1) Please choose if you want to enter text or date range.
Radio Button 1 - Text Input
Radio Button 2 - Date Range

2a) If the user chooses Radio Button 1, then Text Input should be displayed on the main panel, option to enter two dates (From & to) should not be displayed

2b) If the user chooses Ratio Button 2, then the option to enter two dates (From & to) should be displayed on the Main panel and text input should not be displayed.

不知道该怎么做。需要一些指示。

最佳答案

我在这里感觉很慷慨,通常你应该提供你尝试过的代码,但这里是一个如何有条件输入的工作示例。

library(shiny)

runApp(
list(
ui = pageWithSidebar(
headerPanel("Option Input via Radio Buttons"),
sidebarPanel(
radioButtons("radio", label = h3("Radio buttons"),
choices = list("Date" = "date", "Text" = "text"),
selected = 1),
uiOutput("textORdate")
),
mainPanel()
),
server = function(input, output){
output$textORdate <- renderUI({
validate(
need(!is.null(input$radio), "please select a input type")
)
if(input$radio == "text"){
textInput("mytext", "Text Input", "please enter text")
}else{
dateRangeInput("daterange", "Date range:",
start = "2012-01-01",
end = "2015-03-06")
}
})
}))

我在这里展示了多个概念。首先,您可以通过在 server.R 部分中创建输入来创建动态输入。然后由 uiOutput 显示。其次,我总是喜欢向人们介绍validate。这是帮助排除故障或向用户提供有用的错误消息的重要功能。

关于r 基于选择的 Shiny 用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28868305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com