簡體   English   中英

在Shiny中顯示和隱藏數據框

[英]Display and Hide dataframe in Shiny

# ui.R

shinyUI(
 fluidPage(
    titlePanel("Make a data frame and display"),
    fluidRow(column(4,selectInput('types', 'Select the type',c("Basketball","Soccer")))),
    uiOutput('typeChange'),
    mainPanel(dataTableOutput('displayDf'))
 )
)

我正在嘗試接受用戶輸入並將數據存儲到數據框中。 然后,當用戶繼續添加記錄時,我想同時顯示數據框。 上面的代碼使用戶可以在籃球足球之間進行選擇

# server.R
# Create a function to initiate and clear global variables

clear_all <- function(){
z <<- c()
z1 <<- c()
z2 <<- c()
z3 <<- c()
}
clear_all()

shinyServer(function(input, output, session) {

# make additional ui depending on the sport selected.
output$typeChange <- renderUI({
    if (input$types=="Basketball"){
        clear_all()
        fluidRow(
        column(4,textInput("v1","Player name")),
        column(4,selectInput("v2", "Age:",c("20","25","30"))),
        column(4,actionButton("add", "Add the above player")))
    }else if (input$types=="Soccer"){
        clear_all()
        fluidRow(
        column(4,textInput("v1","Player name")),
        column(4,selectInput("v2", "Age",c("20","25","30"))),
        column(4,selectInput("v3", "Income:",c("100","125","150"))),
        column(4,actionButton("add", "Add the above player")))
    }else{}
})

# if a user adds a player, update the dataframe and display
observeEvent(input$add, {
    if (input$types=="Basketball"){
        z1 <<- c(z1,input$v1)
        z2 <<- c(z2,input$v2)
        z<<- data.frame(Player=z1,Age=z2)
    }else if (input$types=="Soccer"){
        z1 <<- c(z1,input$v1)
        z2 <<- c(z2,input$v2)
        z3 <<- c(z3,input$v3)
        z<<- data.frame(Player=z1,Age=z2,Income=z3)
    }

    output$displayDf=renderDataTable(z)
})
})

服務器端代碼根據選擇的運動項目創建新文本和selectinput。 我嘗試創建的功能是,在選擇一項運動之后,用戶可以輸入其他輸入並添加播放器。 只要用戶未選擇其他運動,就應顯示用戶添加的球員列表。 一旦用戶更改了一項運動,該數據框將為空,並且不應顯示前一項運動的任何內容。

我面臨的問題是,如果我在籃球中添加3名球員,然后將我的最佳選擇更改為足球,那么在我添加一名足球球員之前,我仍然會看到籃球數據。 我希望所顯示的數據框在我更改這項運動后立即隱藏。 還附有相同的屏幕截圖。請告知。 在籃球中添加3個球員之后

改變運動為足球

如果你把數據表中的無功輸出,這樣它總是顯示你的問題是可以解決input$type說,那就是:

  1. 使反應性值的列表, tables ,其具有長度為2並且存儲表,用於分別籃球和足球,。
  2. 根據input$type的內容顯示一個或另一個。

我在解決此問題時整理了您的應用程序(1.不要使用重復的輸入ID,2。使用全局變量並不像使用反應堆那樣干凈,3。使用conditionPanels):

library(shiny)
library(plotly)
library(shinyjs)

ui <- shinyUI(
  fluidPage(
    useShinyjs(),
    titlePanel("Make a data frame and display"),
    fluidRow(column(4,selectInput('types', 'Select the type',c("Basketball","Soccer")))),
    fluidRow(column(width=8,
                    column(4, textInput("v1","Player name")),
                    column(4, selectInput("v2", "Age:",c("20","25","30"))),
                    conditionalPanel(condition="input.types == 'Soccer'",
                                     column(4, selectInput("v3", "Income:",c("100","125","150"))))),
                    column(1, actionButton("add", "Add the above player"))),
    mainPanel(dataTableOutput('displayDf'))
  )
)


server <- shinyServer(function(input, output, session) {

  tables <- reactiveValues(basketball = data.frame(Player = character(), Age = numeric()),
                           soccer     = data.frame(Player = character(), Age = numeric(), Income = double()))

# if a user adds a player, update the dataframe and display
  observeEvent(input$add, {
    if (input$types=="Basketball"){
      oldVals <- tables$basketball
      tables$basketball <- rbind(oldVals, data.frame(Player = input$v1, Age = input$v2))
    }else if (input$types=="Soccer"){
      oldVals <- tables$soccer
      tables$soccer <- rbind(oldVals, data.frame(Player = input$v1, Age = input$v2, Income = input$v3))
    }

    # reset the input values
    reset("v1")
    reset("v2")
    reset("v3")
  })

  # display the appropriate table
  output$displayDf <- renderDataTable({
    if (input$types=="Basketball") tables$basketball else tables$soccer
  })
})

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM