如何从 R Shiny 上的用户那里获取序数变量的顺序
How to get the order of an ordinal variable from users on R Shiny
我想从用户那里获取一串数字,例如 3,2,1 或 3,1,2,并使用此信息对序数变量进行排序。这是我的代码,但它不起作用。
server.R:
function(input, output, session) {
eventReactive(input$run_ord, {
order=c(input$correct_order)
})
output$ntext <- renderText({
print(str(order()))
})
}
ui.R:
fluidPage(theme = shinytheme("spacelab"),
navbarPage(("Structure"), id = "allResults",
tabPanel(value='set_ord', title = 'Ordinality',
sidebarLayout(
sidebarPanel(
h5("Specify the correct order below"),
#price_tiers_new : Mainstream ; Premium ; Super Premium ; Value ;
textInput("correct_order", "","4,1,2,3"),
actionButton("run_ord", "Run!"),
width = 3
),
mainPanel(
verbatimTextOutput("ntext")
)
)
)
)
)
想法是:如果用户输入 3,1,2,我会在执行重新排序的函数中按 order=c(3,1,2) 使用它。
欢迎任何意见。谢谢!
@Micael Bird 已经说过了。您需要以某种方式将字符串转换为数字向量。这是一个使用 stringi
包中的 strsplit
函数的解决方案。
两个快速笔记
order
应该是 èventReactive
的 return 值。
- 如果要显示无法强制转换为字符串的输出,请使用
renderPrint
而不是 renderText
。
服务器代码:
library(dplyr)
library(stringi)
function(input, output, session) {
order = eventReactive(input$run_ord, {
strsplit(input$correct_order,",") %>% unlist %>% as.numeric()
})
output$ntext <- renderPrint({
str(order())
})
}
我想从用户那里获取一串数字,例如 3,2,1 或 3,1,2,并使用此信息对序数变量进行排序。这是我的代码,但它不起作用。
server.R:
function(input, output, session) {
eventReactive(input$run_ord, {
order=c(input$correct_order)
})
output$ntext <- renderText({
print(str(order()))
})
}
ui.R:
fluidPage(theme = shinytheme("spacelab"),
navbarPage(("Structure"), id = "allResults",
tabPanel(value='set_ord', title = 'Ordinality',
sidebarLayout(
sidebarPanel(
h5("Specify the correct order below"),
#price_tiers_new : Mainstream ; Premium ; Super Premium ; Value ;
textInput("correct_order", "","4,1,2,3"),
actionButton("run_ord", "Run!"),
width = 3
),
mainPanel(
verbatimTextOutput("ntext")
)
)
)
)
)
想法是:如果用户输入 3,1,2,我会在执行重新排序的函数中按 order=c(3,1,2) 使用它。
欢迎任何意见。谢谢!
@Micael Bird 已经说过了。您需要以某种方式将字符串转换为数字向量。这是一个使用 stringi
包中的 strsplit
函数的解决方案。
两个快速笔记
order
应该是èventReactive
的 return 值。- 如果要显示无法强制转换为字符串的输出,请使用
renderPrint
而不是renderText
。
服务器代码:
library(dplyr)
library(stringi)
function(input, output, session) {
order = eventReactive(input$run_ord, {
strsplit(input$correct_order,",") %>% unlist %>% as.numeric()
})
output$ntext <- renderPrint({
str(order())
})
}