在“==”语句中使用 R 闪亮输入
Using an R shiny input in an '==' statement
在 R shiny 中,我希望在使用 == 的语句中使用用户的输入,但我尝试过的方法都以返回 NA 的值结束。这是我的代码的示例版本,因此您可以看到我要完成的工作:
ui <- fluidPage(
titlePanel("Example Shiny"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "ID",
label = "Choose an ID:",
choices = unique(Data$ID)),
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
#The following statement is what gives me trouble, I am unable to have it give me anything other than an NA from the Data$ID == input$ID
Data.Shiny = Data[Data$ID == input$ID ,]
#There is more code after this which I have tested and works, only the above statement is causing an issue
我尝试了多种解决方案,主要是尝试将 input$ID 转换为不同类型的变量,例如使用 toString() 或 as.character(),但这些都不适合我.我对闪亮的编码还是个新手,我认为这可能与制作反应变量有关,但我不完全确定。
如有任何帮助或建议,我们将不胜感激!
谢谢
编辑:
我能够通过简单地在闪亮代码之外创建一个函数并调用它而不是我之前编写的函数来解决这个问题。基本上:
to.New.DF <- function(ID){
Data.Shiny = Data[Data$ID == ID ,]
return(Data.Shiny)
}
.
.
.
server <- function(input, output) {
output$distPlot <- renderPlot({
Data.Shiny = to.New.DF(input$ID)
当我用内置的 R 函数尝试了几个类似的东西时,不确定为什么会这样,但这解决了我的问题。
(已发布在问题中,但我正在重新发布,以便我可以将其标记为已解决)
我能够通过简单地在闪亮代码之外创建一个函数并调用它而不是我之前编写的函数来解决这个问题。基本上:
to.New.DF <- function(ID){
Data.Shiny = Data[Data$ID == ID ,]
return(Data.Shiny)
}
.
.
.
server <- function(input, output) {
output$distPlot <- renderPlot({
Data.Shiny = to.New.DF(input$ID)
当我用内置的 R 函数尝试了几个类似的东西时,我不确定为什么会这样,但这解决了我的问题。
在 R shiny 中,我希望在使用 == 的语句中使用用户的输入,但我尝试过的方法都以返回 NA 的值结束。这是我的代码的示例版本,因此您可以看到我要完成的工作:
ui <- fluidPage(
titlePanel("Example Shiny"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "ID",
label = "Choose an ID:",
choices = unique(Data$ID)),
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
#The following statement is what gives me trouble, I am unable to have it give me anything other than an NA from the Data$ID == input$ID
Data.Shiny = Data[Data$ID == input$ID ,]
#There is more code after this which I have tested and works, only the above statement is causing an issue
我尝试了多种解决方案,主要是尝试将 input$ID 转换为不同类型的变量,例如使用 toString() 或 as.character(),但这些都不适合我.我对闪亮的编码还是个新手,我认为这可能与制作反应变量有关,但我不完全确定。
如有任何帮助或建议,我们将不胜感激!
谢谢
编辑:
我能够通过简单地在闪亮代码之外创建一个函数并调用它而不是我之前编写的函数来解决这个问题。基本上:
to.New.DF <- function(ID){
Data.Shiny = Data[Data$ID == ID ,]
return(Data.Shiny)
}
.
.
.
server <- function(input, output) {
output$distPlot <- renderPlot({
Data.Shiny = to.New.DF(input$ID)
当我用内置的 R 函数尝试了几个类似的东西时,不确定为什么会这样,但这解决了我的问题。
(已发布在问题中,但我正在重新发布,以便我可以将其标记为已解决)
我能够通过简单地在闪亮代码之外创建一个函数并调用它而不是我之前编写的函数来解决这个问题。基本上:
to.New.DF <- function(ID){
Data.Shiny = Data[Data$ID == ID ,]
return(Data.Shiny)
}
.
.
.
server <- function(input, output) {
output$distPlot <- renderPlot({
Data.Shiny = to.New.DF(input$ID)
当我用内置的 R 函数尝试了几个类似的东西时,我不确定为什么会这样,但这解决了我的问题。