我的循环有一个小问题。这是循环的代码:
for (i in 1:length(input$count))
{
id<-paste("text",i)
titles[i]<-input$id
}
Run Code Online (Sandbox Code Playgroud)
这将返回以下错误
Titles[i] <- input$id 中的错误:替换长度为零
ui.R
library(shiny)
ui <- fluidPage(
numericInput("count", "Number of textboxes", 3),
hr(),
uiOutput("textboxes")
)
Run Code Online (Sandbox Code Playgroud)
服务器R
server <- function(input, output, session) {
output$textboxes <- renderUI({
if (input$count == 0)
return(NULL)
lapply(1:input$count, function(i) {
id <- paste0("text", i)
print(id) // its prints the text1, text2,text3
numericInput(id, NULL, value = abc)
print(input$text1) //it should print value abc , but it is not, why??
})
})
}
Run Code Online (Sandbox Code Playgroud)
小智 7
此错误表明您的输入 id 为 NULL 或长度为 0 的向量:确保索引正确。
此外,在 R 中,通常最好避免 for 循环,因为它们往往非常慢:请参阅为什么 R 中的循环很慢?。几乎总有一种方法可以避免使用 for 循环并改用向量化函数,尽管目前的示例并未提供足够的细节来实际建议函数。
归档时间: |
|
查看次数: |
50537 次 |
最近记录: |