R 无法调用输入$";选项类型1“;
我正在尝试在我闪亮的应用程序中创建动态UI。每次通过按钮添加输入时,我都会增加一个变量(dealNumber)。但是,我需要从这些新输入中获取值。我将dealNumber的值添加到每个输入的ID中。但是,我很难提取这些值R 无法调用输入$";选项类型1“;,r,shiny,shiny-server,shiny-reactivity,R,Shiny,Shiny Server,Shiny Reactivity,我正在尝试在我闪亮的应用程序中创建动态UI。每次通过按钮添加输入时,我都会增加一个变量(dealNumber)。但是,我需要从这些新输入中获取值。我将dealNumber的值添加到每个输入的ID中。但是,我很难提取这些值 #I use the following code to create a new input #dealNumber = 1 column(2,selectInput(paste("optionType",dealNumber,sep=""), label = h5("")
#I use the following code to create a new input
#dealNumber = 1
column(2,selectInput(paste("optionType",dealNumber,sep=""), label = h5(""),choices = option_type, selected = 1)
#I then need to assign the value from the input above to the variable OptionType. If i use input$"OptionType1" or input$OptionType1 it works. But I need to get the number 1 via a variable so that the code is dynamic.
#I have tried the code below without any sucess
assign("OptionType",input$paste("OptionType",dealNumber,sep=""),.GlobalEnv)
我将感谢任何帮助
谢谢基本上,您希望将字符串变量作为“参数”传递给
输入
对象以检索值。这可以通过输入[[“myString”]]
实现
要说明如何将其用于动态分配的ID,请参见以下示例
create_slider <- function(i) {
sliderId <- paste0("slider", i)
sliderInput(sliderId, sliderId, 0, 1, 0)
}
shinyApp(
fluidPage(
create_slider(1),
create_slider(2),
create_slider(3),
numericInput("get_id", "get value of slider", 1, 1, 3, 1),
textOutput("text")
),
function(input, output, session) {
output$text <- renderText({
input[[ paste0("slider", input$get_id) ]]
})
}
)
请始终记住,您正在构造的ID必须是唯一的
和有效的HTML
id(无空格!)。因此,paste0
在这种情况下非常有用
也许你应该考虑使用编程方式。
分配
input
插槽,以避免服务器端繁琐的字符串解析。使用input[[“OptionType1”]]
存档此问题。已解决。格雷戈,非常感谢你。我真的很感谢你的帮助。
getDynamicInput <- function(dealNumber, input) {
input[[ paste0("optionType", dealNumber) ]]
}