上传csv后创建新的数据帧

时间:2017-12-07 22:18:52

标签: r shiny

我正在尝试创建一个闪亮的应用程序,我上传一个csv,基于在该csv中搜索某些值创建一个新的数据框,然后绘制新的数据帧。

然而,我遇到麻烦让新数据帧被反应,从我对闪亮的理解我应该在一个被动类中定义新的数据帧,但是我的代码正在生成一个正确格式的数据帧,但是我的代码是0期待价值观。

我是否以正确的方式创建新数据框?

在我的服务器中.R

library(shiny)
library(ggplot2)

shinyServer(function(input,output){

  seo_data<-reactive({
    file1<-input$file
    if(is.null(file1)){return()}
    seo_data<-read.table(file=file1$datapath,sep=input$sep,header=input$header,stringsAsFactors = input$stringsAsFactors)

    seo_data2<-data.frame(
      page_title_missing =sum(grepl("^$",seo_data$page_title)),
      page_title_length = length(which(seo_data$page_title_length>70)) ,
      page_title_duplicates= sum(seo_data$duplicate_title),

      meta_desc_missing=sum(grepl("^$",seo_data$description)),
      meta_desc_length=length(which(seo_data$description_length>155)),
      meta_desc_duplicate=sum(seo_data$duplicate_description),

      url_dynamic=length(grep("\\?",seo_data$url)),
      url_underscore =length(grep("_",seo_data$url)),
      url_uppercase = length(grep("[A-Z]",seo_data$url)),

  )

    seo_data2<-as.data.frame(t(seo_data2))
    seo_data2[,2]<-row.names(seo_data2)
    print(seo_data2)
    return(seo_data2)

    })


  output$barplot<-renderPlot({
    if(is.null(seo_data)){return()}
    ggplot(seo_data(), aes(x = V2, y = V1)) + geom_bar(stat = 'identity') + coord_flip()+ labs (x= "Audit item" , y= "Instances")

  })


  output$tb<- renderUI({

  tabPanel("summary",tableOutput("barplot"))

  })
})
在我的UI.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(
  titlePanel("Upload a CSV"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file","Upload a file"),
      helpText("Default max size is 5mb, change in server"),
      tags$hr(),
      h5(helpText("Select file options")),
      checkboxInput("header",label = 'Headear',value = F),
      checkboxInput("stringsAsFactors","stringsAsFactors",F),
      br(),
      radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
    ),
  mainPanel(
      uiOutput("tb"),
    plotOutput("barplot"))
  )

  )


  )

seo_data看起来像:

                       V1                     V2
page_title_missing      0     page_title_missing
page_title_length       0      page_title_length
page_title_duplicates   0  page_title_duplicates
meta_desc_missing       0      meta_desc_missing
meta_desc_length        0       meta_desc_length
meta_desc_duplicate     0    meta_desc_duplicate
url_dynamic             0            url_dynamic
url_underscore          0         url_underscore
url_uppercase           0          url_uppercase

但是我会期望V1中的值,而不是0,我无法弄清楚为什么会发生这种情况。

因此,当我上传csv时,代码会产生一个空条形图。

输入一些数据:

