【问题标题】:How do I get my shinyApp to read in my data file?如何让我的 shinyApp 读取我的数据文件?
【发布时间】:2020-04-07 09:43:50
【问题描述】:

我的 github 存储库中有我的 server.R、ui.R 和 csv 文件。当我使用

从 R 运行我的闪亮应用程序时
shiny::runGitHub("st558projectfinal","cgcomora")

我收到object 'crimes' can't be found 的错误。关于我做错了什么有什么建议吗?我尝试将读取 csv 文件并将诸如东北之类的变量初始化到 shinyServer() 行上方的区域的代码移动,但这也不起作用。这是我的 server.R 文件。非常感谢任何帮助!

shinyServer(function(input, output,session){

  crimes <- read.csv("cgcomora/st558projectfinal/hate_crimes.csv")
  # initialize variables for each region of the U.S. 
  northeast <- c("Maine","New Hampshire","Vermont",
                 "Massachusetts","Rhode Island",
                 "Connecticut","New York","New Jersey",
                 "Pennsylvania")
  midwest <- c("Ohio","Michigan","Indiana","Illinois",
               "Wisconsin","Minnesota","Iowa","Missouri",
               "North Dakota", "South Dakota","Nebraska",
               "Kansas")
  south <- c("Texas","Oklahoma","Arkansas","Louisiana",
             "Mississippi","Tennesee","Alabama","Georgia",
             "Florida","West Virginia", "Kentucky",
             "Delaware","Maryland","Virginia","North Carolina",
             "South Carolina","District of Columbia")
  west <- c("Washington","Idaho","Montana","Wyoming",
            "Oregon","California","Nevada","Utah",
            "Arizona","Colorado","New Mexico",
            "Alaska","Hawaii")
  region <- vector()
  for (i in 1:length(crimes$state)){
    if(is.element(crimes$state[i], northeast)==TRUE){
      region[i]= "northeast"}
    else if(is.element(crimes$state[i], midwest)==TRUE){
      region[i]= "midwest"}
    else if(is.element(crimes$state[i], west)==TRUE){
      region[i] = "west"}
    else {region[i] = "south"}
  }   

  #define new column in data set with region assigned to state
  crimes$region <- region
  #create dataframe for unsupervised learning
  df <- column_to_rownames(crimes, var = "state")
  df <- na.omit(df)
  df <- select(df, -12)
  df_scale <- scale(df)
     output$introduction <- renderText({
        "This is my introduction"
    })

     output$data <- renderText({
       "This page allows you to create some basic graphical
       and numeric summaries including a scatter plot, boxplot,
       and statistical summary for any of the variables in the 
       'crimes' dataset."
     })

    selectedData <- reactive({
      crimes %>% filter(region ==input$region)
    })
    selectData2 <- reactive({
      crimes[ , c("region",input$y)]
    })

    selectData3 <- reactive({
      crimes[ , c("region", input$y2)]
    })

    mydata <- reactive({
      crimes[ ,input$var]
    })

    mycluster <- reactive({
      kmeans(df_scale, centers = input$k, nstart = 25)
    })

    d <- reactive({
      dist(df, method = input$dmethod)
    })

    clusterplot <- reactive({
      plot(hclust(d(), method = input$cmethod))
    })

    modelmethod <- reactive({
      input$regmethod
    })

    p <- reactive({
      ggplot(crimes, aes_string(x = input$x,
                                y = input$y)) + 
        geom_point(size = 3) + 
        theme(axis.text.x = element_blank(),
              axis.ticks.x = element_blank(),
              axis.text.y = element_blank())
    })

    output$save <- downloadHandler(
      filename = "save.png",
      content = function(file){
        ggsave(p(), filename = file)
      })

    dt <- reactive({
      input$mytable1
    })

    output$table <- downloadHandler(
      filename = "table.csv",
      content = function(file){
        s = input$mytable1_rows_all
        write.csv(df[s, ,drop = FALSE],file,row.names = TRUE)
      }
    )

    #render scatter plot
    output$scatterplot <- renderPlot({
      scatter <- ggplot(crimes, aes_string(x = input$x,
                                        y = input$y)) + 
        geom_point(size = 3) + 
        theme(axis.text.x = element_blank(),
              axis.ticks.x = element_blank(),
              axis.text.y = element_blank())
      scatter
    })

    output$hover_info <- renderPrint({
      cat("input$plot_hover:\n")
      str(input$plot_hover$coords_img)
    })

    output$boxplot <- renderPlot({
      ggplot(selectData3(), aes(x = region, y = selectData3()[ ,input$y2])) + 
        geom_boxplot(aes(fill = region)) + ylab(input$y2)+
        theme(axis.text.x = element_blank(),
              axis.ticks.x = element_blank(),
              axis.text.y = element_blank())
    })

    #render numerical summary
    output$summary <- renderPrint({
      dataset <- na.omit(mydata())
      summary(dataset)
    })

    output$cluster <- renderPlot({
      fviz_cluster(mycluster(),data = df)
    })

    output$tree <- renderPlot({
      clusterplot()
    })

    #render data table with ability to filter
    output$mytable1 <- DT::renderDataTable({
      DT::datatable(df,class = "display nowrap compact",
                    filter = "top")
    })

    output$model <- renderPrint({
      myformula <- as.formula(paste(input$yvar,"~",input$xvar))
      model <- train(myformula, df,
                     method = modelmethod(),
                     trControl = trainControl(
                       method = "cv", number = 10,
                       verboseIter = TRUE))
      print(model)
    })

})

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您需要使用相对路径。假设您在与您的应用程序相同的文件夹中有hate_crimes.csv,您应该只使用crimes &lt;- read.csv("hate_crimes.csv") 来读取文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      相关资源
      最近更新 更多