我有一个功能强大的闪亮应用程序,其逻辑描述如下:
应用的逻辑:
用户通过使用selectInput()“ Label”选择测试之一。这是主要操作,然后他可以修改其名称,例如将Test 1更改为TestA。然后,用户可以通过numericInput()“ Tests中的项目”在Test中添加项目。这些是总项目。正如您将看到的,“测试中的项目”的数目与hot3表中“测试”中“可用”列的数目相同。通过“选择项目”,他可以选择要在hot5表中显示的特定项目。然后,用户可以单击hot5表以选择特定项目,并且针对该特定测试,hot3表中“ Sel”列下的选定项目(或行)数将显示。 “选择的项目”仅显示在“选择项目”中选择的项目数。请注意,对该表进行的每次修改都不依赖于其他小部件。例如,这意味着不必更改标签名称。
问题:
问题在于,除非更改“标签”名称,否则现在不更新热点3表(左侧),因为这是其他功能的可选功能,因此不应该这样。例如,我可以选择“测试1”,但是我不想更改其名称。然后,如果选择提交按钮,则左侧的表不会更新。如果我将其重命名为其他名称(甚至是“测试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
}
# if(!is.null(input$hot5_rows_selected) && input$hot5_rows_selected!=""){
#
# }
})
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)
请查看是否适合您。
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)