R闪亮应用程序错误字符串匹配



这个闪亮的应用程序的目标是让人们能够交互式地将单词的正确拼写与现有单词相匹配。这是我到目前为止提出的工作和运行,但它也抛出了一个错误。我的问题是,是否有一个更好的方式有下拉菜单的格式,我怎么能解决这个错误,我怎么能有数据框/表与用户输入保存后的事实。

谢谢!

错误:

Warning: Error in data.frame: arguments imply differing number of rows: 5, 0
101: stop
100: data.frame
99: renderDataTable [#13]
98: func
85: renderFunc
84: output$table
3: runApp
2: print.shiny.appobj
1: <Anonymous>

代码:


# Load Libraries
library(shiny)

words <- c("aapple", "apple", "bnanana", "pear", "banana")
choices = c("", "apple", "banana", "pear")
# Create User Interface
ui <- fluidPage(
titlePanel("Matching Words with Dropdown Menus"),
sidebarLayout(
sidebarPanel(
uiOutput("words_list"),
hr(),
uiOutput("dropdown_list")
), 
mainPanel(
h4("Results"),
hr(),
dataTableOutput("table")
)
)
)
# Create Server Function
server <- function(input, output) {

# Output List of Dropdown Menus
output$dropdown_list <- renderUI({
lapply(words, function(x){
selectInput(paste0("select_", x), x,
choices = choices)
})
})

# Output Table of Results
output$table <- renderDataTable({
data.frame(word = words,
type = sapply(words, function(x){
input[[paste0("select_", x)]]
}))
})
}
# Create Shiny App
shinyApp(ui = ui, server = server)

当你在renderUI()中使用输入小部件时,当应用程序启动时,input$id将是NULL,直到uiOutout("dropdown_list")被发送到浏览器。因此,在呈现表时,您需要验证输入不是NULL

library(shiny)
words <- c("aapple", "apple", "bnanana", "pear", "banana")
# replace "" with " " 
choices = c(" ", "apple", "banana", "pear")
ui <- fluidPage(
titlePanel("Matching Words with Dropdown Menus"),
sidebarLayout(
sidebarPanel(
uiOutput("words_list"),
hr(),
uiOutput("dropdown_list")
), 
mainPanel(
h4("Results"),
hr(),
dataTableOutput("table")
)
)
)
server <- function(input, output) {
output$dropdown_list <- renderUI({
lapply(words, function(x){
selectInput(paste0("select_", x), x,
choices = choices)
})
})

output$table <- renderDataTable({
data.frame(word = words,
type = sapply(words, function(x){
# check with req()
req(input[[paste0("select_", x)]])
}))
})

}
shinyApp(ui = ui, server = server)

我的一般方法是永远不要在renderUI()内使用输入小部件,因为这个;并且只对输出使用renderUI()。使用该范例,您将通过在fluidPage()中使用lapply()来创建应用程序的ui部分。如果您想随时间改变选择,则可以使用updateSelectInput()