structure(list(page_title = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 6L, 2L, 2L, 7L, 4L, 
2L, 2L, 2L, 1L, 5L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 3L, 3L, 3L, 3L, 8L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
2L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), .Label = c("Details - name and address | Subscribe to the ghji Digital Pack | The ghji", 
"Details - name and address | Subscribe to the ghji Weekly | The ghji", 
"Details - name and address | Subscribe to the ghji/asdz Newspapers | The ghji", 
"Promo code: GWG20 | The ghji", "Promo code: WAK41X | The ghji", 
"Promo code: WAK51X | The ghji", "Promo code: WAK53F | The ghji", 
"Promo code: WAK53G | The ghji"), class = "factor"), page_title_length = c(76L, 
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
76L, 76L, 76L, 76L, 76L, 76L, 33L, 76L, 76L, 33L, 32L, 76L, 76L, 
76L, 82L, 33L, 82L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
76L, 76L, 82L, 82L, 89L, 89L, 89L, 89L, 33L, 89L, 89L, 89L, 89L, 
89L, 89L, 89L, 89L, 76L, 89L, 89L, 76L, 89L, 89L, 89L, 76L, 89L, 
89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 
89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 
89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L
), duplicate_title = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, 
TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE), description = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA), description_length = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), duplicate_description = c(FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE
), url = structure(c(99L, 109L, 94L, 106L, 64L, 107L, 89L, 100L, 
67L, 97L, 85L, 102L, 66L, 61L, 83L, 95L, 78L, 91L, 80L, 74L, 
75L, 70L, 87L, 81L, 92L, 68L, 60L, 103L, 62L, 90L, 86L, 72L, 
77L, 116L, 59L, 73L, 117L, 114L, 84L, 96L, 79L, 112L, 115L, 113L, 
104L, 108L, 76L, 93L, 88L, 71L, 82L, 101L, 65L, 63L, 58L, 111L, 
110L, 43L, 29L, 38L, 53L, 118L, 52L, 41L, 17L, 12L, 28L, 3L, 
16L, 42L, 98L, 13L, 1L, 69L, 6L, 47L, 18L, 105L, 34L, 51L, 33L, 
11L, 15L, 21L, 8L, 26L, 57L, 46L, 14L, 55L, 25L, 7L, 56L, 54L, 
31L, 10L, 27L, 50L, 19L, 40L, 35L, 32L, 20L, 36L, 45L, 23L, 48L, 
24L, 49L, 44L, 2L, 5L, 30L, 22L, 39L, 37L, 4L, 9L), .Label = c("https://subscribe.ghji.com/checkout/delivery-everyday", 
"https://subscribe.ghji.com/checkout/delivery-everyday?promoCode=GAH41F", 
"https://subscribe.ghji.com/checkout/delivery-everyday+", "https://subscribe.ghji.com/checkout/delivery-everyday+?promoCode=GBG80F", 
"https://subscribe.ghji.com/checkout/delivery-everyday+?promoCode=GBI41H", 
"https://subscribe.ghji.com/checkout/delivery-sixday", "https://subscribe.ghji.com/checkout/delivery-sixday?promoCode=GAA62X", 
"https://subscribe.ghji.com/checkout/delivery-sixday?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/delivery-sixday?promoCode=GBN99F", 
"https://subscribe.ghji.com/checkout/delivery-sixday+?promoCode=GAB41X", 
"https://subscribe.ghji.com/checkout/delivery-sixday+?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/delivery-sixday+?promoCode=VHD21C", 
"https://subscribe.ghji.com/checkout/delivery-sunday", "https://subscribe.ghji.com/checkout/delivery-sunday?promoCode=GBI41G", 
"https://subscribe.ghji.com/checkout/delivery-sunday?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/delivery-sunday+", "https://subscribe.ghji.com/checkout/delivery-sunday+?promoCode=VHD21C", 
"https://subscribe.ghji.com/checkout/delivery-weekend", "https://subscribe.ghji.com/checkout/delivery-weekend?promoCode=GBA80X", 
"https://subscribe.ghji.com/checkout/delivery-weekend?promoCode=GBG41F", 
"https://subscribe.ghji.com/checkout/delivery-weekend?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/delivery-weekend+?promoCode=GBA80F", 
"https://subscribe.ghji.com/checkout/delivery-weekend+?promoCode=GBB41G", 
"https://subscribe.ghji.com/checkout/delivery-weekend+?promoCode=GBG41G", 
"https://subscribe.ghji.com/checkout/delivery-weekend+?promoCode=GBG80F", 
"https://subscribe.ghji.com/checkout/delivery-weekend+?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/delivery-weekend+?promoCode=GBN99G", 
"https://subscribe.ghji.com/checkout/delivery-weekend+?promoCode=VHD21C", 
"https://subscribe.ghji.com/checkout/voucher-everyday", "https://subscribe.ghji.com/checkout/voucher-everyday?promoCode=GAH99X", 
"https://subscribe.ghji.com/checkout/voucher-everyday?promoCode=GBB41X", 
"https://subscribe.ghji.com/checkout/voucher-everyday?promoCode=GBG80X", 
"https://subscribe.ghji.com/checkout/voucher-everyday?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/voucher-everyday+", "https://subscribe.ghji.com/checkout/voucher-everyday+?promoCode=GAH80I", 
"https://subscribe.ghji.com/checkout/voucher-everyday+?promoCode=GBG41G", 
"https://subscribe.ghji.com/checkout/voucher-everyday+?promoCode=GBI41H", 
"https://subscribe.ghji.com/checkout/voucher-sixday", "https://subscribe.ghji.com/checkout/voucher-sixday?promoCode=GBI41G", 
"https://subscribe.ghji.com/checkout/voucher-sixday?promoCode=GBN99F", 
"https://subscribe.ghji.com/checkout/voucher-sixday?promoCode=VHD21C", 
"https://subscribe.ghji.com/checkout/voucher-sixday+", "https://subscribe.ghji.com/checkout/voucher-sunday", 
"https://subscribe.ghji.com/checkout/voucher-sunday?promoCode=GBA41X", 
"https://subscribe.ghji.com/checkout/voucher-sunday?promoCode=GBI41G", 
"https://subscribe.ghji.com/checkout/voucher-sunday?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/voucher-sunday+", "https://subscribe.ghji.com/checkout/voucher-sunday+?promoCode=GBG41G", 
"https://subscribe.ghji.com/checkout/voucher-sunday+?promoCode=GBG80F", 
"https://subscribe.ghji.com/checkout/voucher-sunday+?promoCode=GBI41H", 
"https://subscribe.ghji.com/checkout/voucher-sunday+?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/voucher-sunday+?promoCode=VHD21C", 
"https://subscribe.ghji.com/checkout/voucher-weekend", "https://subscribe.ghji.com/checkout/voucher-weekend?promoCode=GAB41X", 
"https://subscribe.ghji.com/checkout/voucher-weekend?promoCode=GAG41X", 
"https://subscribe.ghji.com/checkout/voucher-weekend+?promoCode=GBB41X", 
"https://subscribe.ghji.com/checkout/voucher-weekend+?promoCode=GBK31F", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweekly6issues?countryGroup=uk&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweekly6issues?countryGroup=us&promoCode=WAK41X", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweekly6issues?countryGroup=us&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyannual?countryGroup=uk", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyannual?countryGroup=uk&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyannual?countryGroup=uk&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyannual?countryGroup=us", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyannual?countryGroup=us&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyquarterly?countryGroup=uk", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyquarterly?countryGroup=us", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyquarterly?countryGroup=us&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonea-ghjiweeklyquarterly?countryGroup=us&promoCode=WAL41X", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=au&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=au&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=ca&promoCode=GWG20", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=ca&promoCode=WAK41X", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=ca&promoCode=WAK51X", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=ca&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=ca&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=eu&promoCode=WAK41X", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=eu&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=eu&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=nz&promoCode=WAK51X", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=nz&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=nz&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=us&promoCode=GWG20", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweekly6issues?countryGroup=us&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=au", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=au&promoCode=GWG20", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=au&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=au&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=ca", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=ca&promoCode=GWG20", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=ca&promoCode=WAK51X", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=ca&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=ca&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=eu", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=eu&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=eu&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=nz", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=nz&promoCode=WAL41X", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyannual?countryGroup=us", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=au", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=au&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=ca", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=ca&promoCode=WAK53F", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=ca&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=ca&promoCode=WAL41X", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=eu", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=nz", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=nz&promoCode=WAK53G", 
"https://subscribe.ghji.com/checkout/weeklyzonec-ghjiweeklyquarterly?countryGroup=us", 
"https://subscribe.ghji.com/checkout?countryGroup=au", "https://subscribe.ghji.com/checkout?countryGroup=int", 
"https://subscribe.ghji.com/checkout?countryGroup=uk", "https://subscribe.ghji.com/checkout?countryGroup=us", 
"https://subscribe.ghji.com/p/GWG20/terms", "https://subscribe.ghji.com/p/WAK41X/terms", 
"https://subscribe.ghji.com/p/WAK51X/terms", "https://subscribe.ghji.com/p/WAK53F/terms", 
"https://subscribe.ghji.com/p/WAK53G/terms"), class = "factor")), .Names = c("page_title", 
"page_title_length", "duplicate_title", "description", "description_length", 
"duplicate_description", "url"), row.names = c(NA, -118L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

正如您的代码目前所示,您有一个额外昏迷的问题:

 seo_data2 <- data.frame(
      page_title_missing =sum(grepl("^$",seo_data$page_title)),
      page_title_length = length(which(seo_data$page_title_length>70)) ,
      page_title_duplicates= sum(seo_data$duplicate_title),

      meta_desc_missing=sum(grepl("^$",seo_data$description)),
      meta_desc_length=length(which(seo_data$description_length>155)),
      meta_desc_duplicate=sum(seo_data$duplicate_description),

      url_dynamic=length(grep("\\?",seo_data$url)),
      url_underscore =length(grep("_",seo_data$url)),
      url_uppercase = length(grep("[A-Z]",seo_data$url)), #extra coma
  )

此外,需要在ui中选择(勾选)标题,以使read.table生效。

server.R

library(shiny)
library(ggplot2)

shinyServer(function(input,output){

  seo_data <- reactive({
    file1 <- input$file
    req(file1)
    seo_data <- read.table(file = file1$datapath,sep=input$sep,
                           header = input$header, 
                           stringsAsFactors = input$stringsAsFactors)

    seo_data2 <- data.frame(
      page_title_missing = sum(grepl("^$",seo_data$page_title)),
      page_title_length = length(which(seo_data$page_title_length>70)) ,
      page_title_duplicates = sum(seo_data$duplicate_title),

      meta_desc_missing = sum(grepl("^$",seo_data$description)),
      meta_desc_length = length(which(seo_data$description_length>155)),
      meta_desc_duplicate = sum(seo_data$duplicate_description),

      url_dynamic = length(grep("\\?",seo_data$url)),
      url_underscore = length(grep("_",seo_data$url)),
      url_uppercase = length(grep("[A-Z]",seo_data$url))

    )

    seo_data2 <- as.data.frame(t(seo_data2))
    seo_data2[,2] <- row.names(seo_data2)
    print(seo_data2)
    seo_data2
  })

  output$barplot<-renderPlot({
    req(seo_data)
    ggplot(seo_data(), aes(x = V2, y = V1)) + geom_bar(stat = 'identity') + coord_flip()+ labs (x= "Audit item" , y= "Instances")

  })

  output$tb<- renderUI({

    tabPanel("summary",tableOutput("barplot"))

  })
})

ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(
  titlePanel("Upload a CSV"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Upload a file"),
      helpText("Default max size is 5mb, change in server"),
      tags$hr(),
      h5(helpText("Select file options")),
      checkboxInput("header", label = 'Headear', value = F),
      checkboxInput("stringsAsFactors", "stringsAsFactors", F),
      br(),
      radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
    ),
    mainPanel(
      uiOutput("tb"),
      plotOutput("barplot"))
  )

)
)

除此之外代码有效 我做的是保存

write.csv(sample_data, "sample.csv")  

选择它作为文件输入,并在ui中勾选Headear(也可以在上传后完成)。

或者默认选中标题:

checkboxInput("header", label = 'Headear', value = T),