问题描述
现在,输入框显示在另一个下方。我希望能够以矩阵形式显示它们,以便有 input$numoc
行和 input$inp1
列。
界面
numericInput("cols","Enter number of columns:",min=1,1),numericInput("rows","Enter number of rows:",1)
服务器
output$matrixx <- renderUI({
k= rep(c(1:input$rows),times = input$cols)
mx<-max(input$rows,input$cols)
lllist <- lapply(1:(input$cols*input$rows),function(i,j=((i-1)%/%input$rows)+1,y=k[[i]]) {
iids <- paste("inp2",i,sep="")
names<- paste("Treatment #",j," Outcome #",y,sep="")
list(
numericInput(iids,names,value=0,min=0,max=1,step=0.05)
)
})
do.call(tagList,unlist(lllist,recursive = FALSE))
})
解决方法
当然,我们可以在 def online_count(statuses):
count = 0
for x in statuses.items():
if x =='online':
count += 1
return count
statuses = {
"Alice": "online","Bob": "offline","Eve": "online",}
print(online_count(statuses))
中添加 fluidRow
和 column
来实现这一点。
这是一个最小的例子:
renderUI
为简单起见,我将其限制为 4 列,并以更重复但更易于阅读的格式编写。以额外的复杂性为代价,您可以让它处理更多的列,并通过使用额外的循环来缩短它。
,这是一种方法,用两个 lapply
语句划分行和列。您可以添加 column
并根据元素数量使用静态值或动态设置 width
。
我还添加了 verbatimTextOutput
以显示在数字输入中输入的值作为演示。
library(shiny)
ui <- fluidPage(
numericInput("cols","Enter number of columns:",min = 1,1),numericInput("rows","Enter number of rows:",verbatimTextOutput("values"),uiOutput("matrixx")
)
server <- function(input,output,session) {
output$matrixx <- renderUI({
lapply(seq(input$cols),function(i) {
column(width = 4,lapply(seq(input$rows),function(j) {
numericInput(paste0("inp2",i,"_",j),paste0("Treatment #",j," Outcome #",i),value = 0,min = 0,max = 1,step = 0.05)
}))
})
})
output$values = renderPrint({
str(
lapply(seq(input$cols),function(i) {
lapply(seq(input$rows),function(j) {
input[[paste0("inp2",j)]]
})
})
)
})
}
shinyApp(ui,server)