首页 > 解决方案 > 带有 2 路离散变量的 Rshiny:为什么它不起作用?

问题描述

我想构建一个应用程序,允许我在 2 个离散变量图上切换变量,但执行时出现错误:

解析错误(文件,keep.source = FALSE,srcfile = src,编码 = enc):C:\Users\HH Portable\Desktop\Dossier R\Jeux de données\Thai/ui.R:13:0: 意外结束输入 11:
mainPanel(plotOutput('plot1')) 12:) ^ 警告:sourceUTF8 中的错误:错误采购 C:\Users\HHPORT~1\AppData\Local\Temp\RtmpekudB9\fileab876054ad6 [没有可用的堆栈跟踪]

library(shiny)

# ----- UI ----------------------------
pageWithSidebar(

  headerPanel('Representer les variables'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(the)),
    selectInput('ycol', 'Y Variable', names(the)
       ),
  mainPanel(plotOutput('plot1'))
)

#----- Server.r ------------------------------
library(shiny)
library(ggplot2)
# ---- Server ----------------------------------
# ---- ggplot with options ------------------------
server=shinyServer(function(input, output, session) {
  output$plot1 <- renderPlot({
    ggplot(the, aes(x=input$xcol, ..count..*100/sum(..count..))) + 
      geom_bar(aes(fill = input$ycol), position = "fill") +
      geom_text(aes( label = scales::percent(..prop..),y= ..prop.. ), 
                stat= "count", vjust = -.5)
      })
})

标签: rggplot2shiny

解决方案


尝试get()在您的input$...asinput$xcolinput$ycolare 字符串周围使用,并且aes()不接受字符串。

下次您发布问题时,请添加一个可复制的小示例供其他人帮助您解决。

希望这可以帮助。

# ---- ggplot with options ------------------------
server=shinyServer(function(input, output, session) {
  output$plot1 <- renderPlot({
    ggplot(the, aes(x=get(input$xcol), ..count..*100/sum(..count..))) + 
      geom_bar(aes(fill = get(input$ycol)), position = "fill") +
      geom_text(aes(label = scales::percent(..prop..),y= ..prop.. ), 
                stat= "count", vjust = -.5)
      })
})

推荐阅读