r Shiny使textInput以先前的selectInput为条件

问题描述 投票:0回答:3

您好,对不起可能是一个基本的闪亮问题。

在我的迷你闪亮应用程序中,我希望用户:

  1. 使用selectInput()从预先存在的名称列表中选择一个名称。
  2. 只有当他/她的名字不在列表中时,我才会想要一个新的小部件出现在他/她应该使用textInput()输入名称的位置。

我被卡住了 - 我在UI侧的mainPanel内的verbatimTextOutput语句无效。

谢谢你的提示!

library(shiny)

ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      uiOutput("chosen_name", label = h4("Select one of the names:"))
    ),
    mainPanel(                       # Just shows what was selected
      # Next two lines don't work if uncommented:
      #  verbatimTextOutput('chosen_name')
      #  verbatimTextOutput("name_openend")
    )
  )
))

server = shinyServer(function(input, output, session) {

  # A vector of pre-existing names:
  mynames <- c("John", "Mary", "Jim")

  # Allow to select a name from the list of pre-existing names:
  output$chosen_name <- renderUI({
    selectInput('chosen_name',"Select a name:",
                choices = c("Name not on our list", mynames),
                selected = "Name not on our list")
  })

  # Open end box to enter name - if the name the user wants to enter is not on the list:
  output$name_openend <- renderUI({
    if (!output$chosen_name == 'Name not on our list') return(NULL) else {
      textInput("If the name you want is not on our list, type it here:")
    }
  })

})


shinyApp(ui = ui, server = server)
r shiny textinput selectinput
3个回答
1
投票

更新的代码

library(shiny)

ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      selectInput("chosen_name", "select name", choices = ""),
      uiOutput("new")
    ),
    mainPanel(
      textOutput("chosen")
    )
  )
))

server = shinyServer(function(input, output, session) {

  # A vector of pre-existing names:
  mynames <- c("John", "Mary", "Jim")

  observe({
    updateSelectInput(session, inputId = "chosen_name", label = "Select a name:", choices = c(mynames, "Name not on our list"))
  })

  # Open end box to enter name - if the name the user wants to enter is not on the list:
  output$new <- renderUI({
    if (!input$chosen_name == 'Name not on our list') return(NULL) else {
      textInput("Not_on_list", "If the name you want is not on our list, type it here:")
    }
  })
  #
  # 
  # Allow to select a name from the list of pre-existing names:

  output$chosen <- renderText({
    if (!input$chosen_name == 'Name not on our list') {
    return(paste("Chosen name:", input$chosen_name))
    }
    else {
      return(paste("Chosen name:", input$Not_on_list))
    }
  })

})

shinyApp(ui = ui, server = server)

1
投票

你对ui方面的功能感到困惑:如果你在服务器端使用renderUI(),你将不得不在ui端使用uiOutput()来使它工作。此外,您应该避免使用相同的ID两次。最后,对于textinput,我添加了一个id和一个标签。

完整代码如下:

library(shiny)

ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      uiOutput("chosen_nm", label = h4("Select one of the names:"))
    ),
    mainPanel(                       # Just shows what was selected
      # Next two lines don't work if uncommented:
      uiOutput("name_openend")
    )
  )
))

server = shinyServer(function(input, output, session) {

  # A vector of pre-existing names:
  mynames <- c("John", "Mary", "Jim")

  # Allow to select a name from the list of pre-existing names:
  output$chosen_nm <- renderUI({
    selectInput('chosen_name',"Select a name:",
                choices = c("Name not on our list", mynames),
                selected = "Name not on our list")
  })

  output$chosen_name2 <- renderText({
    paste("The chosen name is: ", input$chosen_name)
  })

  # Open end box to enter name - if the name the user wants to enter is not on the list:
  output$name_openend <- renderUI({
    req(input$chosen_name)
    if (input$chosen_name == 'Name not on our list'){
      textInput("newName", "If the name you want is not on our list, type it here:")
    }else{
      verbatimTextOutput('chosen_name2')
    }
  })

})


shinyApp(ui = ui, server = server)

0
投票

有一些事情我需要削减才能使其发挥作用。首先是sidebarLayout。这可能是你的主面板搞乱了。我相信你可以让它工作只是确保mainPanel在正确的位置,而不是添加到sidebarLayout,这可能是问题。

还有一些其他问题,如renderUI和UIOutput。也许其他人会添加关于如何使用这些功能的帖子。另外,我添加了一个反应函数,因此textOutput会改变。

看看我的工作方式。


mynames <- c("John", "Mary", "Jim")
ui = shinyUI(
  fluidPage(

    sidebarPanel(
      selectInput('chosen_name',"Select a name:",
                  choices = c("Name not on our list", mynames),
                  selected = "Name not on our list")
    ),
    mainPanel(   
      textOutput('chosen_name2')

    )
  )
)

server = shinyServer(function(input, output, session) {

  # A vector of pre-existing names:
  selectedchosen_name <- reactive({
    if (input$chosen_name == 'Name not on our list') {return("Pick a name")} else {
      return("If the name you want is not on our list, type it here:")
    }
  })

  # Allow to select a name from the list of pre-existing names:
  output$chosen_name2 <- renderText({
    selectedchosen_name()
  })

  # Open end box to enter name - if the name the user wants to enter is not on the list:


})

shinyApp(ui = ui, server = server)

我删除了一些可能是多余的部分,但是现在它已经简化了,所以你可以添加它。如果你有问题,就问吧。

© www.soinside.com 2019 - 2024. All rights reserved.