我有一个功能强大的闪亮应用程序,其逻辑描述如下:
应用的逻辑:
用户通过使用selectInput()“ Label”选择测试之一。这是主要操作,然后他可以修改其名称,例如将Test 1更改为TestA。然后,用户可以通过numericInput()“ Tests中的项目”在Test中添加项目。这些是总项目。正如您将看到的,“测试中的项目”的数目与hot3表中“测试”中“可用”列的数目相同。通过“选择项目”,他可以选择要在hot5表中显示的特定项目。然后,用户可以单击hot5表以选择特定项目,并且针对该特定测试,hot3表中“ Sel”列下的选定项目(或行)数将显示。 “选择的项目”仅显示在“选择项目”中选择的项目数。请注意,对该表进行的每次修改都不依赖于其他小部件。例如,这意味着不必更改标签名称。
问题:
请查看所附的屏幕截图。我将“测试2”的标签更改为“测试B”,添加了4个项目,但未通过“在行上单击”功能来选择它们。然后,我按下提交按钮,我意识到在此之后,“标签”的设置将更改回测试1,但是测试B /测试2的所有设置都保留在我的应用程序中。这样就好像我对测试1的设置一样。我希望能够在按下“提交”按钮或选择另一个测试(“标签”)之后显示的设置是您在“标签”下选择的测试的设置。如果您按下某种复位键,则使其与该测试的实际设置相对应。
应用程序:
library(shiny)
library(DT)
library(rhandsontable)
#library(tidyverse)
ui <- navbarPage(
"Application",
tabPanel("Booklets",
sidebarLayout(
sidebarPanel(
uiOutput("tex2"),
rHandsontableOutput("hot3")
),
mainPanel(
fluidRow(
wellPanel(
fluidRow(
column(4,
DT::dataTableOutput("hot5")
),
column(4,
fluidRow(
uiOutput("book3"),
uiOutput("book6")
),
fluidRow(
uiOutput("book1"),
uiOutput("book10"),
uiOutput("book11")
),
fluidRow(actionButton("submit","submit"))
)
))
)
)
)
)
)
#server
server <- function(input, output, session) {
rv<-reactiveValues()
output$tex2<-renderUI({
numericInput("text2", "#tests", value = 1, min=1)
})
output$book1<-renderUI({
numericInput("bk1",
"Items in test",
value = 1,
min = 1)
})
output$book3<-renderUI({
selectInput("bk3",
"Label",
choices=(paste("Test",1:input$text2)))
})
output$book6<-renderUI({
textInput("bk6", "Change to",
value=NULL
)
})
output$book10<-renderUI({
# changed from selectize
selectizeInput(
"bk10", "Select Items", choices =1:10000,multiple =T,selected = 1,
options = list(maxItems = input$bk1))#changed from
})
output$book11<-renderUI({
textInput("bk11", "Items chosen",
value = nrow(rt5())
)
})
#rt4<-reactive({
observe({
req(input$text2)
rv$rt4 = data.frame(
SNo = rep(TRUE, input$text2),
Test=paste(1:input$text2),
Label=paste("Test",1:input$text2),
Avail=1L,
Sel =as.integer(rep.int(0,input$text2)),
stringsAsFactors = FALSE)
})
observeEvent(input$submit,{
# rt4 <- reactive({
if (is.null( rv$rt4))
return(NULL)
if(!is.null(input$bk6) && input$bk6!=""){
rv$rt4[ rv$rt4$Label==input$bk3, "Avail"] <- input$bk1
rv$rt4[ rv$rt4$Label==(input$bk3), "Sel"] <- length(input$hot5_rows_selected)
rv$rt4[ rv$rt4$Label==input$bk3, "Label"] <- input$bk6
}
else
{
rv$rt4[ rv$rt4$Label==input$bk3, "Avail"] <- input$bk1
rv$rt4[ rv$rt4$Label==(input$bk3), "Sel"] <- length(input$hot5_rows_selected)
#rv$rt4[ rv$rt4$Label==input$bk3, "Label"] <- input$bk6
}
})
observeEvent(input$submit,{
updateSelectInput(session,"bk3","Label", choices=rv$rt4$Label)
}
)
rt55<-reactive({
DF=data.frame(
Id= input$bk10,
Label=paste("Item",input$bk10),
Pf=0,
stringsAsFactors = FALSE
)
})
rt5<-reactive({
DF=data.frame(
Id= input$bk10,
Label=paste("Item",input$bk10),
Pf=0,
stringsAsFactors = FALSE
)
cbind(id=rowSelected(), DF)
})
rowSelected <- reactive({
x <- numeric(nrow(rt55()))
x[input$hot5_rows_selected] <- 1
x
})
output$hot5 <- renderDT(datatable(rt5()[,-1],
selection = list(mode = "multiple",
selected = (1:nrow(rt5()[,-1]))[as.logical(rowSelected())],
target = "row"),rownames = F)
)
output$hot3 <-renderRHandsontable({
req(input$text2)
rhandsontable(rv$rt4)
})
}
shinyApp(ui,server)
答案 0 :(得分:1)
尝试一下。在测试期间,我偶然在ui中定义了bk6,但是您可以使用renderUI公式,它仍然可以工作。此外,您可以将两个watchEvent块组合为一个。
library(shiny)
library(DT)
library(rhandsontable)
#library(tidyverse)
ui <- navbarPage(
"Application",
tabPanel("Booklets",
sidebarLayout(
sidebarPanel(
uiOutput("tex2"),
rHandsontableOutput("hot3")
),
mainPanel(
fluidRow(
wellPanel(
fluidRow(
column(4,
DT::dataTableOutput("hot5")
),
column(4,
fluidRow(
uiOutput("book3"),
textInput("bk6", "Change to",value="")
),
fluidRow(
uiOutput("book1"),
uiOutput("book10"),
uiOutput("book11")
),
fluidRow(actionButton("submit","submit"))
)
))
)
)
)
)
)
#server
server <- function(input, output, session) {
rv<-reactiveValues()
output$tex2<-renderUI({
numericInput("text2", "#tests", value = 1, min=1)
})
output$book1<-renderUI({
numericInput("bk1",
"Items in test",
value = 1,
min = 1)
})
output$book3<-renderUI({
selectInput("bk3",
"Label",
choices=(paste("Test",1:input$text2)),
selected = rv$selected)
})
observeEvent(input$submit,{
if(!is.null(input$bk6) && input$bk6!=""){
rv$selected <- input$bk6
}
else
rv$selected <- input$bk3
}
)
# output$book6<-renderUI({
# textInput("bk6", "Change to",
# value=""
# )
# })
output$book10<-renderUI({
# changed from selectize
selectizeInput(
"bk10", "Select Items", choices =1:10000,multiple =T,selected = 1,
options = list(maxItems = input$bk1))#changed from
})
output$book11<-renderUI({
textInput("bk11", "Items chosen",
value = nrow(rt5())
)
})
#rt4<-reactive({
observe({
req(input$text2)
rv$rt4 = data.frame(
SNo = rep(TRUE, input$text2),
Test=paste(1:input$text2),
Label=paste("Test",1:input$text2),
Avail=1L,
Sel =as.integer(rep.int(0,input$text2)),
stringsAsFactors = FALSE)
})
observeEvent(input$submit,{
# rt4 <- reactive({
if (is.null( rv$rt4))
return(NULL)
if(!is.null(input$bk6) && input$bk6!=""){
rv$rt4[ rv$rt4$Label==input$bk3, "Avail"] <- input$bk1
rv$rt4[ rv$rt4$Label==(input$bk3), "Sel"] <- length(input$hot5_rows_selected)
rv$rt4[ rv$rt4$Label==input$bk3, "Label"] <- input$bk6
}
else
{
rv$rt4[ rv$rt4$Label==input$bk3, "Avail"] <- input$bk1
rv$rt4[ rv$rt4$Label==(input$bk3), "Sel"] <- length(input$hot5_rows_selected)
#rv$rt4[ rv$rt4$Label==input$bk3, "Label"] <- input$bk6
}
})
observeEvent(input$submit,{
updateSelectInput(session,"bk3","Label", choices=rv$rt4$Label,
selected = rv$selected)
updateTextInput(session, "bk6", value = "")
print(rv$selected)
}
)
rt55<-reactive({
DF=data.frame(
Id= input$bk10,
Label=paste("Item",input$bk10),
Pf=0,
stringsAsFactors = FALSE
)
})
rt5<-reactive({
DF=data.frame(
Id= input$bk10,
Label=paste("Item",input$bk10),
Pf=0,
stringsAsFactors = FALSE
)
cbind(id=rowSelected(), DF)
})
rowSelected <- reactive({
x <- numeric(nrow(rt55()))
x[input$hot5_rows_selected] <- 1
x
})
output$hot5 <- renderDT(datatable(rt5()[,-1],
selection = list(mode = "multiple",
selected = (1:nrow(rt5()[,-1]))[as.logical(rowSelected())],
target = "row"),rownames = F)
)
output$hot3 <-renderRHandsontable({
req(input$text2)
rhandsontable(rv$rt4)
})
}
shinyApp(ui,server)