# 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)
})
})
我正在尝试获取用户输入并将数据存储到数据框中。然后我想在用户继续添加记录时同时显示数据框。上面的代码允许用户在篮球和足球
之间进行选择{{1}}
服务器端代码根据选择的运动创建新文本和选择输入。我试图创建的功能是,在选择运动后,用户可以输入其他输入并添加播放器。只要用户没有选择不同的运动,就应该显示用户添加的球员列表。一旦用户更改了运动,数据框将为空,并且不会显示上一项运动中的任何内容。
我面临的问题是,如果我在篮球中添加3名球员然后将我的最佳选择更改为足球,我仍然会看到篮球数据,直到我添加一名足球运动员。我希望一旦我改变运动就隐藏显示的数据框。另外附上相同的屏幕截图。请注意。
答案 0 :(得分:1)
如果您将数据表置于反应输出中,则可以解决您的问题,以便它始终显示input$type
所说的内容,即:
tables
,其长度为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)