保存Shiny代码片段时$符号转义失效问题求助
RStudio代码片段中$符号丢失的问题解决
问题重现
想要将以下Shiny代码保存为RStudio代码片段:
ui <- fluidPage( sidebarLayout( sidebarPanel( textOutput("panel") ), mainPanel( tabsetPanel( id = "tabset", tabPanel("panel 1", "one"), tabPanel("panel 2", "two"), tabPanel("panel 3", "three") ) ) ) ) server <- function(input, output, session) { output$panel <- renderText({ paste("Current panel: ", input$tabset) }) }
配置的代码片段如下:
snippet my_shiny_tabsetPanel ui <- fluidPage( sidebarLayout( sidebarPanel( textOutput("panel")), mainPanel( tabsetPanel( id = "tabset", tabPanel("panel 1", "one"), tabPanel("panel 2", "two"), tabPanel("panel 3", "three") )))) server <- function(input, output, session) { output$panel <- renderText({ paste("Current panel: ", input$tabset) }) }
调用片段后,output$panel变成output,input$tabset变成input,尝试用\$或$$转义无效。
解决方法
这是因为RStudio的代码片段系统中,$是占位符标记,会把$xxx识别为可编辑的占位符,导致后面的内容被截断。正确的转义方式是使用两个反斜杠加$,也就是\\$。
修改后的代码片段如下:
snippet my_shiny_tabsetPanel ui <- fluidPage( sidebarLayout( sidebarPanel( textOutput("panel")), mainPanel( tabsetPanel( id = "tabset", tabPanel("panel 1", "one"), tabPanel("panel 2", "two"), tabPanel("panel 3", "three") )))) server <- function(input, output, session) { output\\$panel <- renderText({ paste("Current panel: ", input\\$tabset) }) }
保存后再调用片段,output$panel和input$tabset就能正常显示了。
内容的提问来源于stack exchange,提问作者TarJae
相关产品推荐
相关产品推荐

