【问题标题】:shiny to upload data and explore crashing闪亮上传数据并探索崩溃
【发布时间】:2020-04-18 22:47:30
【问题描述】:

我想上传 csv/txt 文件,其中包含 id、时间、浓度和绘图(时间(x 轴)与浓度(y 轴)。我的应用程序启动并崩溃。当我试图将 updateelctinput 函数带到外面时reactive闪亮的应用程序没有运行。

有人能告诉我为什么会这样吗?

我在下面粘贴了我的代码:

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(title="Dashboard"),
  dashboardSidebar(
    sidebarMenu(
    menuItem("Input Data", tabName = "dashboard", icon = icon("dashboard")))),
dashboardBody(
  tabItems(
    tabItem(tabName = "dashboard",
            tabPanel("Input Route",
                     # First tab content
                     fileInput("df", "Choose text/csv File",
                               multiple = FALSE,
                               accept = c("text/csv"))),
            tags$br(),
            checkboxInput('header', 'Header', TRUE),
            radioButtons('sep', 'Separator',
                         c(Comma=',',
                           Semicolon=';',
                           Tab='\t'),
                         ','),
            radioButtons('quote', 'Quote',
                         c(None='',
                           'Double Quote'='"',
                           'Single Quote'="'"),
                         '"'),
            tabPanel("First Type",
                         # "Empty inputs" - they will be updated after the data is uploaded
                         selectInput('xcol', 'X Variable', ""),
                         selectInput('ycol', 'Y Variable', "", selected = ""),
                          selectInput('ide','Group',"",selected="")

                       ))),
  fluidRow(
    box(plotOutput("Plot1"),height = 250)
)
))

server <- function(input, output,session) {
  data <- reactive({ 
    inFile <- input$df 
    df <- read.csv(inFile$datapath, header = input$header)
    return(df)
  })
  observe({
    df1 = data()
      updateSelectInput(session,inputId = 'xcol', label = 'X Variable',
                        choices = names(df1), selected =names(df1)[2])
      updateSelectInput(session,inputId = 'ycol', label = 'Y Variable',
                        choices = names(df1), selected = names(df1)[2])})
  ### Plot
  output$Plot1<-renderPlot({plot(Plot1<-data()%>%ggplot()+
                                   geom_line(aes(x=input$xcol,y=input$ycol))+
                                   theme_bw())})
}

shinyApp(ui, server)

【问题讨论】:

  • input$df 是应用程序启动时的NULL。在data 电抗导体的开头添加req(input$df)。这将阻止所有内容,直到您上传文件。 aes(x=input$xcol,y=input$ycol) 也不起作用,因为 input$xcolinput$ycol 是字符串;使用aes_string 而不是aes
  • 嗨!我根据建议更新了代码仍然不起作用。
  • Warning: Error in UseMethod: no applicable method for 'req' applied to an object of class "NULL" 这是我在编译时遇到的错误。闪亮和服务器
  • @Ravua1992,这很奇怪。您正在加载另一个包裹吗? shiny::req(input$df) 有效吗?
  • @StéphaneLaurent 是的,也尝试过,但没有帮助。粘贴下面的代码供您查看。

标签: r shiny shinydashboard


【解决方案1】:

这个怎么样?

library(shiny)
library(datasets)

ui <- shinyUI(fluidPage(
  titlePanel("Column Plot"),
  tabsetPanel(
    tabPanel("Upload File",
             titlePanel("Uploading Files"),
             sidebarLayout(
               sidebarPanel(
                 fileInput('file1', 'Choose CSV File',
                           accept=c('text/csv', 
                                    'text/comma-separated-values,text/plain', 
                                    '.csv')),

                 # added interface for uploading data from
                 # http://shiny.rstudio.com/gallery/file-upload.html
                 tags$br(),
                 checkboxInput('header', 'Header', TRUE),
                 radioButtons('sep', 'Separator',
                              c(Comma=',',
                                Semicolon=';',
                                Tab='\t'),
                              ','),
                 radioButtons('quote', 'Quote',
                              c(None='',
                                'Double Quote'='"',
                                'Single Quote'="'"),
                              '"')

               ),
               mainPanel(
                 tableOutput('contents')
               )
             )
    ),
    tabPanel("First Type",
             pageWithSidebar(
               headerPanel('My First Plot'),
               sidebarPanel(

                 # "Empty inputs" - they will be updated after the data is uploaded
                 selectInput('xcol', 'X Variable', ""),
                 selectInput('ycol', 'Y Variable', "", selected = "")

               ),
               mainPanel(
                 plotOutput('MyPlot')
               )
             )
    )

  )
)
)

