You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

保存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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 21:52:43