摘要中的警告(as.numeric(粘贴(输入$ to,"输入$ to")):由Riny中的强制引入的NAs

时间:2018-05-17 07:27:54

标签: r shiny

我对使用R的Shiny Web应用程序非常新。我想根据checkboxGroupInput中的选择生成摘要,这是我在浏览CSV文件时动态生成的。我的问题是,当我想从String转换为数字时,它打印NA。 我上传了我的两个文件,分别是ui.r和server.r.我两天都在努力。如果有人帮助我,那对我来说非常有益。

如果我的代码中有任何错误,请以正确的方式建议我。

ui.r

library(shiny)
library(shinythemes)


shinyUI(fluidPage(

  theme = shinytheme("cyborg"),
  themeSelector(),
 # Application title
  titlePanel("Data Analytics and Visualization Dashboard"),


  sidebarLayout(
  sidebarPanel(
  fileInput('datafile', 'Choose CSV file',accept=c('text/csv', 'text/comma- 
  separated-values,text/plain')),
  h5("Max file size to upload is 5 MB."),
  radioButtons("sep", "Seperator", choices = c(Comma = ',', semicolon = ';', 
  tab = "\t", space = " " )),
  #checkboxInput("header", "Header?")

  br(),

   h4("Select columns from CSV"),

  uiOutput("toCol"),
  br(),

  h4("Summary"),
  textOutput("sum")
  # tableOutput("disp")
  ),


mainPanel(
  numericInput("obs", "Enter the number of rows to display:", 5),

  tableOutput("input_file"),
  plotOutput("p")



 )
 )
 ))

server.r

library(shiny)

shinyServer(function(input, output,session) {


#This function is repsonsible for reading a csv file
output$input_file <- renderTable({

file_to_read = input$datafile
if(is.null(file_to_read))
{
  return()
}

read.csv(file_to_read$datapath, sep = input$sep, nrows = input$obs))

 })


 #This function is repsonsible for loading in the selected file
 filedata <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
    # User has not uploaded a file yet
      return(NULL)
  }

  read.csv(infile$datapath,nrows = input$obs)
   })

  #The following set of functions populate the column selectors
  output$toCol <- renderUI({
    df <-filedata()
     if (is.null(df)) return(NULL)
       items=names(df)
       names(items)=items
       checkboxGroupInput("to", "Columns",items)

   })

   observe({
      # db <- gsub(",","",input$to)
       #  print(db)
       # paste( intToUtf8(160), input$to, intToUtf8(160))

      # print(summary(as.numeric(as.character(  paste( " ", input$to, " 
       #"))))) })
     print(summary(as.numeric( input$to) ))})
     # output$sum <- renderPrint({

    #   summary(input$data.frame[,as.numeric(input$var)])
    #  })

       # output$disp <- renderTable({
      #    input$to
       # })

   # output$summary1 <- renderPrint({
    #    sum <- as.numeric(as.character(input$to))
   # summary(sum)
   #})
     })

2 个答案:

答案 0 :(得分:0)

就像@Codeer所说,你的代码中没有像summary(as.numeric(paste(input$to, “input$to”)))这样的行。我编辑了你的代码,因此没有显示所有未注释的行,因为没有必要显示它们。

在您的示例中,您将csv文件加载两次,这是您可以明确避免的。 我只将csv-loading移动到了反应中。然后,您可以在闪亮应用程序中的任何位置访问已加载的文件。我认为在您的print(summary())声明中,您错过了数据,因为您只打印了input$to变量的摘要,这只是文本,如果您将其转换为数字,则会创建NA-值。

所以我稍微重新安排了你的代码,我认为它的行为与你想要的方式相符。

library(shiny)
library(shinythemes)

ui <- {shinyUI(fluidPage(
  theme = shinytheme("cyborg"),
  themeSelector(),
  titlePanel("Data Analytics and Visualization Dashboard"),
  sidebarLayout(
    sidebarPanel(
      fileInput('datafile', 'Choose CSV file',accept=c('text/csv', 'text/comma- 
                                                       separated-values,text/plain')),
      h5("Max file size to upload is 5 MB."),
      radioButtons("sep", "Seperator", choices = c(Comma = ',', semicolon = ';', 
                                                   tab = "\t", space = " " )),
      br(),
      h4("Select columns from CSV"),
      uiOutput("toCol"),
      br(),
      h4("Summary"),
      textOutput("sum")
      ),
    mainPanel(
      numericInput("obs", "Enter the number of rows to display:", 5),
      tableOutput("input_file"),
      verbatimTextOutput("summary"),
      plotOutput("p")
    )
  )
  ))}

server <- shinyServer(function(input, output,session) {

  #This function is repsonsible for loading and reading a csv file
  filedata <- reactive({
    req(input$datafile)
    infile <- input$datafile
    if (is.null(infile)) {
      # User has not uploaded a file yet
      return(NULL)
    }
    read.csv(infile$datapath,nrows = input$obs, sep = input$sep)
  })

  output$input_file <- renderTable({
    filedata()
  })

  #The following set of functions populate the column selectors
  output$toCol <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    checkboxGroupInput("to", "Columns",items)
  })

  output$summary <- renderPrint({
    req(input$to)
    data <- filedata()
    print(summary(data[,input$to]))
  })
})

shinyApp(ui, server)

csv文件加载在reactive(filedata)中。在renderTable中,您只需输入反应变量 - filedata()。在观察中,您再次调用反应变量,并仅在所单击的列(input$to)中打印出数据摘要。

答案 1 :(得分:0)

这可能是一个起点,虽然我不建议将其用于高效的应用程序,因为登录过程不是真正安全也不加密。它完全基于文本数据。

但是你必须将ui放在服务器中并根据登录状态呈现页面。所以有2个renderUI但只有1个服务器功能。我不知道你是否可以拥有2个不同的服务器功能并重定向它们。我认为这一切都必须在1个服务器功能中。

library(shiny)

username = "joe"
password = "joe123"


ui <- fluidPage(
  uiOutput("ui")
)

server <- function(input, output, session) {

  LOGGED <- reactiveValues(user = FALSE)

  observeEvent(input$action, {
    if ((input$name == username ) & (input$pass == password)) {
      LOGGED$user = TRUE
    } else {
      LOGGED$user = FALSE
    }
  })

  observe({
    if (LOGGED$user == FALSE) {
      output$ui <- renderUI({
        tagList(
          p(HTML("User is joe <br> and password is joe123")),
          textInput("name", "Enter your username"),
          passwordInput("pass", "Enter your password"),
          actionButton("action", label = "Action")
        )
      })
    } else if (LOGGED$user == TRUE) {
      output$ui <- renderUI({
        tagList(
          h1("You are logged in.")
        )
      })
    }
  })
}

shinyApp(ui, server)