我想提取脚本的server
参数的内容,该脚本为我的包调用shinyApp()
闪亮对象
如果我有此代码:
code <-
'library(tidyverse)
library(shiny)
df <-
mpg %>%
filter(cty > 25)
runApp(shinyApp(
ui = fluidPage(
numericInput("x", "# of obs.", 20),
plotOutput("plot")
),
server = function(input, output) {
my_df <- reactive({
head(cars, input$x)
})
output$plot <- renderPlot(
plot(my_df())
)
}
))'
我想返回此代码:
my_df <- reactive({
head(cars, input$x)
})
output$plot <- renderPlot(
plot(my_df())
)
我目前正在混合使用parse()
和我的 guts(( 函数,但想象一下有更好的方法。
另外,我在哪里可以了解更多信息?rlang
或purrr
中有什么东西可以简化吗?
您可以在不使用parse
使用基本 R 中的str2lang
的情况下获得它:
str2lang(code)[[2]][3][[1]][3][[1]]
#> {
#> my_df <- reactive({
#> head(cars, input$x)
#> })
#> output$plot <- renderPlot(plot(my_df()))
#> }
>编辑使用更新的代码字符串,有 4 个表达式要解析,而不是 1 个,因此我们需要使用as.list(parse(text))
并对结果进行子集:
as.list(parse(text = code))[[4]][[2]][3][[1]][3][[1]]
#> {
#> my_df <- reactive({
#> head(cars, input$x)
#> })
#> output$plot <- renderPlot(plot(my_df()))
#> }