Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R “内部输入”;至于;环_R_Loops_Shiny_Counter - Fatal编程技术网

R “内部输入”;至于;环

R “内部输入”;至于;环,r,loops,shiny,counter,R,Loops,Shiny,Counter,我对用R编写代码相当陌生,对R语言也很陌生,如果有任何帮助,我将不胜感激 我正试图设计一个简单的计数器应用程序,我将需要许多行动按钮。我想知道是否有可能将动作按钮的InputID组合成一个矩阵,以便在应用程序的“服务器”部分创建一个循环,在这个循环中,我可以将特定的InputID引用为矩阵元素 最终目标是能够单击应用程序中的某个操作按钮,不仅在单击后将其值显示为标签,还可以将该值存储在矩阵中。我计划做几个不同的矩阵,这就是为什么我想创建一个循环来缩短代码 下面的代码是我到目前为止所做的,以显示我

我对用R编写代码相当陌生,对R语言也很陌生,如果有任何帮助,我将不胜感激

我正试图设计一个简单的计数器应用程序,我将需要许多行动按钮。我想知道是否有可能将动作按钮的InputID组合成一个矩阵,以便在应用程序的“服务器”部分创建一个循环,在这个循环中,我可以将特定的InputID引用为矩阵元素

最终目标是能够单击应用程序中的某个操作按钮,不仅在单击后将其值显示为标签,还可以将该值存储在矩阵中。我计划做几个不同的矩阵,这就是为什么我想创建一个循环来缩短代码

下面的代码是我到目前为止所做的,以显示我正在尝试的内容,但它不起作用

a1_q1 <- 0
a1_q2 <- 0
a1_q3 <- 0
a1_q4 <- 0

a2_q1 <- 0
a2_q2 <- 0
a2_q3 <- 0
a2_q4 <- 0

v1 <- c(a1_q1,a1_q2,a1_q3,a1_q4)
v2 <- c(a2_q1,a2_q2,a2_q3,a2_q4)
ma <- rbind(v1,v2)

inputbuttons <- c("a","b","c","d","e","f","g","h")
minputbuttons <- matrix(inputbuttons,nrow = 2,ncol = 4)

ui <- fluidPage(
  navbarPage("Scouting",
             tabPanel("Attack",
                      navlistPanel(
                        tabPanel("A",
                                 sidebarPanel(
                                   tags$b("A matrix")
                                 ),
                                 mainPanel(
                                   tags$b("Quality: 1-4"),
                                   br(),
                                   actionButton(minputbuttons[1,1], a_q1,
                                                style = "width: 100px; height:100px; background-color:blue; color:white;"),
                                   actionButton(minputbuttons[1,2], a_q2,
                                                style = "width: 100px; height:100px; background-color:blue; color:white;"),
                                   actionButton(minputbuttons[1,3], a_q3,
                                                style = "width: 100px; height:100px; background-color:blue; color:white;"),
                                   actionButton(minputbuttons[1,4], a_q4,
                                                style = "width: 100px; height:100px; background-color:blue; color:white;"),
                                   br(),
                                   actionButton(minputbuttons[2,1], a_q1,
                                                style = "width: 100px; height:100px; background-color:blue; color:white;"),
                                   actionButton(minputbuttons[2,2], a_q2,
                                                style = "width: 100px; height:100px; background-color:blue; color:white;"),
                                   actionButton(minputbuttons[2,3], a_q3,
                                                style = "width: 100px; height:100px; background-color:blue; color:white;"),
                                   actionButton(minputbuttons[2,4], a_q4,
                                                style = "width: 100px; height:100px; background-color:blue; color:white;")
                                 )
                        )
                      )
             )
  )
)


server <- function(input, output, session) {
  for (i in 1:2){
    for (j in 1:4){
      observeEvent(
        input$minputbuttons[i,j],
        ma[i,j] <<- ma[i,j] + 1,
        updateActionButton(session,minputbuttons[i,j],label = ma[i,j])
      )
    }
  }
}

shinyApp(server = server, ui = ui)

a1_q1对于这种情况,循环在服务器端不起作用,您应该尝试
lappy

库(闪亮)

根据我所读到的,我相信你在寻找。问题不是循环本身的
,而是循环变量被修改了。这可以通过在循环中使用循环变量的本地副本来修复。尽管使用
lappy
etc可能更好。