server <- shinyServer(function(input, output, session) {
    # added "session" because updateSelectInput requires it


  data <- reactive({ 
    req(input$file1) ## ?req #  require that the input is available

    inFile <- input$file1 

    # tested with a following dataset: write.csv(mtcars, "mtcars.csv")
    # and                              write.csv(iris, "iris.csv")
    df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
             quote = input$quote)


    # Update inputs (you could create an observer with both updateSel...)
    # You can also constraint your choices. If you wanted select only numeric
    # variables you could set "choices = sapply(df, is.numeric)"
    # It depends on what do you want to do later on.

    updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                      choices = names(df), selected = names(df))
    updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                      choices = names(df), selected = names(df)[2])

    return(df)
  })

  output$contents <- renderTable({
      data()
  })

  output$MyPlot <- renderPlot({
    # for a histogram: remove the second variable (it has to be numeric as well):
    # x    <- data()[, c(input$xcol, input$ycol)]
    # bins <- nrow(data())
    # hist(x, breaks = bins, col = 'darkgray', border = 'white')

    # Correct way:
    # x    <- data()[, input$xcol]
    # bins <- nrow(data())
    # hist(x, breaks = bins, col = 'darkgray', border = 'white')


    # I Since you have two inputs I decided to make a scatterplot
    x <- data()[, c(input$xcol, input$ycol)]
    plot(x)

  })
})

shinyApp(ui, server)

查看下面的链接,了解如何处理此问题的其他一些想法。

https://shiny.rstudio.com/gallery/

【讨论】:

    【解决方案2】:

    app.R

    library(shiny)
    library(shinydashboard)
    ui <- dashboardPage(
      dashboardHeader(title="Dashboard"),
      dashboardSidebar(
        sidebarMenu(
        menuItem("Input Data", tabName = "dashboard", icon = icon("dashboard")))),
    dashboardBody(
      tabItems(
        tabItem(tabName = "dashboard",
                tabPanel("Input Route",
                         # First tab content
                         fileInput("df", "Choose text/csv File",
                                   multiple = FALSE,
                                   accept = c("text/csv"))),
                tags$br(),
                checkboxInput('header', 'Header', TRUE),
                radioButtons('sep', 'Separator',
                             c(Comma=',',
                               Semicolon=';',
                               Tab='\t'),
                             ','),
                radioButtons('quote', 'Quote',
                             c(None='',
                               'Double Quote'='"',
                               'Single Quote'="'"),
                             '"'),
                tabPanel("First Type",
                             selectInput('xcol', 'X Variable', ""),
                             selectInput('ycol', 'Y Variable', "", selected = ""),
                              selectInput('ide','Group',"",selected="")
    
                           ))),
      # Boxes need to be put in a row (or column)
      fluidRow(
        box(plotOutput("Plot1"),height = 250)
    )
    ))
    server <- function(input, output,session) {
      data <- reactive({ 
       shiny::req(input$df)
        inFile <- (input$df)
        df <- read.csv(inFile$datapath, header = input$header)
        return(df)
      })
      observe({
        df1 = data()
          updateSelectInput(session,inputId = 'xcol', label = 'X Variable',
                            choices = names(df1), selected =names(df1)[2])
          updateSelectInput(session,inputId = 'ycol', label = 'Y Variable',
                            choices = names(df1), selected = names(df1)[2])})
      output$Plot1<-renderPlot({plot(Plot1<-data()%>%ggplot()+
                                       geom_line(aes_string(x=input$xcol,y=input$ycol))+
                                       theme_bw())})
    }
    shinyApp(ui, server)
    
    

    【讨论】:

      猜你喜欢
      • 2022-06-30
      • 2015-05-28
      • 2015-10-05
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 2016-08-25
      相关资源
      最近更新 更多