R 使用quote()和substitute()替换闪亮应用程序中的条件

R 使用quote()和substitute()替换闪亮应用程序中的条件,r,shiny,conditional-statements,subset,quote,R,Shiny,Conditional Statements,Subset,Quote,我正在尝试使用quote/substitute作为一种方法来在shiny中应用条件。我对quote/substitute或shiny都不是很熟悉,所以我很可能没有以正确的方式处理这个问题 我在下面创建了一个简单的例子来说明我遇到的问题 #Create test dataframe test<-data.frame(x=c(0:10), y=c(rep(1,5),rep(2,6)), z=c("A","A","A","B","B","B","C","C","C","C","C")) #ex

我正在尝试使用quote/substitute作为一种方法来在shiny中应用条件。我对quote/substitute或shiny都不是很熟悉,所以我很可能没有以正确的方式处理这个问题

我在下面创建了一个简单的例子来说明我遇到的问题

#Create test dataframe
test<-data.frame(x=c(0:10), y=c(rep(1,5),rep(2,6)), z=c("A","A","A","B","B","B","C","C","C","C","C"))

#example of what I would like to do outside shiny app 
test[test$x > 5,]

#or using quote and eval 
test[eval(quote(test$x > 5)),]
#创建测试数据帧
测试5,]
#或者使用quote和eval
测试[评估(报价(测试$x>5)),]
以上所有代码都有效。但现在让我们假设我想在一个闪亮的应用程序中应用它(并允许用户选择条件):

#创建简单闪亮的应用程序
需要(有光泽)
#服务器

服务器一个快速解决方案可能是使用
deparse
包装,然后使用
eval(parse
)进行解析。现在还不完全清楚为什么输入需要是表达式。如果这只是用于子集,那么有更简单的方法来完成相同的操作

library(shiny)
-用户界面

-输出


我准确地复制了您的代码,遇到错误“无效的下标类型‘列表’”.你知道为什么它为你而不是为我运行吗?@Robert.S我用的是
shinny_1.0.3
,如果是那样的话。我也用了shinny_1.0.3。在尝试解决这个问题时,我得到了一些其他奇怪的结果。例如,我尝试向radioButtons条件变量添加一个选定的参数:
radioButtons(“conditon”,“condition”,choices=list(cond_1=deparse(替换(test$x>5)),cond_2=deparse(替换(test$x>5)),
但我随后得到错误
所选参数的长度必须为1
。然而,当我运行
length(替换(test$x>5)))
我得到了结果
1
。我发现了(某种程度上)问题所在。我一直在按“运行应用程序”按R studio顶部的按钮来执行此操作-这会产生错误。如果我只需通过选择代码并按CTRL+ENTER直接运行它,它就会工作。了解原因吗?无论如何,感谢您的帮助,我将接受您的答案。
library(shiny)
ui <- fluidPage(
  radioButtons("conditon", "Condition", 
                  choices = list(cond_1 = deparse(substitute(test$x > 5)),
                                 cond_2 = deparse(substitute(test$x<5))),
            selected = deparse(substitute(test$x > 5)) ),

   tableOutput("table")

  )
server <- function(input, output) {

  # subset of nodes
  df <- reactive({

    #eliminate certain observations 
     test[eval(parse(text=input$conditon)),, drop = FALSE]


  })

  output$table <- renderTable({
     df()


  })


}
shinyApp(ui = ui, server = server)