在 R shiny 中,如何向操作按钮 "Add" 添加功能?
In R shiny, how do I add functionality to the action button "Add"?
我正在开发 R shiny 应用程序。我已经在 Shiny 中创建了 Tab1 和 Tab2,我试图做的是在我填写以下表格并点击“添加”按钮后使数据表在“Tab2”中可见。
总结
当用户在Tab1中输入“SchoolId, UserId, class, result”的信息并点击“Add”按钮时,应该会出现“Tab2”中的Datatable。
此外,一旦用户按下“添加”按钮,“SchoolId”、“UserId”等字段应为空,以供用户填写“Tab2”中第二行的数据."
问题:
我确定我在这里遗漏了一些东西来为“添加”按钮建立服务器级连接,因为我无法填写 Tab2 中第二行的详细信息,因为“添加”按钮是不工作。
有人可以帮助解决这个问题吗?
library(shiny)
library(shinyBS)
library(stringr)
library(Hmisc)
library(rhandsontable)
library(shinydashboard)
library(DT)
library(V8)
ui <- fluidPage(
fluidRow(tabsetPanel(id='tabs'
, tabPanel("Tab1", uiOutput("tabP1"))
, tabPanel("Tab2", uiOutput("tabP2"))
)
)
)
server <- function(input, output, session) {
output$tabP1 <- renderUI({
tabPanel("Tap1"
, div(id = "form"
, textInput("schoolId *", label="SchoolId" )
, selectInput("userId", label="UserId"
, choices = list("UserA" = "UserA", "UserB" = "UserB",
"UserC" = "UserC")
, selected = "UserA")
, textInput("class", value = NA, label = "class")
, selectInput("result", label="result"
, choices = list("PASS" = "PASS", "FAIL" ="FAIL" ), selected = "")
)
, actionButton("add", "Add")
)
})
# -------------------- Tab2 ----------------------------------
observeEvent(input$add,{
req(input$schoolId,input$userId,input$class,input$result)
tmp<-data.frame(SCHOOLID=input$schoolId, USERID=input$userId
, CLASS= input$class
, RESULT=input$result
)
df <- rbind(df,tmp)
})
output$DT2 <- DT::renderDataTable({
df
})
output$tabP2 <- renderUI({
tabPanel("View"
, DT::dataTableOutput("DT2")
, hr()
, hidden(downloadButton('downloadData', 'Download'))
)
})
}
shinyApp(ui, server)
您想对可以反复更改的值使用 reactive
或 reactiveValues
之类的东西。要在单击按钮之前隐藏 table,请根据按钮的输出使用 conditionalPanel
(它会显示单击了多少次)。
我已经在你下面的代码中实现了这些东西:
编辑:同时回复您的第一条评论
library(shiny)
library(stringr)
library(shinydashboard)
library(tidyverse)
library(DT)
ui <- fluidPage(
fluidRow(tabsetPanel(id='tabs',
tabPanel("Tab1",
div(id = "form",
textInput("schoolId", label="SchoolId *" ),
selectInput("userId", label="UserId", choices = c("UserA", "UserB", "UserC"),selected = "UserA"),
textInput("class", label = "class"),
selectInput("result", label="result", choices = c("PASS", "FAIL" ))
),
actionButton("add", "Add")
),
tabPanel("Tab2",
tabPanel("View",
conditionalPanel("input.add != 0",
DTOutput("DT2"), hr(), downloadButton('downloadData', 'Download'))
)
)
)
)
)
server <- function(input, output, session) {
store <- reactiveValues()
observeEvent(input$add,{
new_entry <- data.frame(SCHOOLID=input$schoolId, USERID=input$userId
, CLASS= input$class
, RESULT=input$result)
if("value" %in% names(store)){
store$value<-bind_rows(store$value, new_entry)
} else {
store$value<-new_entry
}
# If you want to reset the field values after each entry use the following two lines
for(textInputId in c("schoolId", "class")) updateTextInput(session, textInputId, value = "")
updateSelectInput(session, "userId", selected = "UserA")
updateSelectInput(session, "result", selected = "PASS")
})
output$DT2 <- renderDT({
store$value
})
}
shinyApp(ui, server)
我正在开发 R shiny 应用程序。我已经在 Shiny 中创建了 Tab1 和 Tab2,我试图做的是在我填写以下表格并点击“添加”按钮后使数据表在“Tab2”中可见。
总结
当用户在Tab1中输入“SchoolId, UserId, class, result”的信息并点击“Add”按钮时,应该会出现“Tab2”中的Datatable。
此外,一旦用户按下“添加”按钮,“SchoolId”、“UserId”等字段应为空,以供用户填写“Tab2”中第二行的数据."
问题:
我确定我在这里遗漏了一些东西来为“添加”按钮建立服务器级连接,因为我无法填写 Tab2 中第二行的详细信息,因为“添加”按钮是不工作。
有人可以帮助解决这个问题吗?
library(shiny)
library(shinyBS)
library(stringr)
library(Hmisc)
library(rhandsontable)
library(shinydashboard)
library(DT)
library(V8)
ui <- fluidPage(
fluidRow(tabsetPanel(id='tabs'
, tabPanel("Tab1", uiOutput("tabP1"))
, tabPanel("Tab2", uiOutput("tabP2"))
)
)
)
server <- function(input, output, session) {
output$tabP1 <- renderUI({
tabPanel("Tap1"
, div(id = "form"
, textInput("schoolId *", label="SchoolId" )
, selectInput("userId", label="UserId"
, choices = list("UserA" = "UserA", "UserB" = "UserB",
"UserC" = "UserC")
, selected = "UserA")
, textInput("class", value = NA, label = "class")
, selectInput("result", label="result"
, choices = list("PASS" = "PASS", "FAIL" ="FAIL" ), selected = "")
)
, actionButton("add", "Add")
)
})
# -------------------- Tab2 ----------------------------------
observeEvent(input$add,{
req(input$schoolId,input$userId,input$class,input$result)
tmp<-data.frame(SCHOOLID=input$schoolId, USERID=input$userId
, CLASS= input$class
, RESULT=input$result
)
df <- rbind(df,tmp)
})
output$DT2 <- DT::renderDataTable({
df
})
output$tabP2 <- renderUI({
tabPanel("View"
, DT::dataTableOutput("DT2")
, hr()
, hidden(downloadButton('downloadData', 'Download'))
)
})
}
shinyApp(ui, server)
您想对可以反复更改的值使用 reactive
或 reactiveValues
之类的东西。要在单击按钮之前隐藏 table,请根据按钮的输出使用 conditionalPanel
(它会显示单击了多少次)。
我已经在你下面的代码中实现了这些东西:
编辑:同时回复您的第一条评论
library(shiny)
library(stringr)
library(shinydashboard)
library(tidyverse)
library(DT)
ui <- fluidPage(
fluidRow(tabsetPanel(id='tabs',
tabPanel("Tab1",
div(id = "form",
textInput("schoolId", label="SchoolId *" ),
selectInput("userId", label="UserId", choices = c("UserA", "UserB", "UserC"),selected = "UserA"),
textInput("class", label = "class"),
selectInput("result", label="result", choices = c("PASS", "FAIL" ))
),
actionButton("add", "Add")
),
tabPanel("Tab2",
tabPanel("View",
conditionalPanel("input.add != 0",
DTOutput("DT2"), hr(), downloadButton('downloadData', 'Download'))
)
)
)
)
)
server <- function(input, output, session) {
store <- reactiveValues()
observeEvent(input$add,{
new_entry <- data.frame(SCHOOLID=input$schoolId, USERID=input$userId
, CLASS= input$class
, RESULT=input$result)
if("value" %in% names(store)){
store$value<-bind_rows(store$value, new_entry)
} else {
store$value<-new_entry
}
# If you want to reset the field values after each entry use the following two lines
for(textInputId in c("schoolId", "class")) updateTextInput(session, textInputId, value = "")
updateSelectInput(session, "userId", selected = "UserA")
updateSelectInput(session, "result", selected = "PASS")
})
output$DT2 <- renderDT({
store$value
})
}
shinyApp(ui, server)