【问题标题】:Creating a scatter plot using ggvis in a shiny app在闪亮的应用程序中使用 ggvis 创建散点图
【发布时间】:2021-01-23 03:18:20
【问题描述】:

我正在尝试创建一个执行以下操作的简单应用:

  • 将 csv 文件作为响应式导入(这必须是响应式)
  • 将csv的内容作为数据表打印在一个tabPanel中
  • 在另一个 tabPanel 中使用 ggvis 创建交互式散点图

但是我无法创建绘图 - 应该出现绘图的 tabPanel 是空白的。控制台中没有任何错误或警告消息。不知道代码有什么问题。

代码如下:

# Define UI for application 
ui <- fluidPage(

    # Application title
    titlePanel("App"),

    sidebarLayout(
        sidebarPanel(
            fileInput(inputId = "inputCrossTab"
                      , label = "Select the input sheet"
                      , multiple = FALSE
                      , accept = c('.csv')
                      )
        ),

        # Show a table and plot 
        mainPanel(
           tabsetPanel(id = "tabset"
                       , type = 'pills'
                       , tabPanel(title = "Table"
                                  , DT::dataTableOutput('table')
                                  )
                       , tabPanel(title = "Charts"
                                  ,ggvisOutput("plot")
                                  )
               
           )
        )
    )
)

# Define server logic required 
server <- function(input, output) {
    
    # Import csv data as a reactive
    dat <- reactive({
        
        req(input$inputCrossTab$datapath)
        dat_tab <- data.table::fread(  file   = input$inputCrossTab$datapath
                                     , header = TRUE)
        
    })
    
    # Render imported data as table
    output$table <- DT::renderDataTable({
        dat()
    })
    
    # Plot the table as a scatter plot
    plot <- reactive({
        dat()%>%
            ggvis::ggvis(~Total.x,~Total.y)%>%
            layer_points(fill:="red")%>%
            bind_shiny("plot")

    })
    
    
}

# Run the application 
shinyApp(ui = ui, server = server)

这是我正在导入的 csv 文件(它只是一个 5 行 3 列的文件...一些随机组成的数据)..

X1,Total.x,Total.y
ncksncnxzc,0.8338719625,0.0163952762
xsmkslaxmkaslx,0.5867098735,0.2033673932
njasdnsa,0.3586965341,0.8281010715
sadlasdl;,0.060212096,0.1735624054
nsakksad,0.7281606887,0.3851430044

还有一件事,为了可重现的示例,我在这里导入了一个 csv。在我的实际代码中,我正在导入一个 csv 文件并进行一些转换并创建一个响应式数据表,例如给定的 csv 数据。

请帮我解决这个问题。

谢谢, 阿努普

【问题讨论】:

    标签: r shiny shinyapps ggvis


    【解决方案1】:

    您的服务器代码中只有一个很小的错误。当您分配反应时,您必须添加“output$plot”而不仅仅是“plot”。 这是更正后的代码。

    library(ggvis)
    ui <- fluidPage(
     
     # Application title
     titlePanel("App"),
     
     sidebarLayout(
       sidebarPanel(
         fileInput(inputId = "inputCrossTab"
                   , label = "Select the input sheet"
                   , multiple = FALSE
                   , accept = c('.csv')
         )
       ),
       
       # Show a table and plot 
       mainPanel(
         tabsetPanel(id = "tabset"
                     , type = 'pills'
                     , tabPanel(title = "Table"
                                , DT::dataTableOutput('table')
                     )
                     , tabPanel(title = "Charts"
                                ,ggvisOutput("plot")
                     )
                     
         )
       )
     )
    )
    
    # Define server logic required 
    server <- function(input, output) {
     
     # Import csv data as a reactive
     dat <- reactive({
       
       req(input$inputCrossTab$datapath)
       dat_tab <- data.table::fread(  file   = input$inputCrossTab$datapath
                                      , header = TRUE)
       
     })
     
     # Render imported data as table
     output$table <- DT::renderDataTable({
       dat()
     })
     
     # Plot the table as a scatter plot
     output$plot <- reactive({
       dat()%>%
         ggvis::ggvis(~Total.x,~Total.y)%>%
         layer_points(fill:="red")%>%
         bind_shiny("plot")
       
     })
     
     
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)```
    

    【讨论】:

    • 非常感谢!非常有帮助。解决了我遇到的问题
    猜你喜欢
    • 1970-01-01
    • 2016-02-22
    • 2015-02-25
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多